Please check my if and else statement

Example: Enter a letter grade: a Grade range is 90 – 100
Enter a letter grade: B Grade range is 80 – 89 << this is my instruction

here is my code

#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
char Grade, A, a, b, B;
cout<<setw(5)<< "Grade" <<setw(15) << "Range" <<setw(15) << "Grade" <<setw(15) << "Range" <<endl;
cout<<setw(5)<< "A" <<setw(15) << "90 - 100" <<setw(15) << "D" <<setw(15) << "60 - 69" << endl;
cout<<setw(5)<< "B" <<setw(15) << "80 - 89" <<setw(15) << "F" <<setw(15) << "50 - 59" << endl;
cout<<setw(5)<< "C" <<setw(15) << "70 - 79"<<setw(15)<< "" << setw(15) << "" <<endl;
cout << "Enter a letter grade: ";
cin >> Grade;
if (Grade==a && Grade==A)

{ cout << "Grade range is 90 - 100 " << endl;
}
if (Grade==b && Grade==B)

{cout << "Grade range is 80- 89 " << endl;
}

return 0;
}

showing the invalid grade at output

idk what is needed
A/a/b/B are all uninitialized variables. Using them this way doesn't make sense. Use constants instead of variables, like so:
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
char Grade, A, a, b, B;
cout<<setw(5)<< "Grade" <<setw(15) << "Range" <<setw(15) << "Grade" <<setw(15) << "Range" <<endl;
cout<<setw(5)<< "A" <<setw(15) << "90 - 100" <<setw(15) << "D" <<setw(15) << "60 - 69" << endl;
cout<<setw(5)<< "B" <<setw(15) << "80 - 89" <<setw(15) << "F" <<setw(15) << "50 - 59" << endl;
cout<<setw(5)<< "C" <<setw(15) << "70 - 79"<<setw(15)<< "" << setw(15) << "" <<endl;
cout << "Enter a letter grade: ";
cin >> Grade;
if (Grade=='a' && Grade=='A') // Note: ''

{ cout << "Grade range is 90 - 100 " << endl;
}
if (Grade=='b' && Grade=='B') // Note: ''

{cout << "Grade range is 80- 89 " << endl;
}

return 0;
}
Last edited on
if (Grade==a && Grade==A)

That won't do what you're expecting. Here, you're saying that "Grade" MUST equal to lower case AND upper case A in order for this to be true. Moreover, I don't know why you made all those variables.

Here's what you want to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	char Grade;
	std::cout << "Enter A Letter Grade: ";
	std::cin >> Grade;

	if (Grade == 'a' || Grade == 'A')
	{
		std::cout << "The Grade range is between 90 and 100";
	}

	return 0;
}


First, you let the user know what to input, then you use "cin >> " to get the input from them, putting the input as a value for the char "Grade". Once that's done, you check if "Grade" is lower case 'a' OR upper case 'A'. "Or" is indicated with " || ".
Topic archived. No new replies allowed.