Calculating grades and averages from text file

I am stuck... I am trying to figure out what I am missing in the code

Page 1 of 1

9 Replies - 7846 Views - Last Post: 09 December 2009 - 04:39 PM Rate Topic: -----

#1 bawl4lyfe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-October 09

Calculating grades and averages from text file

Posted 28 October 2009 - 10:59 AM

I am stuck... I am trying to figure out what I am missing in the code to get this program to read and calculate totals and averages of integers from my input text file. Its not even displaying the names from the input file.

#include<iostream>
#include<iomanip>
#include<fstream>

using namespace std;




int main(){
	
	string fname,lname;
	char grade='A';'B';'C';'D';'F';
	int totalpoints;
	int pscore1,pscore2,pscore3,pscore4,pscore5;
	int test1,test2;
	float paverage;
	float caverage;
	float taverage;
	
	

//File Input and Output	
	ifstream fin;
	ofstream fout; //step 2
	
	fin.open("input.txt");
	if(!fin){
			 cout<<"Input failure\n";
			 system ("Pause");
			 return 1;
			 }//end of error check
			 
	fout.open("output.txt");
	if(!fout){
			  cout<<"Output failure\n";
			  system("Pause");
			  return 1;
			  }//end of error check


system ("color f0");

//Header and Title
cout<<"12345678901234567890123456789012345678901234567890123456789"<<endl;
cout<<"***********************************************************"<<endl;
cout<<"*		 IT210 Business Applications with C++			*"<<endl;
cout<<"*		 Programmer:  Tevis Daniels					  *"<<endl;
cout<<"*		 Date:  October 22, 2009						 *"<<endl;
cout<<"*														 *"<<endl;
cout<<"*		 Program Assignment 2:  Student Grades II		*"<<endl;
cout<<"*														 *"<<endl;
cout<<"*		 This program reads student information from	 *"<<endl;
cout<<"*		 an input text file and outputs the results	  *"<<endl;
cout<<"*		 to the monitor as well as to the text file	  *"<<endl;
cout<<"*														 *"<<endl;
cout<<"***********************************************************"<<endl;

cout<<"Welcome to the IT210 Grade Calculator!"<<endl<<endl;
cout<<"Examine the output file now before you proceed!"<<endl<<endl;
cout<<"Press any key to read input and calculate the grades of all students"<<endl<<endl;

cout<<"1234567890123456789012345678901234567890123456789012345678901234567890"<<endl;
cout<<"======================================================================"<<endl;
cout<<setw(20)<<left<<"Student Name";

cout<<"----------------------------------------------------------------------"<<endl;


//Calculations
cout<<left<<fixed<<showpoint<<setprecision(2);
totalpoints= (pscore1+pscore2+pscore3+pscore4+pscore5+test1+test2);
paverage= (pscore1+pscore2+pscore3+pscore4+pscore5/5.0);
taverage=(test1+test2/2.0);
caverage=(pscore1+pscore2+pscore3+pscore4+pscore5+test1+test2/7.0);

//While 
while(fin){
fin>>fname>>lname>>totalpoints>>paverage>>taverage>>caverage>>grade;

cout<<setw(20)<<left<<fname+" "+lname;
cout<<setw(10)<<totalpoints<<setw(10)<<paverage<<setw(10)<<taverage<<setw(10)<<caverage<<setw(10)<<grade<<endl;
fout<<setw(20)<<left<<fname+" "+lname;
fout<<setw(10)<<totalpoints<<setw(10)<<paverage<<setw(10)<<taverage<<setw(10)<<caverage<<setw(10)<<grade<<endl;

//If Else Statement
if (caverage >=90)
 grade='A';
else
if(caverage >=80)
	grade='B';
	else
	if (caverage >=70)
		 grade='C';
		else 
		if (caverage >=60)
			grade='D';
			else
			if (caverage >=50)
				 grade='F';


	 
fin.close();
fout.close();
			  
if(fin.peek()=='\n')fin.ignore();
}//end of while





	  system("pause");
	  return 0;
}

