I want to do a loop as an alternative for GOTO,please help

Write your question here.
Hey everyone. Im very new to programming and Im trying to create a text adventure game.

Basically they get two options. choose A or B. I want them to also be able to literally write anything else, but for the program to say "sorry no can do" and return them to where they had to choose between A and B. Im managed to do it slighly with a loop but somehow I also made it ask the question again when they did answer A or B. You can find my code down there, Ive never used GOTO btw because I know it sucks, how someone help me re-form my code to what I need? thank you!! (the optionObject is the options the user can choose from, Ive put them in in a class in another file)

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include "Options.h"
#include <string>

using namespace std;

int main()
{

    cout << "\tWelcome to my text adventure game\n" << endl;
    cout << "Please enter your characters Name: ";
    string name;
    cin >> name;
    cout  << "\nWelcome " << name << endl;
    cout << "\nPlease choose the side you would like to play in - ";

    Options OptionsObject;
    OptionsObject.optionsSide();

    string answer;
    cin >> answer;



    if (answer == "1"){

cout << "You chose the good side. Let the game begin\n "  << endl;
cout << "You are located in a city named after the warrior who saved it from the evil\nmany years ago. The city of Redshore. " << endl;

cout << "You are no ordinary man in the City of Redshore. You are the king who rules it\nYou are seen as King of Justice, a good king.\nOne who only want what is best for his people" << endl;

cout << "but also a troubled man. You experienced something traumatizing when you\nwere a just a little boy, but no one knows about it,\nno one but yourself that is  " << endl;
   
 }

    else if(answer == "2"){

        cout << "hey there" << endl;

    }
}
21
22
23
24
25
26
    do
    {   cin >> answer;
        if (answer != "1" && answer != "2")
            cout << "No can do" << endl;
    }
    while (answer != "1" && answer != "2");

That almost fixed it all thank you! take a look now:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include "Options.h"
#include <string>

using namespace std;

int main()
{

    cout << "\tWelcome to my text adventure game\n" << endl;
    cout << "Please enter your characters Name: ";
    string name;
    cin >> name;
    cout  << "\nWelcome " << name << endl;
    cout << "\nPlease choose the side you would like to play in - ";
    Options OptionsObject;
    OptionsObject.optionsSide();

    string answer;



do
    {   cin >> answer;
        if (answer != "1" && answer != "2")
            cout << "That is not a valid Number. " << endl;
    }
    while (answer != "1" && answer != "2");


    if (answer == "1"){

cout << "You chose the good side. Let the game begin\n "  << endl;
cout << "You are located in a city named after the warrior who saved it from the evil\nmany years ago. The city of Redshore. " << endl;
cout << "You are no ordinary man in the City of Redshore. You are the king who rules it\nYou are seen as King of Justice, a good king.\nOne who only want what is best for his people" << endl;
cout << "but also a troubled man. You experienced something traumatizing when you\nwere a just a little boy, but no one knows about it,\nno one but yourself that is  " << endl;
    }

    else if(answer == "2"){

        cout << "hey there" << endl;


}}


when I write any other number it does say "its invalid" and then you can type whatever you want until you actually type 1 or 2 to proceed. but Is there a way where when the user types something other than 1 or 2, it first says thats an invalid number, but then takes me to the question, or asks it again somehow?
Move line 15 to line 24.
while that did great difference, it did just mess up 1 thing. It also appears when the user enters teh correct answers, which is the number 1 or 2, and we only want it for all the other numbers, Im sorry for asking alot but any idea how to fix that?


It also appears when the user enters teh correct answers, which is the number 1 or 2, and we only want it for all the other numbers,

Sorry, not understanding the problem.

If you moved the line as I suggested, The prompt "Please choose the side you would like to play in" should appear once before the users enters an answer. If the answer is 1 or 2, it should not appear again. If the user enters some other answer, then the prompt should be repeated before accepting another input from the user.

1
2
3
4
5
6
7
do
    {   cout << "\nPlease choose the side you would like to play in - ";
        cin >> answer;
        if (answer != "1" && answer != "2")
            cout << "That is not a valid Number. " << endl;
    }
    while (answer != "1" && answer != "2");


This is the current code.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include "Options.h"
#include <string>

using namespace std;

int main()
{

    cout << "\tWelcome to my text adventure game\n" << endl;
    cout << "Please enter your characters Name: ";
    string name;
    cin >> name;
    cout  << "\nWelcome " << name << endl;
    cout << "\nPlease choose the side you would like to play in - ";
    Options OptionsObject;
    OptionsObject.optionsSide();

    int answer;


   do
    {
        cin >> answer;

        if (answer != 1 && answer != 2)
    cout << "That is not a valid Number.\n " << endl;


    }
    while (answer != 1 && answer != 2);

switch (answer) {
case 1:
    cout << "hewkyweyewy" << endl;
    break;

case 2:
    cout << "2000000000" << endl;
    break;

default:
   cout << "heyoo heyoo heyoo heyoo" << endl;


}


When the user types something other than 1 or 2, the screen will show row 27 and they will then have a chance to write another number which is the way I want it. but here is teh thing, I also want to add the previous question everytime they enter a false number, along with "not a valid number", which is in : OptionsObject.optionsSide();
and when I put that in row 28, only row 28 starts appearing in the message EVEN if the user types teh right answer 1 or 2. but I want it like row 27, to only appear when the user types any other number than 1 or 2. I feel like thats kinda weird, what can be wrong? (also thank you very much)
I also want to add the previous question everytime they enter a false number

I already showed you how to do that in my previous post, but you have chosen to ignore that advice.

BTW, you will never get to the default branch of your switch statement since you can never get past line 31 with a value other than 1 or 2.
Im sorry, I didnt ignore it, I just understood it wrong. I copied line 15 and just posted it in line 24, but I should have removed it from 15 and put it in 24 and now it works like a charm. thank you very much <3
Topic archived. No new replies allowed.