Need some code to restart script if something is typed

I am making a text based game where the player is displayed with some text and a couple of choices. Here is the script:

#include <iostream>
using namespace std;

void choice1();

int homies;

int main()
{
int choice;

cout << "What's good my homie we is starting now\n\n";
cout << "How many homies you got ";
cin >> homies;

cout << "You was walking down the hood with your " << homies << " homies.\n";
cout << "You spot your rivals.\n What do you do\n 1) Run\n 2) Fight them\n";
cin >> choice;

if (choice == 1)
{
cout << "You run but your rivals shoot you and your " << homies << " homies.\n";
}
else if (choice == 2)
{
cout << "You ain't no wimp. You shoot at them with your " << homies << " homies.\n";
cout << "You win and you gain respect from all your other rivals and people.\n";

choice1();
}
else if (choice >= 3)
{
cout << "Don't play me like that.\n";
}
return 0;
}

void choice1()
{
int choice;

cout << "You just chillin at your crib with your " << homies << " homies.\n";
cout << "All of a sudden a car does a drive by on you and yo homies. What do you do?\n";
}

In the int main() I want to do something that if i choose 1 or 3+, the program goes back to the beginning of int main(). And also the same but if i choose the wrong answer in choice1() it goes back to int main(). I know that there is slang and improper grammar, it's supposed to be like that.
Last edited on
if-else if-else blocks or switch
Use a loop. Here is an example of a couple of ways of doing it put together:
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
47
#include <iostream>

bool choice1();

int main() {
    int choice;
    std::cout << "... (get choice): ";
   
    // set a variable to test if we are quitting yet
    bool quit = false;

    // while we aren't, loop
    while (!quit) {
        std::cin >> choice;
        switch(choice) {
            case 2:
                std::cout << "You won\n";
                quit = choice1();
            default:
                std::cout << "You lost, try again\n";
        }
    }

    std::cout << "Finish!";
    return 0;
}

bool choice1() {
    int choice;
    std::cout << "... (get choice)";
    std::cin >> choice;

    // while invalid choice options
    while (choice < 1 || choice > 2) {
        std::cout << "Invalid option. (get choice):"
        std::cin >> choice;
    }

    if (choice == 1) {
        std::cout << "You lost...\n";
        return false; // signal a loss
    } else {
        std::cout << "You Won!\n";
        // either keep adding choices or do this to finish:
        return true;
    }
}


However, as you can see, this requires you to copy a bit of code every time you add some more to the program. A better way would be a data-driven approach: Have a function to do all of this by taking in a list of options, the messages for each option and the ID of the next option to go to, which can all be accessed through an array.
Thanks for your answers but my main question is not solved yet.
Last edited on
What do you mean your question isn't solved yet? Did you try my code? You could probably move the display for the text inside the while loop, but apart from that and changing the text to what you wanted that is exactly what you wanted (unless I misunderstood your question).
Sorry, I forgot to explain. I tried your code bit the thing is that I can keep typing a choice and the answer will keep popping up. Another Thing is that when I type a correct choice,, it displays the text the text for both the right and wrong choice
I figured this out myself now. If someone reading this is having the same problem, Create a function. IF you don't know how to do that then this is how.


#include <iostream>

using namespace std;

void function1(); //You need this for your function to work


int main()
{
cout << " Stuff happens.";

function1(); //tells the computer to do what is in that function

return 0;
}

void function1() // The actual function and what it does
{
cout << " This is function1() being displayed now";
}


Last edited on
that isnt a very good explanation. i would look at the documentation on this site
how is it not a good explanation.
because that doesnt explain how functions work

edit: also you are wrong. you don't need line 5 for the function to work
Last edited on
This thread is terrible. If someone reading this is having the same problem, then you won't find any useful answers here.

@ OP: That is a terrible explanation of functions because it doesn't explain anything. For instance WHY do you need Line 5? Would it work if you put it higher in the file? You were using a function before that post, so what you have typed doesn't make any sense.

@ Little Bobby Tables: Yes, in this context he does so need Line 5. Otherwise the function would need to be defined before it is called.

@ NT3: Your program flow has redundancies and you forgot your "break" statement at the end of your first switch case.
Last edited on
i tried to help him more but i didnt understand what op was asking. and i was more making the point of how he didnt explain prototypes. i wasnt critiquing his code. and actually the function does work without line 5. the compiler just cant find it.
Topic archived. No new replies allowed.