Attached File(s)


This post has been edited by bawl4lyfe: 28 October 2009 - 11:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Calculating grades and averages from text file

#2 YamNad  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 120
  • Joined: 11-July 09

Re: Calculating grades and averages from text file

Posted 28 October 2009 - 11:00 AM

Can you edit the post and use code tags? I can't read that.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: Calculating grades and averages from text file

Posted 28 October 2009 - 11:27 AM

Well, you're trying to calculations before you've read any data into your variables for one thing. You have to read your data first, then do the calculations.
Was This Post Helpful? 0
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Calculating grades and averages from text file

Posted 28 October 2009 - 11:30 AM

Here is one error
char grade='A';'B';'C';'D';'F';

You have to remove the ;you only do commas , in between each variable and when your over then you use the semicolon like this
char grade='A';
remember that char is only for one variable asignment if you want to have more than one you would need, either
char gradeA='A';
char gradeB='B';
char gradeC='C';
char gradeD='D';
char gradeF='F';


or an array of grades
char grade[6]={'A','B','C','D','F'};

For what i see at the end of your program you assign values to your grade char after you compare grades so you could only say
char grade;



Hope that helps :P
Was This Post Helpful? 1
  • +
  • -

#5 bawl4lyfe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-October 09

Re: Calculating grades and averages from text file

Posted 28 October 2009 - 12:01 PM

One more thing, the input text file has a list of names and scores..

Snow White 77 87 76 82 92 96 90
Jack Frost 67 77 87 88 86 89 96
Sleeping Beauty 90 89 78 88 86 85 93
Prince Charming 92 87 88 89 90 91 78
Rapunzel Repunzel 66 76 73 81 85 70 86
Santa Claus 90 89 95 86 88 94 99
Miss Horner 65 76 72 50 69 75 92

The program isn't outputting all of the names... It is only outputting the first name which is "Snow White"... The rest of the names dont appear and neither do the calculated totals and averages...So my question is how do I get the other names to appear and how does the program know to calculate the above integers and display the totals, Im not sure how to get it to recognize that Snow White's first score which is "77" is also pcore1... its different then recognizing it from a cout statement and manually entering the scores and declaring 77 as pscore1, I dont understand how to do this while reading from an input text file...So lost lol HELP
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: Calculating grades and averages from text file

Posted 28 October 2009 - 12:13 PM

You're closing fin within the while loop...you want to do that *after* the while loop is done.
Was This Post Helpful? 0
  • +
  • -

#7 bawl4lyfe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-October 09

Re: Calculating grades and averages from text file

Posted 28 October 2009 - 03:04 PM

I am having trouble calculating averages and totals from my input text file...Instead of having the text file read and displayed as an output I need calculations to be done first before it outputs. Below is my input text file, I want added totals and averages for each name in the input text file. The thing I dont understand is how to get the program to recognize the numbers as different scores...the first 5 scores for snow white are test scores and the last two are program scores and I have to get a test average and a program average. I am attaching a screen shot of how the program is SUPPOSE to look so maybe that can help. Thanks in advance

Snow White 77 87 76 82 92 96 90
Jack Frost 67 77 87 88 86 89 96
Sleeping Beauty 90 89 78 88 86 85 93
Prince Charming 92 87 88 89 90 91 78
Rapunzel Repunzel 66 76 73 81 85 70 86
Santa Claus 90 89 95 86 88 94 99
Miss Horner 65 76 72 50 69 75 92

here is what i have so far..

//Preprocessor Directives
#include<iostream>
#include<iomanip>
#include<fstream>

using namespace std;


//Global Variables, Constants, and Function Prototypes

