How to go back to a previous IF statement?

How can you make a code go back to a previous point? Ill make an example to help you understand what I mean.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
  #include <string>
 using namespace 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. */
    }
}
Last edited on
closed account (E0p9LyTq)
Line 8 you are using the insertion operator (>>) with cout. That will error.

You could enclose your if statements in a "always true" while statement block, and break out of the code block on a "yes" response.

BTW, you could combine both "yes" queries into a single if statement:
if (response == "yes" || response == "Yes")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

int main()
{
   std::string response;

   while(true)
   {
      std::cout << "Are you ready?";
      std::cin >> response;

      if (response == "yes" || response == "Yes")
      {
         std::cout << "What is your race?\n";
         break;
      }
      else
      {
         std::cout << "Tell me when you are.\n";
      }
   }
}
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!
closed account (E0p9LyTq)
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
Then I could also after I put the
if (response[0] == 'y' || response[0] == 'Y')
Can I put
1
2
3
 else { 
 cout >> "Please put Yes or No.";
}

And that would just fix all mistypes?
Last edited on
closed account (E0p9LyTq)
A good start, I would do it slightly different:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while(true)
{
   std::cout << "Are you ready?";
   std::cin >> response;

   if (std::toupper(response[0]) == 'Y')
   {
      std::cout << "What is your race?\n";
      break;
   }
   else if (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().
Last edited on
Sounds good to me, I should get into the habit of that then and not make bad habits for my start. Thank you :)
closed account (E0p9LyTq)
Another bad habit to not get into is using namespace 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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?
closed account (E0p9LyTq)
Ah, the age old question of "how do I accept only certain types of input from users?" :)

http://www.cplusplus.com/forum/articles/6046/

Long story short, think "stringstream." (Put ALL user input into a string and then "stream" it out)

http://www.cplusplus.com/reference/sstream/basic_stringstream/
Topic archived. No new replies allowed.