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;
		
}

line 84 you declared it but its not initialized to anything so when you get to line 87 it throws the error. you should replace grade with numAvg in switch

If statements are better than switch in this case because switch can not be used with ranges of values. For exxample you can not say case numAvg < 60

i guess i didn't think thru my comment because numAvg will not match any label.

ok what grade is supposed to store ?
tell us so that we can help
, as they said the if statement is much better in your case .

grade is to supposed to store a char variable ex. A, B, C, D, F

even if you assigned some thing to grade it won't work as you want , cause it won't match any case , try this
1- add a new function that takes the mark in numbers as parameter and this function return 1 if the mark is less than 60 and so on
2- modify your letter grade function to have two parameters the extra one has the value returned from the function i mentioned earlier , and use it in the switch statement

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.