Comparing C ++ input with array values

Over the past couple of months, I've still slowly but surely made my way through C ++ and I ran into the problem I had in mind to understand. I've tried asking / reading / searching, but I couldn't find a suitable answer. Maybe it's just because the question is rather difficult to ask.

What I am trying to do is end my program so that the final sequence compares the input value with the values ​​inside the array. Do I need to quote the comparison sequence? Is there an easier way to get around this?

#include <iostream>
#include <string>
using namespace std;

int main () {

    string YesAnswers[5] = {"Y", "YES", "yes" "y"};
    string Name;
    string YN;

    do {
        cout << "Enter your name: ";
        getline(cin, Name);

        cout << "Your name is "<< Name;

        cout <<"\nIs this correct? Y\N: ";
        cin >> YN;

    } while(YN == YesAnswers);

    system("Pause");
    return 0;
}

      

+2


a source to share


4 answers


You can use std::find()

from <algorithm>

:

while (std::find(YesAnswers, YesAnswers + 4, YN) != YesAnswers + 4);

      

Or, if you want to do YesAnswers

a vector

or some other container instead of an array:



std::vector<std::string> YesAnswers;

while (std::find(YesAnswers.begin(), YesAnswers.end(), YN) != YesAnswers.end());

      

std::find()

searches for an element in a range; if it finds an element, it returns the iterator (or pointer, in the case of an array) to the found element; if it doesn't find an element, it returns an iterator (or pointer) to the end of the range.

Note that, like most (all?) Standard library algorithms, the range is closed at the beginning but opened at the end. That is, the "end" of the iterator / pointer must point to the one-past-the-end element.

+4


a source


You can use std::set

. The sets are ordered and therefore faster to find things than std::vector

for a large number of items, but in your case you only have 4, so it is probably not much or not at all different. Since kits are designed to quickly find things, they actually have their own member find

, so you don't need to use std::find

. So define YesAnswers

as

set< string > YesAnswers;
YesAnswers.insert( "Y" );
YesAnswers.insert( "YES" );
//... and so on

      

and then check YN

for



YesAnswers.find( YN ) != YesAnswers.end()

      

I still think @ James's answer is better for your needs, although you can store YesAnswers

as an array with its one line initialization rather than multiple lines to set.

0


a source


In general, if you want to know if a value is a member of some collection, you need to loop or use some algorithm functions or member functions as other answers suggest.

In your case, there are only four possibilities, so in theory you could just test them all explicitly in the state using a logical OR (operator ||).

The best solution for your specific problem, in my opinion, is to select a user form, convert it to uppercase (like here how) and compare it to two strings "Y" or "YES".

0


a source


A couple of things: you can declare variables of the same type on the same line, for example:

string Name, YN;

In fact, you can declare a boolean initialized to True for your loop and change it to False given a set of conditions. ** (C ++ treats any non-0 value as True and 0 as False, so you can actually use Integer as a boolean value if you like.)

Bool IsYourName = True;

do {

...

IsYourName = False;

for (int i = 0; i <YesAnswers.size (); i ++)

 if (YN == YesAnswers[i])

     IsYourName = True;

      

}

while (IsYourName);

This is another way to think about approaching a loop

0


a source







All Articles