int main(){
	
	string fname,lname;
	char grade;
	int totalpoints;
	int pscore1,pscore2,pscore3,pscore4,pscore5;
	int test1,test2;
	float paverage,taverage,caverage;
	
	

//File Input and Output	
	ifstream fin;
	ofstream fout; 
	
	
	fin.open("input.txt");
	if(!fin){
			 cout<<"Input failure\n";
			 system ("Pause");
			 return 1;
			 }//end of error check
			 
	fout.open("output.txt");
	if(!fout){
			  cout<<"Output failure\n";
			  system("Pause");
			  return 1;
			  }//end of error check


system ("color f0");

//Header and Title
cout<<"12345678901234567890123456789012345678901234567890123456789"<<endl;
cout<<"***********************************************************"<<endl;
cout<<"*		 IT210 Business Applications with C++			*"<<endl;
cout<<"*		 Programmer:  Tevis Daniels					  *"<<endl;
cout<<"*		 Date:  October 22, 2009						 *"<<endl;
cout<<"*														 *"<<endl;
cout<<"*		 Program Assignment 2:  Student Grades II		*"<<endl;
cout<<"*														 *"<<endl;
cout<<"*		 This program reads student information from	 *"<<endl;
cout<<"*		 an input text file and outputs the results	  *"<<endl;
cout<<"*		 to the monitor as well as to the text file	  *"<<endl;
cout<<"*														 *"<<endl;
cout<<"***********************************************************"<<endl;

cout<<"Welcome to the IT210 Grade Calculator!"<<endl<<endl;
cout<<"Examine the output file now before you proceed!"<<endl<<endl;
cout<<"Press any key to read input and calculate the grades of all students"<<endl<<endl;



cout<<"1234567890123456789012345678901234567890123456789012345678901234567890"<<endl;
cout<<"======================================================================"<<endl;
cout<<setw(20)<<left<<"Student Name"<<setw(10)<<"Total Points"<<setw(10)<<"Program Average"<<setw(10)<<"Test Average"<<setw(10)<<"Course Average"<<setw(10)<<"Grade"<<endl;

cout<<"----------------------------------------------------------------------"<<endl;


//While 
while(fin){
fin>>fname>>lname;
cout<<setw(20)<<left<<fname+" "+lname;
cout<<setw(10)<<totalpoints<<setw(10)<<paverage<<(" ")<<setw(10)<<taverage<<(" ")<<setw(10)<<caverage<<(" ")<<setw(10)<<grade<<endl;
fout<<setw(20)<<left<<fname+" "+lname;
fout<<setw(10)<<totalpoints<<setw(10)<<paverage<<(" ")<<setw(10)<<taverage<<(" ")<<setw(10)<<caverage<<(" ")<<setw(10)<<grade<<endl;


}//end of while

//Calculations
cout<<left<<fixed<<showpoint<<setprecision(2);
totalpoints= (pscore1+pscore2+pscore3+pscore4+pscore5+test1+test2);
paverage= (pscore1+pscore2+pscore3+pscore4+pscore5/5.0);
taverage=(test1+test2/2.0);
caverage=(pscore1+pscore2+pscore3+pscore4+pscore5+test1+test2/7.0);

//If Else Statement
if (caverage >=90)
 grade='A';
else
if(caverage >=80)
	grade='B';
	else
	if (caverage >=70)
		 grade='C';
		else
		if (caverage >=60)
			grade='D';
			else
			if (caverage >=50)
				 grade='F';


fin.close();
fout.close();
			  
if(fin.peek()=='\n')fin.ignore();






	  system("pause");
	  return 0;
}

Attached File(s)


This post has been edited by bawl4lyfe: 28 October 2009 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: Calculating grades and averages from text file

Posted 28 October 2009 - 03:18 PM

You already have a thread open on this here; don't open new topics on the same subject when you have an already open one.

Merging topics.
Was This Post Helpful? 0
  • +
  • -

#9 learningtocode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-December 09

Re: Calculating grades and averages from text file

Posted 07 December 2009 - 06:14 PM

Hey ... we are in the same class. How can i get in contact with you ??
Was This Post Helpful? 0
  • +
  • -

#10 barnwillyb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Re: Calculating grades and averages from text file

Posted 09 December 2009 - 04:39 PM

