Hello,

I am just starting out in a basic C++ class and am having some trouble with this problem for class. Any help would be seriously appreciated.

In the program, I have to use an input file that includes the name of a college, where the college is located, the enrollment total and the tuition cost. I am supposed to determine the schools that have the three highest tuitions, display their names and then display the cost of said schools.

I have been able to figure out the three highest costs, but I cannot figure out how to display the names. The class has not begun arrays, and we can only use while loops and if/else/else if statements. Any help would really be appreciated, I've been working at this for HOURS.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
	ifstream inputFile;
	string schoolName;
	string schoolLocation;
	int schoolEnrollment = 0;
	int schoolCost = 0;
	string mostExpensiveName;
	string midExpensiveName;
	string lowExpensiveName;
	double highestSchoolCost;
	double midSchoolCost;
	double lowestSchoolCost;
	double max = -100000000;
	double max1 = -10000000;
	double max2 = -10000000;

inputFile.open("OH-in.dat");
	if (!inputFile)
	{
		cout << "Error opening file\n.";
	}


	else 
	{
		while (inputFile >> schoolName && inputFile >> schoolLocation && inputFile >> schoolEnrollment && inputFile >> schoolCost && schoolEnrollment)
		{
			if (max < schoolCost)
			{
				max = schoolCost;
				highestSchoolCost = max;
			}
			
		}
	inputFile.close();

	inputFile.open("OH-in.dat");

		while (inputFile >> schoolName && inputFile >> schoolLocation && inputFile >> schoolEnrollment && inputFile >> schoolCost && schoolEnrollment)
		{
			if (max1 < schoolCost && schoolCost < highestSchoolCost)
			{
				max1 = schoolCost;
			}

		}
		midSchoolCost = max1;

	inputFile.open("OH-in.dat");
		while (inputFile >> schoolName && inputFile >> schoolLocation && inputFile >> schoolEnrollment && inputFile >> schoolCost && schoolEnrollment)
		{
			if (max2 < schoolCost && schoolCost < midSchoolCost)
			{
				max2 = schoolCost;
			}

		}

		lowestSchoolCost = max2;
				
		cout << highestSchoolCost;
		cout << endl;
		cout << midSchoolCost;
		cout << endl;
		cout << lowestSchoolCost;
		cout << endl;
	}

	inputFile.close();
	system("pause");
	return 0;
}

As you can see, I have not used the mostExpensiveName, etc. variables yet because I cannot figure out how to get them to show up. Every time the loop runs, it gives a different name off the input file until it reaches the very last name on the file and displays that, ie "Youngstown State University" based on the input data file provided below:

AntiochCollege
YellowSprings
330
27800
ArtAcademyofCincinnati
Cincinnati
163
19990
AshlandUniversity
Ashland
2793
22216
Baldwin-WallaceCollege
Berea
3625
22404
BlufftonUniversity
Bluffton
1030
21380
BowlingGreenStateUniversity
BowlingGreen
16085
15086
CapitalUniversity
Columbus
2824
26360
CaseWesternReserveUniversity
Cleveland
4080
32800
CedarvilleUniversity
Cedarville
3064
19680
CentralStateUniversity
Wilberforce
1747
11462
ClevelandStateUniversity
Cleveland
9878
10664
CollegeofMountSt.Joseph
Cincinnati
1916
19650
ColumbusCollegeofArt&Design
Columbus
1581
21768
DefianceCollege
Defiance
827
20120
DenisonUniversity
Granville
2263
32160
FranciscanUniversity
Steubenville
1982
17800
HeidelbergCollege
Tiffin
1330
18190
HiramCollege
Hiram
1205
24215
KentStateUniversity
Kent
18136
15862
KenyonCollege
Gambier
1661
37030
LakeErieCollege
Painesville
682
20500
LourdesCollege
Sylvania
1733
12570
MaloneCollege
Canton
1960
18600
MariettaCollege
Marietta
1412
24220
MiamiUniversity
Oxford
14551
22362
MountUnionCollege
Alliance
2193
21800
MountVernonNazarene
MountVernon
2171
17544
MuskingumCollege
NewConcord
1634
17500
NotreDameCollege
SouthEuclid
1240
20540
OberlinCollege
Oberlin
2829
36064
OhioUniversity
Athens
17176
17871
OhioWesleyanUniversity
Delaware
1935
31510
TheClevelandInstituteofArt
Cleveland
604
28100
TheCollegeofWooster
Wooster
1819
31870
TheOhioStateUniversity
Columbus
38479
21015
TheUniversityofAkron
Akron
18016
16467
TheUniversityofFindlay
Findlay
4926
22906
TheUniversityofToledo
Toledo
16067
15627
TiffinUniversity
Tiffin
1437
16800
UniversityofCincinnati
Cincinnati
19512
22419
UniversityofDayton
Dayton
7473
24880
UrsulineCollege
PepperPike
1180
20910
WalshUniversity
NorthCanton
2078
18300
WilmingtonCollege
Wilmington
1666
21578
WittenbergUniversity
Springfield
2059
31100
WrightStateUniversity
Dayton
12268
14004
XavierUniversity
Cincinnati
3910
24660
YoungstownStateUniversity
Youngstown
11987
12165
***
This data was collected from petersons.com
on October 10 and 11.  Some name and location
data has been altered.

Any help would REALLY be appreciated, as I am extremely lost. Not expecting someone to just give me the answer, I would really like to be taught! Thanks!

First thing I see is a very complex and IMO cumbersome while statements. A while statement is supposed to test something and either enter/continue or exit the loop. It's not meant to input a lot of values and AND the return values of the inputs. Take all those inputs and do them somewhere else and use the while to control the loop.

Actually, this is one of the things the OP, surprisingly, did right. I'm saying "surprisingly" because this is a very common mistake among beginners. Assuming...

string word;
ifstream file("in.txt"); // in.txt contains a list of words

...what most beginners do when reading that file is this:

while (!file.fail())
{
  file >> word;

  //do something with word
}

As a result, there are tons of "Help! Last line of the file processed twice!" threads all around the web. After they realize that they must check the stream status right after the input operation, they usually change their code to something like this...

while (true)
{
  file >> word;

  if (file.fail()) break;

  //do something with word
}

...which, of course, works, but is not as elegant as this one:

while (file >> word)
{
  //do something with word
}

True, the OP got carried away and added an unnecessary (but harmless here) "&& schoolEnrollment" part, but if you scratch that one, the rest is ok. It could be a bit more concise though:

while (inputFile >> schoolName >> schoolLocation >> schoolEnrollment >> schoolCost)
{
  // do stuff...
}

Now, about the actual problem. The solution is very simple. You just have to add an assignment statement inside each loop. When you change the max variable, also change the respective school name to the current school name.

Also, don't forget to close the file before opening it for the third time.

This article has been dead for over six months. Start a new discussion instead.