Reading user input to then output part of an array

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.

This is the txt file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1998	Tennessee Volunteers
1999	FSU Seminoles
2000	Oklahoma Sooners
2001	Miami Hurricanes
2002	OSU Buckeyes
2003	LSU Tigers
2004	USC Trojans
2005	Texas Longhorns
2006	Florida Gators
2007	LSU Tigers
2008	Florida Gators
2009	Alabama Crimson Tide
2010	Auburn Tigers
2011	Alabama Crimson Tide
2012	Alabama Crimson Tide
2013	FSU Seminoles
2014	OSU Buckeyes
2015	Alabama Crimson Tide
2016	Clemson Tigers
2017	Alabama Crimson Tide
2018	Clemson Tigers
2019	LSU Tigers


And this is what is displayed when I enter 2000:
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?

This is the program so far:
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
81
82
83
84
85
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

class Champs
{
private:
	ifstream inChamps;		//input file

	int ayear[22];			//arrays
	string aname[22];

	int choice;				//holds value for user input

	void openFile();
	void testFile();
	void readFile();
	void yearName();
	void closeFile();

public:
	void driver()
	{
		openFile();
		readFile();
		yearName();
		closeFile();
	}
};

void Champs::openFile()							//opens input file
{
	inChamps.open("NationalChampionship.txt");		
}

void Champs::readFile()
{
	if (inChamps.is_open())
	{
		cout << "National Championship Inquiry" << endl << endl;
		cout << "Reading the input file..." << endl << endl;
	}
	else
		testFile();
}

void Champs::testFile()
{
	cout << "Unable to open input file";
	exit(1);
}

void Champs::yearName()
{
	for (int i = 0; i < 22; i++)
	{
		int year;
		inChamps >> year;
		ayear[i] = year;

		string name;
		getline(inChamps, name);
		aname[i] = name;

		cout << "\nEnter the year: ";
		cin >> ayear[i];
		cout << "\n" << ayear[i] << " " << aname[i] << " won the national championship.";
	}
}


void Champs::closeFile()
{
	inChamps.close();
}

int main()
{
	Champs obj;
	obj.driver();
	system("pause");
	return 0;
}


This is the part of the program that I am having problems with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Champs::yearName()
{
	for (int i = 0; i < 22; i++)
	{
		int year;
		inChamps >> year;
		ayear[i] = year;

		string name;
		getline(inChamps, name);
		aname[i] = name;

		cout << "\nEnter the year: ";
		cin >> ayear[i];
		cout << "\n" << ayear[i] << " " << aname[i] << " won the national championship.";
	}
}



Last edited on
Also, it does not look very 'pretty' right now because I am trying to get the program to simply work before I actually make it look nice
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]).
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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).
Last edited on
I see, for whatever reason Visual Studios was not showing the warning but thank you.
Topic archived. No new replies allowed.