I'm working on my take home final for my Intro to Programming class and need to add the ability to search my class.txt file for a specific record by inputting the student ID. I can't for the life of me figure out what needs to be done.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
char eol = '\n';

double avgrd(int, int, int, int, int, double&, string&);
double num(int, int, int, int, int);

void main()

{	
	ifstream inF; 

	int cnt=0,cntf=0, cnt1=0, cnt2=0, cnt3=0, cnt4=0, fcnt=0, sum=0, sID, quiz1, quiz2, quiz3, quiz4, final;
	double total1=0, total2=0, total3=0, total4=0, finalTotal=0, totalAvgs=0, sID2;
	double avg, totalAvg1=0, totalAvg2=0,  totalAvg3=0,  totalAvg4=0, finalAvgtotal=0, ftotalAvgs=0;  
	char num;
	string fname, LastN, FirstN, grade = " ";

	cout << fixed << setprecision(1);
	cout <<"Enter file Name: "; 
	cin >> fname;
	fname += ".txt"; 
	cout << endl;
	inF.open(fname.c_str());

	if(!inF.fail())
	{
		cout << setw(18) << ' ' << "Fall 2011 Student Grade report:" << endl;
		cout << endl << "No.   ID    Last Name     First Name   Quiz1 Quiz2 Quiz3 Quiz4 Final  Avg  Grd";
		cout << endl << "------------------------------------------------------------------------------";
			    
		while (!inF.eof())
		{ 
			inF >> sID >> LastN >> FirstN;
			inF >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> final;
			cnt++; total1+=quiz1; total2+=quiz2; total3+=quiz3; total4+=quiz4; finalTotal+= final; 
			inF.get(num);
			if (quiz1 !=0)
				cnt1++;
			if (quiz2 !=0)
				cnt2++;
			if (quiz3 !=0)
				cnt3++;
			if (quiz4 !=0)
				cnt4++;
			if (final !=0)
				fcnt++;
				avgrd(quiz1, quiz2, quiz3, quiz4, final, avg, grade);
			
			if (avg >= 96.67)
				grade = "A+";
			else if (avg >= 93.33)
				grade = "A ";
			else if (avg >= 90)
				grade = "A-";
			else if (avg >= 86.67)
				grade = "B+";
			else if (avg >= 83.33)
				grade = "B ";
			else if (avg >= 80)
				grade = "B-";
			else if (avg >= 76.67)
				grade = "C+";
			else if (avg >= 73.33)
				grade = "C ";
			else if (avg >= 70)
				grade = "C-";
			else if (avg >= 67.67)
				grade = "D+";
			else if (avg >= 63.33)
				grade = "D ";
			else if (avg >= 60)
				grade = "D-";
			else if (avg < 60)
				grade = "F ";
			
			if (quiz1 == 0 && quiz2 == 0 && quiz3 == 0 && quiz4 == 0)
			{
				cout << setw(3) << cnt << setw(7) << sID << ' ' << ' ';
				cout << left << setw(14) << LastN << setw(12) << FirstN << right << setw(4) 
				<< " " << setw(6) << " " << setw(6) << " " << setw(6) << " " 
				<< setw(6) << final << setw(8) << avg << setw(4) << grade << endl;
			totalAvgs+=avg;
			}
			else if (quiz1 > 0 && quiz2 > 0 && quiz3 == 0 && quiz4 > 0)
			{
			cout << setw(3) << cnt << setw(7) << sID << ' ' << ' ';
			cout << left << setw(14) << LastN << setw(12) << FirstN << right << setw(4) 
				<< quiz1 << setw(6) << quiz2 << setw(6) << " " << setw(6) << quiz4 
				<< setw(6) << final << setw(8) << avg << setw(4) << grade << endl;
			totalAvgs+=avg;	
			}
			else if (quiz1 == 0 && quiz2 > 0 && quiz3 == 0 && quiz4 > 0)
			{
			cout << setw(3) << cnt << setw(7) << sID << ' ' << ' ';
			cout << left << setw(14) << LastN << setw(12) << FirstN << right << setw(4) 
				<< " " << setw(6) << quiz2 << setw(6) << " " << setw(6) << quiz4 
				<< setw(6) << final << setw(8) << avg << setw(4) << grade << endl;
			totalAvgs+=avg;	
			}
			else if (quiz1 > 0 && quiz2 == 0 && quiz3 > 0 && quiz4 > 0)
			{
			cout << setw(3) << cnt << setw(7) << sID << ' ' << ' ';
			cout << left << setw(14) << LastN << setw(12) << FirstN << right << setw(4) 
				<< quiz1 << setw(6) << " " << setw(6) << quiz3 << setw(6) << quiz4 
				<< setw(6) << final << setw(8) << avg << setw(4) << grade << endl;
			totalAvgs+=avg;	
			}
			else if (quiz1 > 0 && quiz2 > 0 && quiz3 > 0 && quiz4 > 0)
			{
			cout << setw(3) << cnt << setw(7) << sID << ' ' << ' ';
			cout << left << setw(14) << LastN << setw(12) << FirstN << right << setw(4) 
				<< quiz1 << setw(6) << quiz2 << setw(6) << quiz3 << setw(6) << quiz4 
				<< setw(6) << final << setw(8) << avg << setw(4) << grade << endl;
			totalAvgs+=avg;	

			}
		}
	}

	

	totalAvg1 = total1/cnt1; totalAvg2 = total2/cnt2; totalAvg3 = total3/cnt3; totalAvg4 = total4/cnt4; 
	finalAvgtotal = finalTotal/fcnt; ftotalAvgs = totalAvgs/fcnt;
	cout << setw(38) << ' ' << "-------------------------------------" << endl;
	cout << setw(40) << ' ' << totalAvg1 << setw(6) << totalAvg2 << setw(6) << totalAvg3 << setw(6) 
		<< totalAvg4 << setw(6) << finalAvgtotal << setw(6) << ftotalAvgs << endl;	

	cout << endl << endl;
	cout << "          Student Information System:" << endl << endl;
	cout << "Enter Student ID (999 to end): "; cin >> sID;


	inF.close();
}
double avgrd(int a, int b, int c, int d, int e, double& avg, string& grade)
{
	double sum=0, min=0, max=0;
	if(a > b)
		max = a;
	else if (b > a)
		max = b;
	if (c > max)
		max=c;
	if (d > max)
		max = d;
	if (e > max)
		max = e;
	if (a < b) 
		min = a; 
	else if (b < a)
		min = b;
	if (c < min)
		min = c;
	if (d < min)
		min = d;
	if (e < min)
		min = e;
	sum = max + (a + b + c + d + e) - min;
	avg = double (sum) / 6.;
	return avg;	
}

