I am trying to read full names that are separated by commas from a file that I ask the user to type in. In the example I will submit it is about the first ten presidents of the US. Here is my code (I have all the necessary libraries, I am just showing the main function just so I can be told where I am mistaken):
int main()
{
const int NUMBER_OF_PRESIDENTS = 50;
int n = 0;
string president[NUMBER_OF_PRESIDENTS];
string fileName;
ifstream inputFile;
cout << "Enter name of input file ";
getline(cin, fileName);
inputFile.open(fileName.c_str());
if (inputFile.fail()) {
cout << "This file does not exist.";
}
if (inputFile >> president[n]) {
n++;
}
cout << n << " lines of text read from the input file.\n"
<< "Here are the unsorted names:\n"
<< "--------------------------- \n";
for (int i = 0; i < n; i++) {
cout << "[" << (i+1) << "] " << president[i] << endl;
}
return 0;
}
The user inputs a txt file names "firstTen.txt" and the following txt is shown:
Washington, George
Adams, John
Jefferson, Thomas
Madison, James
Monroe, James
Adams, John Quincy
Jackson, Andrew
Van Buren, Martin
Harrison, William Henry
Tyler, John
My problem is that I want it to read each line and then go to the next president's name. However when it shows, it only shows up like this: 
Aucun commentaire:
Enregistrer un commentaire