ASSGINMENT #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.

A couple questions because I'm kinna wondering a few things.
First- I've created a Text file using notepad with the following data:

10
Isaacs 10 8 22 16 22
Brown 7 10 17 11 19
Marley 1 4 21 29 11 
Holt 1 2 18 10 17 
Washington 1 5 21 8 18
Hammond 3 7 16 14 14
Ellis 8 9 20 30  23
Spear 10 10 25 28 21
Wilson 8 3 22 24 25
Bolo 5 4 23 22 11 

I saved it to my Desktop as an .dat File. How do i ask the program to read from left to right the student’s Lastname, 2 test-scores, 2 exam scores and project score? I also need to output for each student, the program should print, to an output file, his/her name, average, and grade...

I'm kinna lost-but i know the program would have near to it's completion:

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




int main()
{


if(gradeAvg>=90)
cout<< "A";
else if (gradeAvg>=80)
cout<< "B";
else if (gradeAvg>=70)
cout<< "C";
else if (gradeAvg>=60)
cout<< "D";
else
cout<< "F";

    return 0;
}

Any help is appreciated

Recommended Answers

All 67 Replies

Read this tutorial. Searching for C++ File IO in google will also give you more than enough results.

if(gradeAvg>=90)
cout<< "A";
else if (gradeAvg>=80)
cout<< "B";
else if (gradeAvg>=70)
cout<< "C";
else if (gradeAvg>=60)
cout<< "D";
else
cout<< "F";

In the above code....what I'm trying to get is a program to 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)....As ii is.....if there is a grade that both fits the criteria of being above 70 and 80...it would print A, B and C....correct. Any help is appreciated.

Given a series of "else if", you only ever get the first one which evaluates the true. Any other later ones which also happen to be true are skipped.

My inFile is:Isaacs; 10; 8; 22; 16; 22;
Brown; 7; 10; 17; 11; 19;
Marley; 1; 4; 21; 29; 11;
Holt; 1; 2; 18; 10; 17;
Washington; 1; 5; 21; 8; 18;
Hammond; 3; 7; 16; 14; 14;
Ellis; 8; 9; 20; 30; 23;
Spear; 10; 10; 25; 28; 21;
Wilson; 8; 3; 22; 24; 25;
Bolo; 5; 4; 23; 22; 11;
---------------------------------------------------------------------------------
My source is is:

[TEX]#include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
#include<cmath>

using namespace std;




int main()
{	
	int stunum;//# of students in the class
	int stuname;// Name of the student
	int Test1;//score for test 1
	int Test2;//score for test 2
	int Midterm;//score for midterm
	int Final;//score for final
	int Term;//score for term project
	ifstream inFile;
	ofstream outFile;

	inFile.open ("ClassData.dat");
	outFile.open ("ClassOutput");
	inFile>>stunum>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;//variables for student info
	int gradeAvg= Test1 + Test2 + Midterm + Final + Term;//All semester grade of students






if(gradeAvg>=90)
cout<< "A";
else if (gradeAvg>=80 || gradeAvg<90)
cout<< "B";
else if (gradeAvg>=70 || gradeAvg<80)
cout<< "C";
else if (gradeAvg>=60 || gradeAvg<70)
cout<< "F"<<endl;



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

	return 0;
}[/TEX]

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

A few problems guys:
1. 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. As you can see from my inFile, it looks like they compiler is reading the data, but if you look at my values for in the first line...mathematically, they add up to "78". So why does it output "B" instead of "C"?
2.It's apparent that the program is reading the data, but how would I also get the program to read the names of the student's in my inFile...i.e. Isaacs, Marley...ect....
3. How can i get the the program should print a summary report stating the number of “A”s, “B”’s, “C”’s and “F”’s assigned.

Thanks for all the tips, advice and assistance guys..

The source code is as follows:

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

using namespace std;




int main()
{	
	int stunum;//# of students in the class
	int stuname;// Name of the student
	int Test1;//score for test 1
	int Test2;//score for test 2
	int Midterm;//score for midterm
	int Final;//score for final
	int Term;//score for term project
	ifstream inFile;
	ofstream outFile;

	inFile.open ("ClassData.dat");
	outFile.open ("ClassOutput");
	inFile>>stunum>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;//variables for student info
	int gradeAvg= Test1 + Test2 + Midterm + Final + Term;//All semester grade of students






if(gradeAvg>=90)
cout<< "A";
else if (gradeAvg>=80 || gradeAvg<90)
cout<< "B";
else if (gradeAvg>=70 || gradeAvg<80)
cout<< "C";
else if (gradeAvg>=60 || gradeAvg<70)
cout<< "F"<<endl;



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

	return 0;
}

> else if (gradeAvg>=80 || gradeAvg<90)
Your first attempt a couple of posts ago was much better.

This expression is always true - I figure you meant && rather than ||

But the extra test is redundant anyway because of the way "if/else if" chains work.

When i revert back to my old if-else....it print's "F" rather than "C"...

Did you check that you read the file properly?

Because if your input file really contains all those semicolons, then your attempt to read the file will fail, and your average calculation is just junk.

