I need help with the //Find letterGrade function. The program works perfect with the if/else statements that are commented out, but I want to convert it to a switch statement. I did it, but it keeps saying that the variable 'grade' is being used without being initialized...Please help.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//Function prototypes
void inputdata(string&, string&, double&, double&, double&, double&);
double findTotal(double t1, double t2, double lab, double final);
char letterGrade(double);
void displayHeading();
void display(string, string, double, double, double, double, double, char);
//Global Parameters
ifstream dataIn("data.txt");
ofstream dataOut("outdata.txt");
//Main
int main()
{
//Delcare array size to hold # of people
const int SIZE = 5;
//Local Parameters
string first[SIZE], last[SIZE];
double test1[SIZE], test2[SIZE], lab[SIZE], final[SIZE];
double numAvg[SIZE];
char finalGrade[SIZE];
//Function Call
displayHeading();
for (int i=0; i<SIZE; i++)
{
//Function Call
inputdata(first[i], last[i], test1[i], test2[i], lab[i], final[i]);
//Function Call
numAvg[i] = findTotal(test1[i], test2[i], lab[i], final[i]);
//Function Call
finalGrade[i] = letterGrade(numAvg[i]);
//Function Call
display(first[i], last[i], test1[i], test2[i], lab[i], final[i], numAvg[i], finalGrade[i]);
}
dataIn.close();
dataOut.close();
cout <<"\n";
system ("pause");
return 0;
}
//Function to receive data and store it in main
void inputdata(string& fname,string& lname, double& test1, double& test2, double& lab, double& final)
{
dataIn>>fname>>lname>>test1>>test2>>lab>>final;
}
//Function to find numerical average
double findTotal(double t1, double t2, double lab, double final)
{
double total;
t1*=.20;
t2*=.20;
lab*=.25;
final*=.35;
total = (t1+t2+lab+final);
return total;
}
//Function to find letter grade
char letterGrade(double numAvg)
{
char grade;
switch (grade)
{
case(1):
if (numAvg < 60)
grade = 'F';
break;
case(2):
if (numAvg < 70)
grade = 'D';
break;
case(3):
if (grade < 80)
grade = 'C';
break;
case(4):
if (grade < 90)
grade = 'B';
break;
default: grade = 'A';
}
return grade;
}
// if (numAvg < 60)
// grade = 'F';
// else if (numAvg < 70)
// grade = 'D';
// else if (numAvg < 80)
// grade = 'C';
// else if (numAvg < 90)
// grade = 'B';
// else
// grade = 'A';
//
//Function to display heading
void displayHeading()
{
cout<<left;
cout<<setw(15)<<"NAME"<<setw(8)<<"TEST1"<<setw(8)<<"TEST2"<<setw(8)<<"LAB"<<setw(8)<<"FINAL"<<setw(8)<<"TOTAL"<<"GRADE";
cout<<endl;
cout<<"-----------------------------------------------------------------"<<endl;
dataOut<<left;
dataOut<<setw(15)<<"NAME"<<setw(8)<<"TEST1"<<setw(8)<<"TEST2"<<setw(8)<<"LAB"<<setw(8)<<"FINAL"<<setw(8)<<"TOTAL"<<"GRADE";
dataOut<<endl;
dataOut<<"-----------------------------------------------------------------"<<endl;
}
//Function to display data
void display(string f, string l, double t1, double t2, double lab, double final, double numAvg, char finalGrade)
{
string n;
n = f +" "+l;
cout<<left<<fixed<<showpoint<<setprecision(1);
cout<<setw(15)<<n<<setw(8)<<t1<<setw(8)<<t2<<setw(8)<<lab<<setw(8)<<final<<setw(8)<<numAvg<<setw(8)<<finalGrade;
cout<<endl;
//Send data to outfile
dataOut<<left<<fixed<<showpoint<<setprecision(1)<<endl;
dataOut<<setw(15)<<n<<setw(8)<<t1<<setw(8)<<t2<<setw(8)<<lab<<setw(8)<<final<<setw(8)<<numAvg<<setw(8)<<finalGrade;
}