Need help with char

So i'm tasked with making a code that use char to identify your grade such as A,B,C,D and F. Basically, when the persons inputs a letter grade, the output will be a certain phrase equal to the letter grade they inputted (i.e. A= You are doing excellent work) My problem is I am having trouble using char (and probably more stuff) in getting this code 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
  #include <iostream>

using namespace std;

int main()
{
	char('A'), char('B'), char('C'), char('D'), char('F');

	cout << "Enter your grade " << endl;
	cin >> char;
	if ('A');
	{
		cout << "You are doing excellent work " << endl;
	}
	else if ('B');
	{
		cout << "You are doing above average work " << endl;
	}
	else if ('C');
	{
		cout << " You are doing average work " << endl;
	}
	else if ('D');
	{
		cout << "You are doing below average work " << endl;
	}
	else ('F');
	{
		cout << "You are failing " << endl;
	}

	return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
    char grade ; // variable (of type char) to hold the grade entered by the user
    cout << "Enter your grade (A/B/C/D/F): " ;
    cin >> grade ; // read the grade entered by the user into variable grade

    // if the grade is equal to 'A' etc.
    if( grade == 'A' ) cout << "You are doing excellent work\n" ;
    else if( grade == 'B' ) cout << "You are doing above average work\n" ;
    else if( grade == 'C' ) cout << " You are doing average work\n" ;
    else if( grade == 'D' ) cout << "You are doing below average work\n" ;
    else if( grade == 'F' ) cout << "You are failing\n" ;
    else cout << "You entered an invalid grade\n" ;
}
Line 7 is pointless. You don't need a char variable for each character you plan on using, and you aren't even making variables when you do that anyway.

To make a variable, declare the type and then a name:

char grade;

^Variable declaration. You now how a variable named grade that's going to hold a single character.

Now, you want to get the input from the user - USE THE VARIABLE NAME:

std::cin >> grade

And if statements don't work like that, they simply check for true/false. The "==" operator will return a true or false. EX:

grade == 'D' //This Will Return True If Grade Is Equal To 'D'

so:

if (grade == 'D') checks if the grade is 'D'. But this:

if ('D') Doesn't actually check the variable at all!
You probably want a switch statement when you are checking things like this,
a switch statement looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "iostream"
using namespace std;

int main(){
   char grade;
   cin >> grade;
   switch(grade){
   case 'A': //checks whether the char 'grade' is equal to 'A'
   cout << "You’re doing awesome"; 
   break; // ends the case, like the squiggly brackets in the if statement

   case 'B': //checking for 'B' now
   cout << "your parents will not ground you";
   break;

   default:
   cout << "invalid input";
   }

}


It just makes it so there’s even less things to worry about when you’re looking at it.
or a lookup table.
string tbl[] = {"awesome", "good", "average", "terrible", "error", "failing"};
//check grade for in range before using table!!
if(grade >= 'A' && grade <= 'F' && grade != 'E')
cout << "your doing " << tbl[grade -'A'] << " work\n";
else cout << "invalid input\n";

this takes up less bloat and skips checking all the possible conditions and just directly resolves the information. Error checking the input is now costing more than anything else... as it often does.
Last edited on
Topic archived. No new replies allowed.