Hello all. I'm currently in a programing class where the professor is just falling through. We get some training in pseudo code then are required to go and figure out the C++ apparently on our own. Not working out so well. :( I have some code made for a project to figure out grades for a class. I keep receiving an undeclared identifier error but I can't figure out what I'm missing or doing wrong. It starts with the first A and hits for the b, c, d, and f. Any advice? As I said we haven't had much help so there is probably a few errors.
#include <iostream>
using namespace std ;
int main()
{
float score;
char grade;
// read in total score
cout << endl ;
cout << "Enter total score (float, must be <= 100) : " ;
cin >> score ;
{
if (score >= 85)
grade = A;
else if (score >= 75)
grade = B;
else if (score >= 65)
grade = C;
else if (score >= 55)
grade = D;
else grade = F;
}
// display the result
cout << endl ;
cout << "Your grade for CMIS 102 is: " << grade << endl ;
The problem is how your if and else if statements are assigning the char variable for grade. Char's hold a single character in the ASCII table. The ASCII equivalent for the letter A is number 65. So you are trying to assign the number 65 to a char variable, which you can't do using the code. Int's yes, char's no.
What you want to do is assign the letter grade 'A' to the char variable and you do that by enclosing them in single quotes.
cout << endl ;
cout << "Enter total score (float, must be <= 100) : " ;
cin >> score ;
if (score >= 85)
grade = 'A';
else if (score >= 75)
grade = 'B';
else if (score >= 65)
grade = 'C';
else if (score >= 55)
grade = 'D';
else grade = 'F';
// display the result
cout << endl ;
cout << "Your grade for CMIS 102 is: " << grade << endl ;
system("PAUSE");
return (0); // terminate with success
}
for char to display character use ' ' . And you could use #include <string> and just replace char grade with string grade, then you can add " " on your letters.