Hello everyone, my assignment is to calculate each student's test average and final letter grade from a class of 20 students with 3 tests. The scores for each test are located in the text files that I am attaching.

I have worked on this code for some time now, but in the output, all of the averages are incorrect.

Here is an example of what a section of an output might look like:

Student #1:
Class Average: 95

But I want to display this for each of the 20 students. Instead, it gave me a class average of -57283, which is obviously wrong.

Here is my code:

``````#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
int score1, score2, score3;
int grade, num, student;
int avg;
char letter;
ifstream inp1, inp2, inp3;
inp1.open("test1.txt");
inp2.open("test2.txt");
inp3.open("test3.txt");
for(student=1; student<21; student++)
{
num=1;
{
}
if(90<=avg)
letter='A';
else if(80<=avg<90)
letter='B';
else if(70<=avg<80)
letter='C';
else if(60<=avg<70)
letter='D';
else
letter='F';
cout<<"Student #"<<num<<": "<<endl;
cout<<"Class Average: "<<avg<<endl;
cout<<endl;
num++;
}
char holdscr;
cin>>holdscr;
inp1.close();
inp2.close();
inp3.close();
return 0;
}``````

## All 6 Replies

Take this piece of code:

``````for(grade=0; grade<num; grade++)
{
}
cout<<avg<<endl;``````

Run it by hand on paper for 3 students. What does avg display?

Be sure you execute each statement exactly as written, not as you think it should be.

And please learn now to format your code properly. It helps us help you, and you yourself can see what's happening easier.

Sorry, I am having trouble understanding what you are trying to say. Do you suggest that I insert

``cout << avg << endl;``

after the for loop? Or are you saying that there is a mistake within the for loop?

Sorry, I am having trouble understanding what you are trying to say. Do you suggest that I insert

``cout << avg << endl;``

after the for loop? Or are you saying that there is a mistake within the for loop?

Really? OK:

Print out just that piece of code -- the loop.
Sit at your desk.
Put that pice of code in front of you.
Pick up a pencil.
Write down some numbers labeled FILE that will be the values of the 'file' you are going to read (based on your assignment). Use only 3 students for this.
Looking at those numbers, what should the answer be for the class average (based on your assignment). Write it down.
You can also calculate each student's average. Write them down, too.

Now, start with the FOR statement and write down `num =` whatever num is (from 'file' -- it's 3) `grade = 0` since that's what the FOR does
Go to the next line and "input" (write down) a value for score1[grade] (the first number from your 'file')
Continue to the next statement and do what it says.
Keep doing this until you have finished the loop.

What's the value of avg? If avg is correct, you didn't follow your loop as written. If avg is wrong, why? If you don't know, do it again and really look at what you are doing.

As for "And please learn now to format your code properly. It helps us help you, and you yourself can see what's happening easier", just follow the link.

WaltP, thanks for the help! I have corrected my code. Apparently, I misread the assignment, which doesn't require the student numbers. Instead, it required that I output a list of scores, the calculated average, and the letter grade for each student in the class.

Here is the corrected code:

``````#include <iostream>
#include <fstream>

using namespace std;

int main()
{
int score1, score2, score3;
int grade, num, student, avg;
char letter;
ifstream inp1, inp2, inp3;

inp1.open("test1.txt");
inp2.open("test2.txt");
inp3.open("test3.txt");

for(student = 1; student < 21; student++)
{
num = 1;

{

cout << "Test Grades: " << score1[grade] << " "
<< score2[grade] << " " << score3[grade] << endl;

if(90 <= avg)
letter = 'A';
else if(80 <= avg && avg < 90)
letter = 'B';
else if(70 <= avg && avg < 80)
letter = 'C';
else if(60 <= avg && avg < 70)
letter = 'D';
else
letter = 'F';

}

cout << "Student Average: " << avg << endl;
cout << "Final Grade: " << letter << endl;
cout << endl;

}

char holdscr;
cin >> holdscr;

inp1.close();
inp2.close();
inp3.close();

return 0;
}``````

Good.

By the way, you can make these IFs simpler:

``````if(90 <= avg)
letter = 'A';
else if(80 <= avg && avg < 90)  // for this if is it even possible for AVG
// to be >= 90?
letter = 'B';
else if(70 <= avg && avg < 80)  // Same here with 80...
letter = 'C';
else if(60 <= avg && avg < 70)
letter = 'D';
else
letter = 'F';``````

Also `if(90 <= avg)` is confusing to most professionals. `if(avg > 90)` is the (unwritten) standard format. But this is not set in stone.

Okay, thanks for the tip :)

Be a part of the DaniWeb community

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