Here is a sample from the class file.

32998	Benway		Eileen		76	98	80	75	95
15246	Berling		Danielle	85	64	0	69	85
94304	Bowdy		James		0	57	70	92	98
50838	Burr		Jermiah		83	84	94	77	63
84511	Calluari	Henry		72	84	97	73	74
77921	Connors		Sarah		52	58	88	61	78
24609	Cooper		Camille		62	93	92	58	66
28248	Courville	David		81	88	89	64	87
61084	Cruz		Waldemar	70	68	76	100	86
10364	Denaro		Tony		73	85	55	68	89
17600	DeNinno		David		62	68	93	84	75

If anyone could help me I'd greatly appreciate it.

So, what is supposed to be the output? -- just one of these records?

Can't believe I forgot that. This things frying my brain... The output's goal should resemble this

Enter Student ID (999 to end): 61084
   Cruz          Waldemar    86.3 B

Search for record by student ID:

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

int main()
{
   ifstream inFile;
   string result = "";
   string id = "10364";
   string line;
   inFile.open("records.txt",ios::binary);
   while( getline(inFile,line) )
   {
      stringstream ss(line);
      if( ss >> result )
      {
         if(result == id)
         {
            //parse the rest of the record.
         }
      }
   }
   
}

Happy Holidays, and beware of potential bugs, this isn't your entire assignment, just a sample of something that will hopefully work.

http://fredosaurus.com/notes-cpp/strings/stringstream-example.html
http://www.cplusplus.com/reference/iostream/stringstream/stringstream/


As for your existing code, every time I see while (!inF.eof()) it means that you may not be aware of it reading one past the end of the file, basically it reads the EOF stuff because .eof() only returns true after you hit the end of the line.

I've always followed this example and it has served me well:

string word;
string number;
ifstream inFile("somestuff.txt",ios::binary);
while( inFile >> word >> number )
{
   cout << word << " " << number << endl;
}
if(inFile.bad())//if badbit
{
   cerr << "Bad! Input bad!" << endl;//lol
}

or

string line;
string word;
string number;
ifstream inFile("somestuff.txt",ios::binary);
while(getline(inFile, line))
{
   stringstream ss(line);//stringstream from line.
   if( ss >> word >> number )
      cout << "OK.\n";
   else
      cerr << "Bad!\n";
}

I suggest loading the data before doing any calculations.
Only do calculations when they are called-for.

That way, you can ensure your code is clean at each step and you can change pieces of it without having to disturb all of the code at once.

Are you expected (or allowed to) use C++ "classes"?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.