6 Replies - 1061 Views - Last Post: 03 August 2007 - 06:25 AM Rate Topic: ***** 1 Votes

#1 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Problems with structures and inFile getting data

Posted 28 July 2007 - 02:24 PM

 #include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
	int stunum;//# of students in the class
	string stuname;// Name of the student
	float Test1;//score for test 1
	float Test2;//score for test 2
	float Midterm;//score for midterm
	float Final;//score for final
	float Term;//score for term project
	float Grade;
	float NumA=0;
	float NumB=0;
	float NumC=0;
	float NumF=0;

	ifstream inFile;
	ofstream outFile;

	inFile.open ("ClassData.dat");
	outFile.open ("ClassOutput");
	inFile>>stunum;//reads in the # of students in the class
	outFile<<"The # of students in the class:"<<stunum<<endl;
	for (int j=0; j<stunum; j++);
	{
	while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term);
	}
	outFile<<setw(10)<<"Name"<<setw(10)<<"Test1"<<setw(10)<<"Test2"<<setw(10)<<"Midterm"<<setw(10)<<"Final"<<setw(12)<<"Project"<<setw(14)<<"Total Grade"<<setw(16)<<"Letter Grade"<<endl;
	Grade= Test1+Test2+Midterm+Final+Term;
	
	{
	
		{
			outFile<<setw(10)<<stuname<<setw(10)<<Test1<<setw(10)<<Test2<<setw(10)<<Midterm<<setw(10)<<Final<<setw(10)<<Term<<setw(10)<<Grade<<endl;
	
			{	
				float Grade= Test1+Test2+Midterm+Final+Term;
				if(Grade>=90)
				{
				outFile<<"A ";
				NumA++;
				}
				else if (Grade>=80)
				{
				outFile<<"B ";
				NumB++;
				}
				else if (Grade>=70)
				{
				outFile<<"C ";
				NumC++;
				}
				else if (Grade<70)
				{
				outFile<<"F ";
				NumF++;
				outFile<<endl;
				}
				inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;
				
			}

			outFile<<"The # of A's:"<<NumA<<endl;
			outFile<<"The # of B's:"<<NumB<<endl;
			outFile<<"The # of C's:"<<NumC<<endl;
			outFile<<"The # of F's:"<<NumF<<endl;
		}
		
	}


inFile.close();
outFile.close();

	return 0;
}



Thanks for the assistance:

If I were to do the following, why does it only show the information for Kenez from my inFile?

10
Gayle 75 82 62 61 59
Brown 79 100 17 19 89
Byles 100 94 81 79 81
Watts 71 22 48 100 17
Walsh 41 85 71 88 81
Ahmed 93 47 76 64 94
Ellis 87 99 100 70 83
Silny 80 76 85 78 81
Simms 88 93 42 84 65
Kenez 75 48 77 69 82
-------------------------------------------------------------------------------

Is This A Good Question/Topic? 0
  • +

Replies To: Problems with structures and inFile getting data

#2 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Problems with structures and inFile getting data

Posted 30 July 2007 - 07:20 AM

What is your input and what SHOULD you get for a result?
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Problems with structures and inFile getting data

Posted 30 July 2007 - 08:48 AM

	for (int j=0; j<stunum; j++);
	{
	while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term);
	}


you have a while statement inside your for loop that executes without doing anything...right down to the last line of the file before proceeding.

you also have several opening and closing braces with no discernible purpose.
Was This Post Helpful? 0
  • +
  • -

#4 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: Problems with structures and inFile getting data

Posted 02 August 2007 - 11:04 AM

Below is my new code:

#include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
	int stunum;//# of students in the class
	string stuname;// Name of the student
	float Test1;//score for test 1
	float Test2;//score for test 2
	float Midterm;//score for midterm
	float Final;//score for final
	float Term;//score for term project
	float Grade;
	float NumA=0;
	float NumB=0;
	float NumC=0;
	float NumF=0;

	ifstream inFile;
	ofstream outFile;

	inFile.open ("ClassData.dat");
	outFile.open ("ClassOutput");
	inFile>>stunum;//reads in the # of students in the class
	outFile<<"The # of students in the class:"<<stunum<<endl;
	for (int j=0; j<stunum; j++)
	inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;

	Grade= Test1+Test2+Midterm+Final+Term;
	Grade=Grade/5;
	inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;
	{
	
		{	outFile<<setw(10)<<"Name"<<setw(10)<<"Test1"<<setw(10)<<"Test2"<<setw(10)<<"Midterm"<<setw(10)<<"Final"<<setw(12)<<"Project"<<setw(14)<<"Total Grade"<<setw(14)<<"Letter Grade"<<endl;
			outFile<<setw(10)<<stuname<<setw(10)<<Test1<<setw(10)<<Test2<<setw(10)<<Midterm<<setw(10)<<Final<<setw(10)<<Term<<setw(13)<<Grade<<endl;
	
			{	
				float Grade= Test1+Test2+Midterm+Final+Term;
				Grade=Grade/5;

				if(Grade>=90)
				{
				outFile<<"A ";
				NumA++;
				}
				else if (Grade>=80)
				{
				outFile<<"B ";
				NumB++;
				}
				else if (Grade>=70)
				{
				outFile<<"C ";
				NumC++;
				}
				else if (Grade<70)
				{
				outFile<<"F ";
				NumF++;
				}				
				inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;
				
			}

			outFile<<"The # of A's:"<<NumA<<endl;
			outFile<<"The # of B's:"<<NumB<<endl;
			outFile<<"The # of C's:"<<NumC<<endl;
			outFile<<"The # of F's:"<<NumF<<endl;
		}
		
	}


