Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Okay I have an issue with my code I need

to read from a file that gets 4 test scores from a text file and then grade the score

Here is the instructions:
Each line represents the four tests taken by students in a class during the semester.  You need to calculate the average for each student in the class by reading in the file.  Display the average for each student, a space, and with their letter grade.


The file content is below

44 55 77 88
79 88 100 99
77 99 98 99
100 88 89 100
55 56 40 77
100 100 99 95
88 84 87 88
96 97 99 100
30 44 77 55
79 77 88 0
54 52 60 77
88 77 88 77
44 77 10 95


the output should be like this in the image below
[^]

Please somebody help me I have 3 tries left

What I have tried:

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


double calculateAvg(int studentNum)
{
fstream infile("grades.txt",ios::in);
if(!infile){cout<<"file could not be found!";exit(1);}

double sum = 0; //sum of all test scores
double testScore; //individual student test score
int i = 0;

 while( i < 4 ) {
      infile >> testScore;
 sum = sum + testScore;
      i++;
   }

 infile.close();

return sum/4.0;
 

}

void printGrade(double average)
{
if (average >= 90)
cout << "A" << endl;
 else if (average >= 80)
cout << "B" << endl;
 else if (average >= 70)
cout << "C" << endl;
else if (average >= 60)
cout << "D" << endl;
else
cout << "F" << endl;
}

int main()
{
const int NUM_OF_STUDENTS = 0;
double averages[NUM_OF_STUDENTS];
//calculates and stores 10 students averages
int i = 0;

while(i<NUM_OF_STUDENTS) {
    averages[i] = calculateAvg(i);
      i++;
       
   }
  cout<<(averages[i])<<" ";
printGrade(averages[i]);
}
Posted
Updated 12-Oct-17 11:54am

C++
const int NUM_OF_STUDENTS = 0;
double averages[NUM_OF_STUDENTS];

Exactly how many elements do you think will be allocated to your averages array?
 
Share this answer
 
Quote:
Okay I have an issue with my code

Yes, which issue ?

You have no idea of what is the problem in your code, use the debugger, it will show you what your code is doing, step by step.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900