Strcmp program freezing
Hey yall...
My program just freezes up after I enter my input.txt file.
I emailed it to my professor and he said it worked fine for him.
Ive compiled and ran successfly similar programs to this.. I cant figure it out..
Just sorting names and scores here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iostream>
#include <fstream>
const int size = 20;
void swap(char [], char []); //prototype void function
void swap(int &, int &);
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
char source[20];
char name[size][20];
int data[size];
int i;
int actualsize=0;
//input data
cout<<"\n Destination of source file: ";
cin>>source;
infile.open(source); // check if file opened
i = 0;
while (! infile.eof())
{
infile>>name[i];
infile>>data[i];
i++;
}
actualsize = i-1;
// prints list before being sorted
for (int i=0;i<actualsize;i++)
{
cout<<"\n "<<i+1<<". "<<name[i]<< " " <<data[i];
}
//sort list
for (int i = 0; i< actualsize-1; i++) //loop cycle
for(int j = 0; j< actualsize-1; j++)
if(strcmp(name[j], name[j+1])>0)
{ swap(name[j], name[j+1]);
swap(data[j], data[j+1]);
}
// print out
cout<<"\n printed sorted list ";
for (int i=0;i<actualsize;i++)
{
cout<<"\n "<<i+1<<". "<<name[i]<< " " <<data[i];
}
char ch;
cout<<"\nenter any character to exit. ";
cin>>ch;
infile.close();
return 0;
}
void swap(char a1[], char a2[])
{
char a3[20];
strcpy(a3,a1);
strcpy(a1,a2);
strcpy(a2,a3);
return;
}
void swap (int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
return;
}
| |
Umm any text file with a number will work heres mine.
Michael Donegan 88
Denise Santoro 77
Ralph Knight 90
Amanda Clark 69
Luis Ramos 78
Let me know if this runs fine for you guys.. THAnks
Erm, on your swap function...what if a1 or a2 are larger than 20 characters?
Have you checked what the values for name[i] and data[i] on reading the file?
Topic archived. No new replies allowed.