Click to See Complete Forum and Search --> : Help


May 3rd, 1999, 10:20 AM
When I ran the following code, my computer hang up. There was no compiling and linking error. What's wrong with it?

Thanks.

// My code
#include <fstream.h>
#include <iostream.h>
int main()
{
char inputfile[80];
char outputfile[80];
int igndigit;
char ch;
cout << "inputFile name: ";
cin >> inputfile;
cout<< "outputfile name: ";
cin >> outputfile;
cout << "ignore digits: ";
cin >> igndigit;
ofstream fout(outputfile); // open for writing
ifstream fin(inputfile); // open for reading
fin.ignore(igndigit,'\n');
while (fin.get(ch))
{
cout << ch; // output to screen
fout << ch; // write to outputfile
while ( ch == '\n' ) // if it is the end of a line, ignor the next igndigit chars
{
fin.ignore(igndigit,'\n');
}
}
fout.close(); // close outputfile
fin.close(); // close inputfile
return 0;
}

// Following is my input file.

1: /* qsort example */
2:
3: #include <iostream.h>
4: #include <stdlib.h>
5:
6: // form of sort_function required by qsort
7: int sortFunction( const void *intOne, const void *intTwo);
8:
9: const int TableSize = 10; // array size
10:
11: int main(void)
12: {
13: int i,table[TableSize];
14:
15: // fill the table with values
16: for (i = 0; i<TableSize; i++)
17: {
18: cout << "Enter a number: ";
19: cin >> table[i];
20: }
21: cout << "\n";
22:
23: // sort the values
24: qsort((void *)table, TableSize, sizeof(table[0]), sortFunction);
25:
26: // print the results
27: for (i = 0; i < TableSize; i++)
28: cout << "Table [" << i << "]: " << table[i] << endl;
29:
30: cout << "Done." << endl;
31: return 0;
32: }
33:
34: int sortFunction( const void *a, const void *b)
35: {
36: int intOne = *((int*)a);
37: int intTwo = *((int*)b);
38: if (intOne < intTwo)
39: return -1;
40: if (intOne == intTwo)
41: return 0;
42: return 1;
43: }

// The last is my expected result.

/* qsort example */
#include <iostream.h>
#include <stdlib.h>
// form of sort_function required by qsort
int sortFunction( const void *intOne, const void *intTwo);
const int TableSize = 10; // array size

int main(void)
{
int i,table[TableSize];
// fill the table with values
for (i = 0; i<TableSize; i++)
{
cout << "Enter a number: ";
cin >> table[i];
}
cout << "\n";
// sort the values
qsort((void *)table, TableSize, sizeof(table[0]), sortFunction);

// print the results
for (i = 0; i < TableSize; i++)
cout << "Table [" << i << "]: " << table[i] << endl;

cout << "Done." << endl;
return 0;
}

int sortFunction( const void *a, const void *b)
{
int intOne = *((int*)a);
int intTwo = *((int*)b);
if (intOne > intTwo)
return -1;
if (intOne == intTwo)
return 0;
return 1;
}