homework help! im stuck

so my teacher wants us to make it so when you input a letter grade that it will say for an a excellent work and for a b below average work but when i start it and it asks me to put in the letter grade i put in a and then it displays an error saying the variable grade is not being initialized, how do i fix this? i really need help i'm at my end here! i just want this to work!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 #include <iostream>

using namespace std;

int main()
{
	int grade;
	char a;
	char b;
	char c;
	char d;
	char f;
	

	cout << "please enter in your grade!" << endl;
	cin >> a;
	if (grade=a) 
	{
		cout << "Excellent work!" << endl;




		return 0;

	}
	else if(grade==b)
	{
		cout << "above average work" << endl;
		cin >> b;
	}


	







}e you need help with here.
you have int grade. but you are not giving any value to it. Grade remains a blank variable. Then you are saying if grade is equal to char a? Grade has no value.
im new to this how do i give grade a value?
You are making a comparison with grade before giving it a value, (though on an aside grade=a is not a comparison but an assignment). Actually, the only variable you give any value to is a. A variable must be initialized before being used, otherwise it only contains garbage. So to fix your problem, make sure grade contains the grade value before ever comparing it or performing an operation on it.

Giving a value to a variable with direct assignment: grade = 4;
Giving a value to a variable using user input: cin >> grade;

P.S. You cannot compare an integer to a character.
Last edited on
well he wants us to use chars for this and make it if the grade is =a then output excellent work!
or of the grade is =b then output above average work and he wants us to use if and else statements too.
how do i do that? he said instead of his example where he did this int score = 0; he said he wants use to use letter grade chars like a,b,c,d,e,f for our letter grades. how do i do this?
Last edited on
Here is a response to an email that I sent another student:

Let me clarify what you are being asked to do.The video shows you how to input a score. You are to change this so that instead of inputting a score you are going input a letter grade. The program should run something like this



Enter the grade on your last exam

C

You are doing average work



If the input was A then you would output

You are doing excellent work.



If the input was B then you would output

You are doing above average work



Hopefully I don’t need to go on here.

this is what he states
You cannot compare an integer to a character.
In C and C++ chars are tiny ints so you can compare them.
1
2
3
4
5
6
7
  int ch1 = 65;
  char ch2 = 'A';

  if (ch1 == ch2)
    cout << "Equal";
  else
    cout << "Not equal";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    char grade ;
    std::cout << "please enter in your grade: " ;
    std::cin >> grade ;

    if( grade == 'A' ) std::cout << "excellent work!\n" ;
    else if( grade == 'B' ) std::cout << "above average work!\n" ;
    else if( grade == 'C' ) std::cout << "average work!\n" ;
    else if( grade == 'D' ) std::cout << "below average work!\n" ;
    else std::cout << "fail\n" ;
}
Topic archived. No new replies allowed.