Comparison Operators for strings

Hi again,
Finish one problem, cause another from me I'm afraid.

How can I take an variable which takes input of a letter from the user and then compare that to a value?

For example I ask the user to choose from five labels, A-E, and for each of those A-Es you can then choose from a different set of letters. In order to restrict this second choice correctly I need to check what the user has inputed in the first stage. My idea was:

if (firstvariable==A){
...
}

But i fear this is too simplistic. Help much appreciated.

Thanks
if(firstvariable=='A'){...}
'A' is a char, while A could be any variable.
Last edited on
You've got the right idea. Use something like the following
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
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

char menu()
  {
  string s;
  cout << "What would you like to do?\n"
          " A   Say Hello\n"
          " B   Say Goodbye\n"
          " C   Quit (default)\n"
          "Choose> "
       << flush;
  getline( cin, s );
  if (!cin || s.empty())
    s = "C";
  return toupper( s[ 0 ] );
  }

int main()
  {
  bool done = false;
  while (!done)
    switch (menu())
      {
      case 'A':
        cout << "Hello, world!\n";
        break;
      case 'B':
        cout << "Goodbye cruel world!\n";
        break;
      case 'C':
        done = true;
        break;
      default:
        cout << "What?\n";
      }
  return 0;
  }

Hope this helps.
Thanks guys, job seems to be done.
Topic archived. No new replies allowed.