1

So my problem is, I want to terminate the program if the right input was inserted, like A-E(big or smallcaps) and I want to ask them again the question if they input wrong letters like k,x,L. But my problem is, it wont stop asking even if the right letter is inputted.

#include <iostream>
#include <string>
using namespace std;
int main()
{
char letter;
char grades;
char A, B, C, D, E;
letter = A, B, C, D, E;
do{
cout << "\n Input grade letter: ";
cin >> letter;
if (letter=='A' || letter=='a')
{
    cout << "Your grade is in the range of 96-100";
}else if (letter=='B' || letter=='b')
{
    cout << "Your grade is in the range of 91-95";
}
else if (letter=='C' || letter=='c')
{
    cout << "Your grade is in the range of 86-90";
}
else if (letter=='D'|| letter=='d')
{
    cout << "Your grade is in the range of 80-85";
}
else if (letter=='E'|| letter=='e')
{
    cout << "Your grade is in the range of 79 and below";
}
else {
    cout << "Invalid Input! Try again!";
}
}while(letter==A, B, C, D, E);
///if the input correct
cout<< "\nBye";

return 0;

}

user438383
  • 5,716
  • 8
  • 28
  • 43
maru11
  • 11
  • 2
  • 3
    What exactly is this line supposed to do `letter = A, B, C, D, E;`? I think you have a very fundamental misunderstanding of the difference between variable names and `char` literals. – Cory Kramer Oct 27 '21 at 14:21
  • while(letter==A, B, C, D, E); doesn't do what you (probably) expect it does. – erenon Oct 27 '21 at 14:22
  • B C D E are evaluated to true by your , separation – Anis Belaid Oct 27 '21 at 14:25
  • There are multiple problems with the code but the linked questions is not a duplicate – Ivaylo Strandjev Oct 27 '21 at 14:25
  • What can I do with my code? – maru11 Oct 27 '21 at 14:26
  • First learn a little more of the fundamental concepts of the language before you try to do something like this. You are just going to frustrate yourself with issues like this and other people with questions about issues like this if you don't first understand the basics of the language, its operators and syntax. – Louis Cloete Oct 27 '21 at 14:30

1 Answers1

0

There are multiple problems with your code:

  1. char A, B, C, D, E; does not assign any values to the variables A, B, C, D, E. It seems you expect they will be set to 'A' 'B' ... 'E'.

  2. letter = A, B, C, D, E; does not do what you seem to expect. letter is just a char, it can not hold multiple values. Consider using a data structure better suited for what you need, in particular std::set

  3. Similarly while(letter==A, B, C, D, E); does not check if letter is one of these values.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176