I need to read in numbers from a .txt file and sort the numbers to find the 3 highest and i can not figure out how to accomplish this. I'm so lost i dont even know where to start. :(

How about start with reading the file?
Are you allowed to use vectors?

When you post some code (using code-tags), we'll help more. But no effort == no help.

I have read in the file, but i dont know how to sort the numbers i have read in here is what i have got so far....

outfile.open("SmithM_rpt5.txt");
	infile.open("pgm5.txt");

	if(!infile)
	{
		cout << "Error file could not be found" << endl;
		return 1;
	}

	while(infile >> schoolname)
	{
		infile >> location;
		infile >> enrollment;
		infile >> tuition;

		if(strcmp(location,"Cincinnati")==0)
		{
			if(tuition < 20000)
			{
				outfile << "School Name: " << schoolname << endl;
				outfile << "Located in: " << location << endl;
				outfile << "Enrollment: " << enrollment << endl;
				outfile << "Tuition: " << tuition << endl;

				count++;
			}
		}
		if(enrollment > 10000)
		{
			totaltuition=totaltuition+tuition;
			counttwo++;

		}
              outfile << "There are " << count << " schools in Cincinnati with a tuition under 
              20,000." << endl;

	infile.close();

Edited 7 Years Ago by Nakeo: n/a

idk what happened with that last post i copied and pasted wrong i guess but here is all of it

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

int main()
{
	ifstream infile;
	ofstream outfile;

	double avg, enrollment, tuition, count=0, totaltuition=0, counttwo=0, count3,lasttuition=0;
	string schoolname;
	char location[65];

	outfile.open("SmithM_rpt5.txt");
	infile.open("pgm5.txt");

	if(!infile)
	{
		cout << "Error file could not be found" << endl;
		return 1;
	}

	while(infile >> schoolname)
	{
		infile >> location;
		infile >> enrollment;
		infile >> tuition;

		if(strcmp(location,"Cincinnati")==0)
		{
			if(tuition < 20000)
			{
				outfile << "School Name: " << schoolname << endl;
				outfile << "Located in: " << location << endl;
				outfile << "Enrollment: " << enrollment << endl;
				outfile << "Tuition: " << tuition << endl;

				count++;
			}
		}
		if(enrollment > 10000)
		{
			totaltuition=totaltuition+tuition;
			counttwo++;
			cout << "School Name: " << schoolname << endl;
			cout << "tuition: " << tuition << endl;

		}


	}
	infile.close();

	outfile << "There are " << count << " schools in Cincinnati with a tuition under 20,000." << endl;
	outfile << " " << endl;
	outfile << " " << endl;
	outfile << "The average tuition for schools above 10,000 enrollment is " << totaltuition/counttwo << endl;
	outfile.close();


	return 0;

}

Where's the rest of the code?

[edit]
Also post a few lines from your textfile.

Edited 7 Years Ago by Nick Evan: n/a

i posted everything i wrote already(screwed up the first time i posted it), but this is where im stuck idk how to sort the tuition costs i read in from the file.

I solved my problem, i ended up just finding the highest tuition cost first then closing the file and reopening it and running through the data again looking for the next highest etc... here is my code if you are interested. Also any tips for making this easier or better would be greatly appreciated.

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

int main()
{
	ifstream infile;
	ofstream outfile;

	double enrollment, tuition, count=0, totaltuition=0, counttwo=0,lasttuition=0, tuitionone,tuitionthree;
	string schoolname, schoolone,schooltwo,schoolthree,locationone,locationtwo,locationthree;
	char location[65];

	outfile.open("SmithM_rpt5.txt");
	infile.open("pgm5.txt");

	if(!infile)
	{
		cout << "Error file could not be found" << endl;
		return 1;
	}

	while(infile >> schoolname)
	{
		infile >> location;
		infile >> enrollment;
		infile >> tuition;

		if(strcmp(location,"Cincinnati")==0)
		{
			if(tuition < 20000)
			{
				outfile << "School Name: " << schoolname << endl;
				outfile << "Located in: " << location << endl;
				outfile << "Enrollment: " << enrollment << endl;
				outfile << "Tuition: " << tuition << endl;

				count++;
			}
		}
		if(enrollment > 10000)
		{
			totaltuition=totaltuition+tuition;
			counttwo++;
			cout << "School Name: " << schoolname << endl;
			cout << "tuition: " << tuition << endl;

		}

		if(tuition > lasttuition)
		{
			lasttuition = tuition;
			schoolone = schoolname;
		}



	}
	infile.close();
	infile.clear();
	infile.open("pgm5.txt");
	if(!infile)
	{
		cout << "Error file could not be found" << endl;
		return 1;
	}

	while(infile >> schoolname)
	{
		infile >> location;
		infile >> enrollment;
		infile >> tuition;

		if(tuition >= tuitionone && tuition < lasttuition)
		{
			tuitionone = tuition;
			schooltwo = schoolname;
		}
	}
	infile.close();
	infile.clear();
	infile.open("pgm5.txt");
	if(!infile)
	{
		cout << "Error file could not be found" << endl;
		return 1;
	}

	while(infile >> schoolname)
	{
		infile >> location;
		infile >> enrollment;
		infile >> tuition;

		if(tuition > tuitionthree && tuition < tuitionone)
		{
			tuitionthree = tuition;
			schoolthree = schoolname;
		}
	}
	infile.close();

	outfile << "There are " << count << " schools in Cincinnati with a tuition under 20,000." << endl;
	outfile << " " << endl;
	outfile << " " << endl;
	outfile << "The average tuition for schools above 10,000 enrollment is " << totaltuition/counttwo << endl;
	outfile << "The School with the highest tuition is "<< schoolone <<" with a tuition of "<< lasttuition << endl;
	outfile << "The School with the second highest tuition is "<< schooltwo <<" with a tuition of "<< tuitionone   << endl;
	outfile << "The School with the third highest tuition is "<< schoolthree <<" with a tuition of "<< tuitionthree <<endl;

	outfile.close();


	return 0;

}

some lines from the txt file:
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

Edited 7 Years Ago by Nakeo: n/a

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