My advise:
Your program is too complicated for someone starting out, simplify.
The code above doesn't compile, you should strip out everything and start with
1 2 3 4 5
|
int main()
{
cout << "Hello World!" << endl;
return 0;
}
|
If you started with the above code, you'd know it's
int main()
not
int Main()
.
Code one step, then test, code another... make sure it does what you want, make sure you know what it does....
As I said, too complicated, forget about the output file until your able to read the input file. You can't do anything with output until you have input and sort it.
Someone who has to do a "bubble sort" shouldn't be having these problems opening input files.
write a program flowchart
always put
return 0;
at the end of main.
always close the files you open.
use comments
You have a good start, took me a few mins to find the errors, for the most part it looks like it should work. There is not just one way to do something. Below is what I found worked for me.
I stripped out everything but what you needed to read a file, since that is what your having trouble with. I think the next thing you will want to do is read it into an array, then sort it, then output the array to a file. Should be fun, enjoy!
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
|
int main()
{
string textFile;
ifstream iFile;
string Line;
cout << "Please enter the name of the file you wish to open:\n";
cin >> textFile;
iFile.open (textFile.c_str());
if (iFile.good())
{
while (iFile.good())
{
getline(iFile, Line);
// do something with input here
cout << Line << endl;
}
}
else // Let user know if can't open the input file.
{
cout << "Unable to open file." << endl;
}
iFile.close();
return 0;
}
|