I am trying to read in the user's input for the year so I can output who won the national championship for that year by using two parallel arrays but I am running into a bit of a problem where if I put in a year- let's say 2000- it will not display the info for 2000, but instead the first line of the txt file.
National Championship Inquiry
Reading the input file...
Enter the year: 2000
2000 Tennessee Volunteers won the national championship.
Enter the year:
I am also running into a problem where I have to enter the year ~20 times because of the "for loop", but isn't there a way for me to allow the user to input, lets say -99, and it would end the loop?
Load the file into an array completely first. Don't worry about user input yet.
Once you have the file loaded into an array, then ask for input (cin), and iterate over the array and check if (user_input_year == ayear[i]).
void Champs::yearName()
{
for (int i = 0; i < 22; i++)
{
int year;
inChamps >> year;
ayear[i] = year;
string name;
getline(inChamps >> ws, name);
aname[i] = name;
}
cout << "\nEnter the year: ";
cin >> userInputYear;
for (int i = 0; i < 22; i++)
{
if (userInputYear == ayear[i]);
{
cout << "\n" << userInputYear << " " << aname[i] << " won the national championship.";
}
}
}
This is what I came up with, but now the display looks crazy and it is still not what I am trying to accomplish.
This is what I am getting when I run the program and enter 2002.
National Championship Inquiry
Reading the input file...
Enter the year: 2002
2002 Tennessee Volunteers won the national championship.
2002 FSU Seminoles won the national championship.
2002 Oklahoma Sooners won the national championship.
2002 Miami Hurricanes won the national championship.
2002 OSU Buckeyes won the national championship.
2002 LSU Tigers won the national championship.
2002 USC Trojans won the national championship.
2002 Texas Longhorns won the national championship.
2002 Florida Gators won the national championship.
2002 LSU Tigers won the national championship.
2002 Florida Gators won the national championship.
2002 Alabama Crimson Tide won the national championship.
2002 Auburn Tigers won the national championship.
2002 Alabama Crimson Tide won the national championship.
2002 Alabama Crimson Tide won the national championship.
2002 FSU Seminoles won the national championship.
2002 OSU Buckeyes won the national championship.
2002 Alabama Crimson Tide won the national championship.
2002 Clemson Tigers won the national championship.
2002 Alabama Crimson Tide won the national championship.
2002 Clemson Tigers won the national championship.
2002 LSU Tigers won the national championship.
Line 19: Remove the semi-colon after the if statement.
Please note, enabling compiler warnings can detect potential errors like these.
main.cpp:18:5: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
main.cpp:19:5: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’
-Wall in GCC/clang, similar options in project settings in Visual Studio (/W3 for example).