I am writing a program that takes input from the .csv file. The program run but the problem is it only returns the last line of the .csv file and skips all the other 9 lines. It also does not display the average and the grade. Please help me figure out what I am doing wrong.
The professor is very specific about using what we have learned so far and that is loops and functions. I can only use loops and functions so I am not allowed to use arrays.
Here's the scores.csv file
#include <iostream>
#include <fstream>
using namespace std;
int testScore1, testScore2, testScore3, testScore4, testScore5;
double avg;
char grade;
double getAvg(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5){
avg = (testScore1 + testScore2 + testScore3 + testScore4 + testScore5)/5;
return avg;
}
char getGrade(double avg){
if (avg >= 90){
grade = 'A';
cout << 'A' << endl;
}else if (avg >= 80){
grade = 'B';
cout << 'B' << endl;
}else if (avg >= 70){
grade = 'C';
cout << 'C' << endl;
}else if (avg >= 60){
grade = 'D';
cout << 'D' << endl;
}else{
grade = 'F';
cout << 'F' << endl;
}
return grade;
}
int main(){
ifstream myFile;
myFile.open("scores.csv");
if(!myFile.is_open()){
cout << "Error" << endl;
}
string testScore1, testScore2, testScore3, testScore4, testScore5;
while (myFile.good()) {
getline(myFile, testScore1, ',');
getline(myFile, testScore2, ',');
getline(myFile, testScore3, ',');
getline(myFile, testScore4, ',');
getline(myFile, testScore5, '\n');
}
cout<< setw(15) <<" Test scores"<<endl;
cout<<"--------------------"<<endl;
cout<< " 1 2 3 4 5 Avg Grade"<<endl;
cout<<"=== === === === === ===== ====="<<endl;
cout<< " "<< testScore1<< " "<< testScore2 << " "<< testScore3 << " "<< testScore4 << " "<< testScore5<< " " << getAvg << " " << getGrade <<endl;
return 0;
}
An answer has been given already and accepted.
To be complete, I will show a refactored and working version your code.
I uses no arrays. Just loops and functions.
#include <iostream>
#include <fstream>
#include <iomanip>
double getAvg(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5){
double avg = (testScore1 + testScore2 + testScore3 + testScore4 + testScore5)/5.0;
return avg;
}
char getGrade(double avg){
char grade = 'F';
if (avg >= 90){
grade = 'A';
}else if (avg >= 80){
grade = 'B';
}else if (avg >= 70){
grade = 'C';
}else if (avg >= 60){
grade = 'D';
}
return grade;
}
int main(){
std::ifstream myFile("scores.csv");
if(!myFile){
std::cerr << "\nError: Could not open input file\n";
}
else {
std::cout<< std::setw(15) <<" Test scores\n--------------------\n 1 2 3 4 5 Avg Grade\n=== === === === === ===== =====\n";
int testScore1, testScore2, testScore3, testScore4, testScore5;
char comma1, comma2, comma3, comma4;
// Read all values in one line, then next line, next line and so on
while (myFile >> testScore1 >> comma1 >> testScore2 >> comma2 >> testScore3 >> comma3 >> testScore4 >> comma4 >> testScore5) {
// Calculate
double average = getAvg(testScore1, testScore2, testScore3, testScore4, testScore5);
char grade = getGrade(average);
std::cout<< " "<< testScore1<< " "<< testScore2 << " "<< testScore3 << " "<< testScore4 << " "<< testScore5<< " " << average << " " << grade << '\n';
}
}
return 0;
}