After reading the data from the file, try printing it out to see if the data is correct.

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

using namespace std;




int main()
{	
	int stunum;//# of students in the class
	int stuname;// Name of the student
	int Test1;//score for test 1
	int Test2;//score for test 2
	int Midterm;//score for midterm
	int Final;//score for final
	int Term;//score for term project
	ifstream inFile;
	ofstream outFile;

	inFile.open ("ClassData.dat");
	outFile.open ("ClassOutput");
	inFile>>Test1>>Test2>>Midterm>>Final>>Term;//variables for student info
	int gradeAvg= Test1 + Test2 + Midterm + Final + Term;//All semester grade of students






if(!inFile &&! outFile)
cout<<"You have an error"<<endl;
else if(gradeAvg>=90 && 89<gradeAvg)
outFile<< "A";
else if (gradeAvg>=80 && 79<gradeAvg)
outFile<< "B";
else if (gradeAvg>=70 && 69<gradeAvg)
outFile<< "C";
else if (gradeAvg<70)
outFile<< "F"<<endl;




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

	return 0;
}

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

A few problems guys:
1. 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. As you can see from my inFile, it looks like they compiler is reading the data, but if you look at my values for in the first line...mathematically, they add up to "78". So why does it output "B" instead of "C"?
2.It's apparent that the program is reading the data, but how would I also get the program to read the names of the student's in my inFile...i.e. Isaacs, Marley...ect....
3. How can i get the the program should print a summary report stating the number of “A”s, “B”’s, “C”’s and “F”’s assigned.

Thanks for all the tips, advice and assistance guys..

The values in those if conditions (line 36) are incorrect. A grade can not be both > 90 and < 89 at the same time! And if someone gets 100% then your program will give him no grade at all. Line 36 should read simply >= 90. The conditional statements in your original post seem to be correct.

As Salem pointed out all those semicolons will cause huge problems. Edit your data file and remove them.

I have removed the semi-colons as was suggested....as u can see that i've placed error log in the event that the program hasn't read any values from my input file. However, the problems that i listed before are still there. I've also changed my conditional staements. I can't see where i'm going wrong and how to get the name to the output file....thanks.

> inFile>>stunum>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;
But your input file doesn't have a student number.
So I can't see how this code can ever possibly work.

Furthermore, until this works as expected (the first line of the file is printed back to you), the rest of the code can be ignored.

inFile>>stunum>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;
cout << stunum << " " << stuname << " " << Test1 << " " 
     << Test2 << " " << Midterm << " " << Final << " " << Term << endl;

my varaible 'stunum' refers to the # of students in the class

Maybe so, but your example input file doesn't begin with a student number, but a student name.

So either the input file you posted is wrong, or your code is wrong - make a choice and fix the one which is wrong.

I've fixed my inFile....so it does show the correct grade now...but how do i get the program to show the names of the students and tally all the A's, B's, C's and F's? I'm pretty sure it wouldn't be a counter because we aren't receiving any input from the user? Thx.

To show the total number of A's, B'c, etc you need a counter for each grade and expand those if conditions to tally up the number of people with each grade For example:

int NumA = 0;
int NumB = 0;
int NumC = 0;
int NumD = 0;
int NumF = 0;

if(gradeAvg>=90)
{
    NumA++;
    outFile<< "A";
}

now convert line 26 into a while loop and put all that inside the loop

int NumA = 0;
int NumB = 0;
int NumC = 0;
int NumD = 0;
int NumF = 0;
while( inFile>>stunum>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)
{
    if(gradeAvg>=90)
    {
        NumA++;
        outFile<< "A";
    }
    // remain if conditions go here
}
#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
	int Test1;//score for test 1
	int Test2;//score for test 2
	int Midterm;//score for midterm
	int Final;//score for final
	int Term;//score for term project
	int NumA = 0;//# of A's
	int NumB = 0;//# of B's
	int NumC = 0;//# of C's
	int NumF = 0;//# of D's
	int gradeAvg= Test1 + Test2 + Midterm + Final + Term;

	ifstream inFile;
	ofstream outFile;

	inFile.open ("ClassData.dat");
	outFile.open ("ClassOutput");
	while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)//variables for student info
	
	
    if(gradeAvg>=90)
    {
        NumA++;
        outFile<< "A";
    }
else if (gradeAvg>=80 || 79<gradeAvg)
	{
		NumB++;
		outFile<< "B";
	}
else if (gradeAvg>=70 || 69<gradeAvg)
	{
		NumC++;
		outFile<< "C";
	}
else if (gradeAvg<70)
	{
		NumF++;
		outFile<<"F"<<endl;
	}

	

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

	return 0;

}

----------------------------------------------------------------------------------
When I compile it says: The variables Test1, Test2, Midterm, Final and Term are being used without being defined...I gess it has to do with my 'int gradeAvg= Test1 + Test2 + Midterm + Final + Term' in relation to 'while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)'...So where should i place these terms so it goes through? thx

You forgot brackets for the while loop so it's only doing the first if-check

while(condition)
{
    //do this stuff
}

also you should use the space bar on your keyboard to format the code better so that it is easier to read, and you may also get a better grade.

