Security_Gate
Security_Gate

Reputation: 53

Comparing C++ input with array values

Over the last couple months I've still been slowly but surely trudging through C++, and I've run into a snag that I've been meaning to figure out. I've tried asking/reading/searching, but I could never find an appropriate answer. Maybe it is simply because the question is sort of difficult to ask.

What I'm trying to do is at the end of my program, have the end sequence compare the input value with values within an Array. Do I have to loop a comparison sequence? Is there an easier way 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;
}

Upvotes: 2

Views: 3028

Answers (4)

Tim
Tim

Reputation: 171

A few things: You can declare variables of the same type on the same line, for example:

string Name, YN;

You can actually 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 an Integer as a boolean value if you want to.)

Bool IsYourName = True;

do {

...

IsYourName = False;

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

 if (YN == YesAnswers[i])

     IsYourName = True;

}

while (IsYourName);

That's another way to think about approaching the loop

Upvotes: 0

Maciej Hehl
Maciej Hehl

Reputation: 7985

In general case, if You want to find if some value is a member of some collection, You have to loop or use some algorithm or collection member functions as other answers suggest.

In Your case there are only four possibilities, so theoretically You could simply check all of them explicitly in a condition using logical OR (operator ||).

The best solution for Your particular problem in my opinion, is to take an input form the user, convert it to upper-case (check for example here how) and compare it with just two strings "Y" or "YES".

Upvotes: 0

Troubadour
Troubadour

Reputation: 13421

You could use a std::set. Sets are ordered and so are quicker to find things in than a std::vector for large numbers of elements however in your case you only have 4 so it probably makes little or no difference. Since sets are designed for looking things up quickly they actually have their own find member so you don't have to use std::find. So define YesAnswers as

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

and then check YN with

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

I still think @James' answer is better for your needs though as you can keep YesAnswers as an array with its one line initialisation as opposed to the multi-line insertions for a set.

Upvotes: 0

James McNellis
James McNellis

Reputation: 355019

You can use std::find() from <algorithm>:

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

Or, if you were to make 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() looks for an element in a range; if it finds the element, it returns an iterator (or a pointer, in the case of an array) to the found element; if it doesn't find the element, it returns an iterator (or a pointer) to the end of the range.

Note that, as with most (all?) of the standard library algorithms, the range is closed at the beginning but open at the end. That is, the "end" iterator/pointer should point at the element one-past-the-end.

Upvotes: 5

Related Questions