inFile.close();
outFile.close();

	return 0;
}



Two problems: Why does it only print the profile for "Kenez" from my inFile? Secondly, in my outFile, it only print's out data for "Kenez" and it places the "Letter Grade", for Kenez in a separate line? Below are my inFile & outFile and also the aim of this program:
ASSIGNMENT #1

Accounting teachers, being naturally inept with numbers, have a difficult time calculating students’ averages and assigning letter grades at the end of each semester. Your accounting teacher is no exception. To make this end of semester grading easier, and more accurate, he hires you to write a program (C++) to automate the task. There are a variable number of students in his class, with each students grade being calculated as follows:



2 Tests 10% each

Midterm Exam 25%

Final Exam 30%

Term Project 25%



Input data is stored in a file named ClassData.dat. The first line in the input file has the number of students in the class. Each subsequent line contains one student’s Lastname, test-scores, exam scores and project score. Your program should begin by reading the number of students in the class, and then for each student, read the student’s name and test, exam and project scores. The program should then calculate the student’s average and assign him/her a letter grade (“A” for 90-100; “B” for 80-89; “C” for 70-79; and “F” for 69 and below). Finally, for each student, the program should print, to output file, his/her name, average, and grade.



Once all students have been processed, the program should print a summary report stating the number of “A”s, “B”’s, “C”’s and “F”’s assigned.

----------------------------------------------------------------------------------

I've created a Text file using notepad with the following data:

10
Gayle 75 82 62 61 59
Brown 79 100 17 19 89
Byles 100 94 81 79 81
Watts 71 22 48 100 17
Walsh 41 85 71 88 81
Ahmed 93 47 76 64 94
Ellis 87 99 100 70 83
Silny 80 76 85 78 81
Simms 88 93 42 84 65
Kenez 75 48 77 69 82

My outFile shows like tgis however:
The # of students in the class:10
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Kenez 75 48 77 69 82 70.2
C The # of A's:0
The # of B's:0
The # of C's:1
The # of F's:0

This post has been edited by zandiago: 02 August 2007 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Problems with structures and inFile getting data

Posted 02 August 2007 - 12:35 PM

#include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
#include<cmath>

using namespace std;

void main()
{
	int	nNumStudents;	//# of students in the class
	string strStudentName;  // Name of the student
	float  fTest1;		  //score for test 1
	float  fTest2;		  //score for test 2
	float  fMidterm;		//score for midterm
	float  fFinal;		  //score for final
	float  fProject;		//score for project
	float  fGrade;		  //overall grade for term
	int	nNumA=0;		 //number of As
	int	nNumB=0;			//number of Bs
	int	nNumC=0;			//number of Cs
	int	nNumF=0;			//number of Fs

	ifstream inFile;		//input file object
	ofstream outFile;	   //output file object

	inFile.open ("ClassData.txt");
	outFile.open ("ClassOutput.txt");

	inFile >> nNumStudents;//reads in the # of students in the class

	outFile << "The # of students in the class:" << nNumStudents << endl;

	for (int nIndex = 0; nIndex < nNumStudents; nIndex++)
	{
		inFile >> strStudentName >> fTest1 >> fTest2 >> fMidterm >> fFinal >> fProject;

		fGrade = (fTest1 + fTest2 + fMidterm + fFinal + fProject);
		fGrade = (fGrade / 5);

		outFile << setw(10) << "Name" << setw(10) << "Test1" << setw(10) << "Test2" << setw(10) << "Midterm" << setw(10) << "Final" << setw(12) << "Project" << setw(14) << "Total Grade" << setw(14) << "Letter Grade" << endl;
		outFile << setw(10) << strStudentName << setw(10) << fTest1 << setw(10) << fTest2 << setw(10) << fMidterm << setw(10) << fFinal << setw(12) << fProject << setw(14) << fGrade;
		
		if(fGrade>=90)
		{
			outFile << setw(14) << "A" << endl;
			nNumA++;
		}
		else if (fGrade>=80)
		{
			outFile << setw(14) << "B" << endl;
			nNumB++;
		}
		else if (fGrade>=70)
		{
			outFile << setw(14) << "C" << endl;
			nNumC++;
		}
		else if (fGrade<70)
		{
			outFile << setw(14) << "F" << endl;
			nNumF++;
		}				
	}//END for
	
	outFile << "The # of A's:" << nNumA << endl;
	outFile << "The # of B's:" << nNumB << endl;
	outFile << "The # of C's:" << nNumC << endl;
	outFile << "The # of F's:" << nNumF << endl;

	inFile.close();
	outFile.close();
}//END main



