#include <iostream>
#include <string>
usingnamespace std;
int main() {
string response;
cout >> "Are you ready?"; << endl;
{
cin >> response;
if (response == "yes"){
cout << "What is Your race?" << endl;
}
if (response == "Yes"){
cout << "What is your race?" << endl;
}
if (response == "No") {
cout << "Tell me when you are." << endl;
/* I'm wondering if there is a way that I can make it so when they say that they are not ready, the code will reset to "Are you ready?" and the player has the option to say Yes or no again and it will not continue until they eventually say yes. */
}
}
Yeah sorry about line 8 I retyped it for some reason instead of just copying it from my program lol, And I've been trying to figure out how to combine the Yes queries into a single If statement thank you for that!
There are a couple of better ways to check your responses, that take into account the case of the entries and possible mistypes:
1. if (response[0] == 'y' || response[0] == 'Y')
Checks only the first character element in your response string
2. if (std::toupper(response[0]) == 'Y')
Converts the first character element to be upper case so only one comparison is needed. Be sure to include the <cctype> header file if you use std::toupper().
http://www.cplusplus.com/reference/cctype/toupper/?kw=toupper
while(true)
{
std::cout << "Are you ready?";
std::cin >> response;
if (std::toupper(response[0]) == 'Y')
{
std::cout << "What is your race?\n";
break;
}
elseif (std::tolower(response[0]) == 'n')
{
std::cout << "Tell me when you are.\n";
}
else
{
std::cout << "Huh? What did you say?\n";
}
}
Are you ready?n
Tell me when you are.
Are you ready?t
Huh? What did you say?
Are you ready?y
What is your race?
std::tolower() converts characters to lower case, the opposite of std::toupper().
http://www.cplusplus.com/reference/cctype/tolower/
I personally prefer to make only one comparison so I use std::toupper() or std::tolower().
Another bad habit to not get into is usingnamespace std;. :)
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice
Okay, ill stop using that too, my guide just told me too I didn't know it was bad.. I was wondering why you were using std::cout instead. Just seemed a lot longer but it makes sense now. I have a question why
while (true) {
if(Age <= 15){
std::cout << "You are too young to play this \n";
Sleep(1000);
std::cout << "G";
Sleep(200);
std::cout << "A";
Sleep(200);
std::cout << "M";
Sleep(200);
std::cout << "E";
Sleep(200);
std::cout << "O";
Sleep(200);
std::cout << "V";
Sleep(200);
std::cout << "E";
Sleep(200);
std::cout << "R" << endl;
Sleep(1000);
}
else {
std::cout << "That is not a number.";
}
}
Its saying cout is ambiguous? I noticed before without the else if you put something other than a number like "Hello" it would say "You are too young to play this game" How can I fix this to where the player may only enter numbers?