View Postbawl4lyfe, on 28 Oct, 2009 - 02:04 PM, said:

I am having trouble calculating averages and totals from my input text file...Instead of having the text file read and displayed as an output I need calculations to be done first before it outputs. Below is my input text file, I want added totals and averages for each name in the input text file. The thing I dont understand is how to get the program to recognize the numbers as different scores...the first 5 scores for snow white are test scores and the last two are program scores and I have to get a test average and a program average. I am attaching a screen shot of how the program is SUPPOSE to look so maybe that can help. Thanks in advance

Snow White 77 87 76 82 92 96 90
Jack Frost 67 77 87 88 86 89 96
Sleeping Beauty 90 89 78 88 86 85 93
Prince Charming 92 87 88 89 90 91 78
Rapunzel Repunzel 66 76 73 81 85 70 86
Santa Claus 90 89 95 86 88 94 99
Miss Horner 65 76 72 50 69 75 92

here is what i have so far..

//Preprocessor Directives
#include<iostream>
#include<iomanip>
#include<fstream>

using namespace std;


//Global Variables, Constants, and Function Prototypes

int main(){
	
	string fname,lname;
	char grade;
	int totalpoints;
	int pscore1,pscore2,pscore3,pscore4,pscore5;
	int test1,test2;
	float paverage,taverage,caverage;
	
	

//File Input and Output	
	ifstream fin;
	ofstream fout; 
	
	
	fin.open("input.txt");
	if(!fin){
			 cout<<"Input failure\n";
			 system ("Pause");
			 return 1;
			 }//end of error check
			 
	fout.open("output.txt");
	if(!fout){
			  cout<<"Output failure\n";
			  system("Pause");
			  return 1;
			  }//end of error check


system ("color f0");

//Header and Title
cout<<"12345678901234567890123456789012345678901234567890123456789"<<endl;
cout<<"***********************************************************"<<endl;
cout<<"*		 IT210 Business Applications with C++			*"<<endl;
cout<<"*		 Programmer:  Tevis Daniels					  *"<<endl;
cout<<"*		 Date:  October 22, 2009						 *"<<endl;
cout<<"*														 *"<<endl;
cout<<"*		 Program Assignment 2:  Student Grades II		*"<<endl;
cout<<"*														 *"<<endl;
cout<<"*		 This program reads student information from	 *"<<endl;
cout<<"*		 an input text file and outputs the results	  *"<<endl;
cout<<"*		 to the monitor as well as to the text file	  *"<<endl;
cout<<"*														 *"<<endl;
cout<<"***********************************************************"<<endl;

cout<<"Welcome to the IT210 Grade Calculator!"<<endl<<endl;
cout<<"Examine the output file now before you proceed!"<<endl<<endl;
cout<<"Press any key to read input and calculate the grades of all students"<<endl<<endl;



cout<<"1234567890123456789012345678901234567890123456789012345678901234567890"<<endl;
cout<<"======================================================================"<<endl;
cout<<setw(20)<<left<<"Student Name"<<setw(10)<<"Total Points"<<setw(10)<<"Program Average"<<setw(10)<<"Test Average"<<setw(10)<<"Course Average"<<setw(10)<<"Grade"<<endl;

cout<<"----------------------------------------------------------------------"<<endl;


//While 
while(fin){
fin>>fname>>lname;
cout<<setw(20)<<left<<fname+" "+lname;
cout<<setw(10)<<totalpoints<<setw(10)<<paverage<<(" ")<<setw(10)<<taverage<<(" ")<<setw(10)<<caverage<<(" ")<<setw(10)<<grade<<endl;
fout<<setw(20)<<left<<fname+" "+lname;
fout<<setw(10)<<totalpoints<<setw(10)<<paverage<<(" ")<<setw(10)<<taverage<<(" ")<<setw(10)<<caverage<<(" ")<<setw(10)<<grade<<endl;


}//end of while

//Calculations
cout<<left<<fixed<<showpoint<<setprecision(2);
totalpoints= (pscore1+pscore2+pscore3+pscore4+pscore5+test1+test2);
paverage= (pscore1+pscore2+pscore3+pscore4+pscore5/5.0);
taverage=(test1+test2/2.0);
caverage=(pscore1+pscore2+pscore3+pscore4+pscore5+test1+test2/7.0);

//If Else Statement
if (caverage >=90)
 grade='A';
else
if(caverage >=80)
	grade='B';
	else
	if (caverage >=70)
		 grade='C';
		else
		if (caverage >=60)
			grade='D';
			else
			if (caverage >=50)
				 grade='F';


fin.close();
fout.close();
			  
if(fin.peek()=='\n')fin.ignore();






	  system("pause");
	  return 0;
}



