user2856303
user2856303

Reputation: 39

Comparing a string against a set of characters in C++

I'm trying to get a string of run together characters to be parsed against a set of "valid" characters. If the string of characters all are ones of the "valid" set, the code should continue on. If the string however contains any characters other than the valid set, it should return an error and prompt for re-entry, which is again checked for validity.

I came up with two different sets of code to perform the check, where "guess" is the input string and A, a, B, b, C, c, D, and d are the allowed characters. The first set of code appears to run correctly the first time, then accepts anything, and the second set will only accept a single valid letter input after entering the loop. After looking though, it seems like the issue is somehow rooted in the logic statement. Anyway, any assistance would be appreciated.

Code #1:

int main (){
using namespace std;
string guess;

cout << "please enter your multiple choice answers: ";
cin >> guess;

bool nogood = true;
int i = 0;
while (nogood==true){
if (guess[i]== ('A'||'a'||'B'||'b'||'C'||'c'||'D'||'d')){
    i++;
}
else{
    cout << "That is not a valid choice please try again: ";
    cin.clear();
    cin >> guess;
    i=0;
}

if (i=guess.length()-1){
    nogood = false;
}
else{
    nogood = true;
}

}
...code goes on

Code #2:

int main (){
using namespace std;
string guess;

cout << "please enter your multiple choice answers: ";
cin >> guess;

for (int i =0; i < guess.length(); i++){
    if (guess[i] == ('A'||'a'||'B'||'b'||'C'||'c'||'D'||'d')){
    }
    else{
        cout << "That is not a valid choice please try again: ";
        cin.clear();
        cin >> guess;
        i=0;
    }
}
...code goes on

Upvotes: 2

Views: 2989

Answers (2)

goji
goji

Reputation: 7092

Use std::string::find_first_of or std::strchr:

const std::string validChars = "AaBbCcDd";
if (validChars.find_first_of(guess[i]) != std::string::npos) {
  // whatever
}

Or:

if (std::strchr("AaBbCcDd", guess[i])) {
  // whatever
}

Upvotes: 2

Daniel Frey
Daniel Frey

Reputation: 56863

The logic statement is broken, it should read

if (guess[i] == 'A' || guess[i] == 'a' ||
    guess[i] == 'B' || guess[i] == 'b' ||
    guess[i] == 'C' || guess[i] == 'c' ||
    guess[i] == 'D' || guess[i] == 'd' )){
}

otherwise the compiler first "calculates" a single value 'A'||'a'||'B'||'b'||'C'||'c'||'D'||'d' (which is equivalent to true) and compares the guess[i] with true which is this case means true is converted to 1.

Also, in your first code example you use

if (i=guess.length()-1)

but this assigns to i instead of comparing it. You need == instead of =:

if (i==guess.length()-1)

Finally, you can simplify the whole test using std::string::find_first_not_of() to

cout << "please enter your multiple choice answers: ";
cin >> guess;

while( guess.find_first_not_of( "AaBbCcDd" ) != std::string::npos ) {
    cout << "That is not a valid choice please try again: ";
    cin.clear();
    cin >> guess;
}

Upvotes: 6

Related Questions