- Just some logic errors really.. i'd say about 95% correct.. nicely done :)

1. You were not reading in a project grade. I fixed this so now it reads in the project grade.

2. The reason it was only reading in that name is you kept getting information until the # of students had been reached so you were only getting information for the last guy rather than each individual record.

This code works... I changed the input/output files to ".txt" so you will need to change those back to ".dat". I only did this to make sure the input/output was correct.

Input:
10
Gayle 75 82 62 61 59
Brown 79 100 17 19 89
Byles 100 94 81 79 81
Watts 71 22 48 100 17
Walsh 41 85 71 88 81
Ahmed 93 47 76 64 94
Ellis 87 99 100 70 83
Silny 80 76 85 78 81
Simms 88 93 42 84 65
Kenez 75 48 77 69 82

Output: (probably not going to show up formatted) :(
The # of students in the class:10
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Gayle 75 82 62 61 59 67.8 F
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Brown 79 100 17 19 89 60.8 F
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Byles 100 94 81 79 81 87 B
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Watts 71 22 48 100 17 51.6 F
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Walsh 41 85 71 88 81 73.2 C
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Ahmed 93 47 76 64 94 74.8 C
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Ellis 87 99 100 70 83 87.8 B
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Silny 80 76 85 78 81 80 B
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Simms 88 93 42 84 65 74.4 C
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Kenez 75 48 77 69 82 70.2 C
The # of A's:0
The # of B's:3
The # of C's:4
The # of F's:3

This post has been edited by Topher84: 02 August 2007 - 12:39 PM

Was This Post Helpful? 0
  • +
  • -

#6 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: Problems with structures and inFile getting data

Posted 02 August 2007 - 01:16 PM

I see, that sure does work...but what if you don't want the headers to be printed all the time. I wanted my outFile to look like the following exept with all the students:
The # of students in the class:10
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
***********************************************************
Kenez 75 48 77 69 82 72.75 C
The # of A's: 0
The # of B's: 0
The # of C's: 1
The # of F's: 0
Was This Post Helpful? 0
  • +
  • -

#7 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Problems with structures and inFile getting data

Posted 03 August 2007 - 06:25 AM

#include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
#include<cmath>

using namespace std;

void main()
{
	int	nNumStudents;	//# of students in the class
	string strStudentName;  // Name of the student
	float  fTest1;		  //score for test 1
	float  fTest2;		  //score for test 2
	float  fMidterm;		//score for midterm
	float  fFinal;		  //score for final
	float  fProject;		//score for project
	float  fGrade;		  //overall grade for term
	int	nNumA=0;		 //number of As
	int	nNumB=0;			//number of Bs
	int	nNumC=0;			//number of Cs
	int	nNumF=0;			//number of Fs

	ifstream inFile;		//input file object
	ofstream outFile;	   //output file object

	inFile.open ("ClassData.txt");
	outFile.open ("ClassOutput.txt");

	inFile >> nNumStudents;//reads in the # of students in the class

	outFile << "The # of students in the class:" << nNumStudents << endl;

	outFile << setw(10) << "Name" << setw(10) << "Test1" << setw(10) << "Test2" << setw(10) << "Midterm" << setw(10) << "Final" << setw(12) << "Project" << setw(14) << "Total Grade" << setw(14) << "Letter Grade" << endl;
	outFile << setw(90) << "******************************************************************************************" << endl;

	for (int nIndex = 0; nIndex < nNumStudents; nIndex++)
	{
		inFile >> strStudentName >> fTest1 >> fTest2 >> fMidterm >> fFinal >> fProject;

		fGrade = (fTest1 + fTest2 + fMidterm + fFinal + fProject);
		fGrade = (fGrade / 5);

		outFile << setw(10) << strStudentName << setw(10) << fTest1 << setw(10) << fTest2 << setw(10) << fMidterm << setw(10) << fFinal << setw(12) << fProject << setw(14) << fGrade;
		
		if(fGrade>=90)
		{
			outFile << setw(14) << "A" << endl;
			nNumA++;
		}
		else if (fGrade>=80)
		{
			outFile << setw(14) << "B" << endl;
			nNumB++;
		}
		else if (fGrade>=70)
		{
			outFile << setw(14) << "C" << endl;
			nNumC++;
		}
		else if (fGrade<70)
		{
			outFile << setw(14) << "F" << endl;
			nNumF++;
		}				
	}//END for
	
	outFile << "The # of A's:" << nNumA << endl;
	outFile << "The # of B's:" << nNumB << endl;
	outFile << "The # of C's:" << nNumC << endl;
	outFile << "The # of F's:" << nNumF << endl;

	inFile.close();
	outFile.close();
}//END main



just move the statement that prints the header outside of the for loop so it only does it once and also add the "****" line under it. I did it for you in the code here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1