You need to read each program score and each test score and then do your calculations:

//——————————————————————————————————————————————————————————
//						  includes
//——————————————————————————————————————————————————————————

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;



//——————————————————————————————————————————————————————————
//					   main listing
//——————————————————————————————————————————————————————————

int main(){
	
	string fname,lname;
	char grade;
	int total_points;
	int p_score_1, p_score_2, p_score_3, p_score_4, p_score_5;
	int test_1,test_2;
	float paverage, taverage, caverage;

	ifstream fin;
	ofstream fout; 

	fin.open("/Developer/Applications/Mac OS X Books/C++ Programming/Read Snow File/Student Scores.txt");
	if (!fin){
		cout << "Input failure\n";
		system ("Pause");
		return 1;
	}
	
	fout.open("/Developer/Applications/Mac OS X Books/C++ Programming/Read Snow File/output.txt");
	if (!fout){
		cout << "Output failure\n";

		return 1;
	}

	cout << setw(20) << "\n" << "Student Name" << setw( 19 ) << "Total Points" << setw( 20 ) << "Program Average" << setw( 17 ) << "Test Average" << setw( 19 ) << "Course Average" << setw( 10 ) << "Grade" << endl;

	cout << "-------------------------------------------------------------------------------------------------" << endl;

	// set precision to 2 decimals
	cout << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint );
	// read first and last name and the 5 program scores and 2 test scores
	while (fin >> fname >> lname >> p_score_1 >> p_score_2 >> p_score_3 >> p_score_4 >> p_score_5 >> test_1 >> test_2) {
		// calculations
		total_points = p_score_1 + p_score_2 + p_score_3 + p_score_4 + p_score_5 + test_1 + test_2;
		paverage = (p_score_1 + p_score_2 + p_score_3 + p_score_4 + p_score_5) / 5.0;
		taverage = (test_1 + test_2) / 2.0;
		caverage = (p_score_1 + p_score_2 + p_score_3 + p_score_4 + p_score_5 + test_1 + test_2) / 7.0;

		// grade equivalent
		if (caverage >= 90)
			grade = 'A';
		else if(caverage >= 80)
			grade = 'B';
		else if (caverage >= 70)
			grade = 'C';
		else if (caverage >= 60)
			grade = 'D';
		else if (caverage >= 50)
			grade = 'F';

		cout << setw( 23 ) << left << fname+" "+lname;
		cout << setw( 18 )
				 << total_points << setw( 17 )
				 << paverage << setw( 19 )
				 << taverage << setw( 17 )
				 << caverage << setw( 8 )
				 << grade << endl;

		fout << setw( 23 ) << left << fname+" "+lname;
		fout << setw( 18 )
				 << total_points << setw( 17 )
				 << paverage << setw( 19 )
				 << taverage << setw( 17 )
				 << caverage << setw( 8 )
				 << grade << endl;
	}
	cout << "-------------------------------------------------------------------------------------------------" << endl;

	fin.close();
	fout.close();

	if(fin.peek() == '\n')
		fin.ignore();

	return 0;
}


You pretty much had it all there, just not in the correct place.
Anyway, hope that helps!

This post has been edited by barnwillyb: 09 December 2009 - 08:28 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1