Validating int and char using enum?

Hi,
I am trying to validate whether a user did not input one of the following 4 characters: a,b, 1 and 2, and was wondering if I can use an enum type to accomplish this. Here is a sample code of what I was trying to do. Thanks in advance for any assistance offered.

1
2
3
4
5
6
7
8
9
10
11
12
13
enum e{a = 1, b};

char choice;

cout << "Enter either a, b, 1 or 2: " ;
while(!(cin >> choice) || (choice != a) || (choice != b) || (choice != 'a') || (choice != 'b') || (cin.get()!= '\n'))
{
   cin.clear();
   cin.ignore(1000,'\n');
   system("cls");
   cout << "Enter either a, b, 1 or 2: ";
}
cout << "Input is valid";
a and b are equal to 1 and 2, not to '1' and '2' ( ASCII codes )
Thanks for the reply Bazzy.

So what you are saying is I can't use a char type (choice) to validate an enum type.
You need to set a to '1' instead of 1
Your construct won't work anyway. The choice cannot ever be equal to all of the valid responses, and short circuit evaluation will always prevent that final cin.get() from being called.

The proper way is to read the user's input as a string, then test against those choices. Here's something that will work for your case:
1
2
3
#include <cctype>
#include <iostream>
#include <string> 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string valid_inputs = "ab12";
char choice;
cout << "Please enter one of a, b, 1, or 2: " << flush;
while (true)
{
  string users_input;
  getline( cin, users_input );
  if (!users_input.empty())
  {
    string::size_type index = valid_inputs.find( tolower( users_input[ 0 ] ) );
    if (index != string::npos)
      {
      choice = valid_inputs[ index ];
      break;
      }
  }
  cout << "You must only enter one of a, b, 1, or 2: " << flush;
}
cout << "Good job! You entered '" << choice << "'.\n";

Hope this helps.

[edit] Fixed line 10 to use tolower() instead of toupper()... Sorry 'bout that! :-S
Last edited on
Bazzy,
Thanks for the reply. Unfortunately, when I did this:
enum e{a = '1', b};
it did not work.
Duoas,
Thanks for taking the time to provide that code!

I like the approach you took and it taught me some new tricks which I will use from now on (especially line 1 and line 8).

string valid_inputs = "ab12";

if(!users_input.empty())

But I'm currently googling 2 lines from your code that I don't understand (10 and 11) (particularly size_type and npos):

string::size_type index = valid_inputs.find( toupper( users_input[ 0 ] ) ); index != string::npos

Now I tried to run it but I'm getting a "compilation failed with 0 errors and 2 warnings" message in wxDevC++ (I am fed up with wxDev and Dev by the way) so I need to double check it and do my research. Will post back soon Thanks Duoas!
Last edited on
*stiinkin browser crashed... let's see if I can remember what I was writing...

Dev-C++ has an old version of MinGW installed. You may want to download the latest.

Also, tell use what the two warnings were.
Duoas,
I left out "using namespace std". On one machine I am running wxDev and for the life of me I cannot get the task bar with the following tabs to appear: "Compile Resources", "Compile Log", "Debug" and "Find Results" so I would not have been able to tell you what the warnings were. So I'll look for the latest version of MinGW but if Im not mistaken, I vaguely remember reading a warning on wxDev's site which said that if you download a particular update (can't remember which one), you will lose the ability to create GUIs in wxDev. Anyway I'll get my fact straight and try and download the lastest MinGW.

Now I ran the code but it only accepts 1 and 2 as valid inputs, (not a and b) for some reason. Any idea why? I'll keep researching this until it works. Thanks!

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
#include <cctype>
#include <iostream>
#include <string> 
using namespace std;

int main()
{
    string valid_inputs = "ab12";
    char choice;
    cout << "Please enter one of a, b, 1, or 2: " << flush;
    while (true)
    {
        string users_input;
        getline( cin, users_input );
        if (!users_input.empty())
        {
            string::size_type index = valid_inputs.find( toupper( users_input[ 0 ] ) ); // should be tolower
            if (index != string::npos)
            {
                choice = valid_inputs[ index ];
                break;
        }
        }
            cout << "You must only enter one of a, b, 1, or 2: " << flush;

}
cout << "Good job! You entered '" << choice << "'.\n";

system("pause");
}

Last edited on
Duoas' code used tolower. Your code uses toupper.

tolower converts to lower case so that string::find() finds one of 'a', 'b', '1', '2'.
toupper converts to upper case so string::find() will never find 'a' or 'b'.
Jsmith,
Your right, Duoas code works like a charm!
Last edited on
Topic archived. No new replies allowed.