-
User prompt question
I'm trying to prompt for Y,y,Yes,yes - and I need help.
Here is what definitly doesn't work:
Code:
#include <iostream>
using std::cout;
using std::cin;
int main()
{
char c = ' ';
while( c != 'y' || c != 'Y' || c != 'Yes' || c != 'yes' )
{
cout << "Continue? (Y/N): ";
cin.get(c); // Get character from user
cin.ignore(1, '\n'); // Skip newline
if( c == 'y' || c == 'Y' || c == 'yes' || c == 'Yes')
cout << " you entered yes";
else
cout << " you entered no";
}
return 0;
}
Thanks
-
This shows how to test for lower and upper case at the same time.
Down side - it accepts any word beginning with 'y' as being yes, so it needs a little refinement in that area.
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char buff[100];
cin.getline(buff,sizeof buff);
if ( toupper(buff[0]) == 'Y' ) {
cout << "yes" << endl;
}
}
-
Why not just use getline to read the entire string and then simply test the first character?
Code:
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
bool keep_going()
{
string reply;
if ( getline ( cin, reply ) && tolower ( (unsigned char)reply[0] ) == 'y' ) {
return true;
}
return false;
}
int main()
{
while ( keep_going() ) {
cout<<"Still going\n";
}
}
-
would either of these work if the user entered: Ynoblaablaablaa?
They should only "continue" if the user entered 1 of 4 things:
Yes,yes,Y,y
Thanks again
-
Code:
string test = "y";
while(test == "y" ||
test == "Y" ||
test == "Yes" ||
test == "yes")
{
//do something
cout << "to continue enter y, Y, Yes, or yes" << endl;
cin >> test;
}
-
>would either of these work if the user entered: Ynoblaablaablaa?
Yes, with minor changes:
Code:
bool keep_going()
{
string reply;
if ( getline ( cin, reply ) ) {
if ( reply == "y" || reply == "Y" || reply == "yes" || reply == "Yes" ) {
return true;
}
}
return false;
}
-
Thank you guys for your time - I think either of these will work great, and while waiting, I actually came up with a solution of my own that is similar-
Hopefully one day I'll be the one replying with solutions instead of posting all the questions...
Code:
cout << "enter response";
cin >> response;
string a = response;
if (a == "yes" || a == "Yes" || a == "y" || a == "Y")
cout << "you entered yes";
else cout << "you entered no";