Need help modifying this code to replace int with Chars

So this is practice code using if statements and such. My teacher now wants me to switch the int out for Chars that represent Grades A-F, Ive tried using char to do Grade = A and such but all I get are errors and uninitialized variables. Can anyone help me use the right expression?


[CODE]

int main()
{
int score = 0;
cout << "Enter your score " << endl;
cin >> score;
if (score > 89)
{
cout << "You got an A" << endl;
}
else if (score > 79)
{
cout << "You got a B" << endl;
}
else if (score > 69)
{
cout << "You got a C " << endl;
}
else if (score > 59)
{
cout << "You got a D " << endl;
}
else
{
cout << "You are failing " << endl;
}





}
If you showed us the code you were trying to compile that doesn't compile, we would be able to tell you exactly why it didn't compile, which would be more informative for you to learn from.

But to use chars here, you'd declare a variable like:
char letter_grade;

Then, within each if/else block:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (score > 89)
{
    letter_grade = 'A';
}
else if (score > 79)
{
    letter_grade = 'B';
}
// ...
else
{
    letter_grade = F';
} 


Then, at the end, you could say:
cout << "You got a " << letter_grade << '\n';

Is that what you're going for?
Last edited on
Topic archived. No new replies allowed.