And you still have those damed || conditions in the if statements :icon_eek: Your program will never work until you fix that. For example lets assume the grade is 60, your program will give him a B instead of a C. It doesn't really matter what the grade is -- any grade below 90 is a B, even a grade of 0 :-O

I see it now....I've made the necessary corrections to my program including the placement of the {}....the last problem is where to place the two follwing statements:
'int gradeAvg= Test1 + Test2 + Midterm + Final + Term' in relation to 'while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)'.

Since gradeAvg is the average for the student just read, put that statement immediately following the while keyword -- you will have to add braces around the while loop because it will now be multiple-line controle.

while( infile >> <snip> )
{
    int gradeAvg = ...
    if(gradeAvg>=90)
    {
         <snip>
     }
}

My inFile contains the following:
----------------------------------------------------------------------------------
Isaacs 10 8 22 16 22
Brown 7 10 17 11 19
Marley 1 4 21 29 11
Holt 1 2 18 10 17
Washington 1 5 21 8 18
Hammond 3 7 16 14 14
Ellis 8 9 20 30 23
Spear 10 10 25 28 21
Wilson 8 3 22 24 25
Bolo 5 4 23 22 11
----------------------------------------------------------------------------------
My outFile(which is going crazy) contains the following:
----------------------------------------------------------------------------------
CF
F
F
F
F
AABF
---------------------------------------------------------------------------------
My source code contains the following:
----------------------------------------------------------------------------------

#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
	int Test1;//score for test 1
	int Test2;//score for test 2
	int Midterm;//score for midterm
	int Final;//score for final
	int Term;//score for term project
	int NumA = 0;//# of A's
	int NumB = 0;//# of B's
	int NumC = 0;//# of C's
	int NumF = 0;//# of F's
	
	ifstream inFile;
	ofstream outFile;

	inFile.open ("ClassData.dat");
	outFile.open ("ClassOutput");
	while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)//variables for student info

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

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

	return 0;

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

Almost there guys:confused: A few questions....I've placed the statements/conditions as they should, but as you can see from the output in the outFile.....I'm getting something way-off. Secondly, how do I get the names of the students to show? Thanks.

> My outFile(which is going crazy) contains the following:
Well only 'F' gets a newline, the rest don't

> Secondly, how do I get the names of the students to show? Thanks. outfile << stuname; perhaps?

ok thanks for your help....the counter though-isn't working...when I compile the program it doesn't tell me the number of “A”s, “B”’s, “C”’s and “F”’s assigned. Also, in addition to the letter grades, how can i get the program to also show the numeric grade average after it's calculated(average)? Thanks.

> it doesn't tell me the number of “A”s, “B”’s, “C”’s and “F”’s assigned
That's because you don't print them, at least not in any code you've posted already.

> how can i get the program to also show the numeric grade average after it's calculated(average)?
Same as everything else, it's just a bit of math and cout statements in the right places.

Ok, i got the mathematical part of it and have made the corrections so i'm now able to get the numeric average printed out. However, with regards to the tally of the different letter grades, my source code is as below:
----------------------------------------------------------------------------------

#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
	int Test1;//score for test 1
	int Test2;//score for test 2
	int Midterm;//score for midterm
	int Final;//score for final
	int Term;//score for term project
	int NumA = 0;//# of A's
	int NumB = 0;//# of B's
	int NumC = 0;//# of C's
	int NumF = 0;//# of F's
	
	ifstream inFile;
	ofstream outFile;

	inFile.open ("ClassData.dat");
	outFile.open ("ClassOutput");
	while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)//variables for student info

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

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

	return 0;

}

----------------------------------------------------------------------------------
My outFile comes out like this, i thought that the counter would automatically do the tally as a whole and not for each individual letter grade...Thanks.

Also, how could i get the program to inform the user of the number of student's in the class? I've placed the number '10' in my inFile, but it messes everything else up...where in my inFile/source code should I put the variable 'stunum' for the number of students?

> outFile<<NumA++;
Shouldn't you be printing these after the while loop has finished reading the file?

hhhmmm. Did you mean something like this:

#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
int Test1;//score for test 1
int Test2;//score for test 2
int Midterm;//score for midterm
int Final;//score for final
int Term;//score for term project
int NumA = 0;//# of A's
int NumB = 0;//# of B's
int NumC = 0;//# of C's
int NumF = 0;//# of F's
ifstream inFile;
ofstream outFile;
inFile.open ("ClassData.dat");
outFile.open ("ClassOutput");
while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)//variables for student info
{
int gradeAvg= Test1 + Test2 + Midterm + Final + Term;
if(gradeAvg>=90)
{
outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"A"<<endl;
}
outFile<<NumA++;
else if (gradeAvg>=80)
{
outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"B"<<endl;
}
outFile<<NumB++;
else if (gradeAvg>=70)
{
outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"C"<<endl;
}
outFile<<NumC++;
else if (gradeAvg<70)
{
outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"F"<<endl;
}
outFile<<NumF++;
}
inFile.close();
outFile.close();
return 0;
}

If i place the counters after the loop...it pretty much prints four zeros, because that was the initial value being declared for them.

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.