سؤال

I have this simple c++ program for a console application in VS 2013 Express (for Windows Desktop):

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

int main()
{
string mystr;
cout << "Welcome, what is your name? ";
getline(cin, mystr);
cout << "Nice to meet you, " << mystr << endl;
cout << "May i call you \"Idiot\" for short? (y/n)" << endl;

string mystr2;
getline(cin, mystr2);

while ( ??? ) 
{
    if (cin)
    {
        if (mystr2 == "y")
        {
            cout << "Thank you, Idiot" << endl;
        }
        else
        {
            if (mystr2 == mystr)
            {
                cout << "You found the hidden secret! The hidden secret is..... I dunno. It is what ever you want it to be. \n \nProbably a let down." << endl;
            }
            else
            {
                if (mystr2 == "n")
                {
                    cout << "Ok then, " << mystr << endl;
                }
                else
                {
                    cout << "Please enter a valid response (y/n)" << endl;
                    getline(cin, mystr2);
                }
            }
        }
    }
}

}

I've just started to learn and i prefer to learn as i make something, so i decided to do this. As you may guess from the final else statement, i want it to say "Please enter a valid response (y/n)" if the user has put in anything other than y, n or mystr2 == mystr. It works fine but i need to it loop so i have put it all in the while statement.

I now need an expression for the while statement which won't effect it, or is a result of the user triggering the last else statement. This filler is what i need help with.

I know it is small and pointless but i want to finish it.

هل كانت مفيدة؟

المحلول 2

You can simply use while(true) for the loop to execute indefinitely. If you ever want it to stop, you must break or return

نصائح أخرى

getline returns a reference to the stream from which it was reading. And all streams provide an implicit conversion to bool which results in true if the stream is valid (i.e. if the last operation succeeded). So it's pretty idiomatic to do this:

string mystr2;

while (getline(cin, mystr2))
{
    if (mystr2 == "y")
    {
        cout << "Thank you, Idiot" << endl;
    }
    else
    {
        if (mystr2 == mystr)
        {
            cout << "You found the hidden secret! The hidden secret is..... I dunno. It is what ever you want it to be. \n \nProbably a let down." << endl;
        }
        else
        {
            if (mystr2 == "n")
            {
                cout << "Ok then, " << mystr << endl;
            }
            else
            {
                cout << "Please enter a valid response (y/n)" << endl;
            }
        }
    }
}

I think you are looking for while(true) which is just an endless loop until you break or return out of it. It will not affect your anything else.

I think you want while(mystr2!="y" && mystr2!="n" && mystr2!=mystr) That way if the input is anything other than "y", "n", or mystr, it will keep looping.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top