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";
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:
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
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!
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!
#include <cctype>
#include <iostream>
#include <string>
usingnamespace 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");
}
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'.