help with looping or restarting the program

hello
im having trouble looping my program

basically i thought of do while loop but i don't know if it's possible in my case
because im making a program with many end (end being game over im making an interactive fiction program where there is a lot of dead ends)
and i would like to put an option in those ends that says something like "play again? [y/n]"

what my code kinda looks like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cout<<"blhablahblahblah"; //some story or whatever
cout<<"choose A or B or C?"; 
cin>>choice1;  ///this is where the player chooses his path or choice idk.
if (choice1=='A')
{
     cout<<"blahblahblah"; ///more of the story the branches out to many paths until a dead end or just finishing the game
    ///more codes here
     cout<<"A. Go further \nB. Go back \n.C. Stay still";  ///now here's the problem for example, choice B & C is a dead end 
     cin>>choice25;
     if (choice25=='A')
     {
         cout<<"blahblahmore storyline";  ///more story and dead ends
     }
     else if (choice25=='B')
     {
         cout<<"blahblahblahh";
         cout<<"Game over";  ///here's where i want the 'restart the game or not' option
     }
     else if (choice25=='C')
     {
         cout<<"asdsadasdhaksjhdsa"; ///also a dead end but they have different outcome so i separated choice B & C
         cout<<"Game over";
     }
Last edited on
closed account (48T7M4Gy)
What about a switch control? It's much clearer what branching is going on.
because i want some sort of fool proof input like when a user inputs an 'a' instead of 'A'
idk if that works in switch xD (we just finished discussing about loops and this is my project for this week)
i just finished coding 2500+ lines last night before someone told me about toupper and all other things and im like f* it xD
right now i just want a little polishing like as i mention above
Last edited on
Something like this, perhaps:

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
bool play_once_again()
{
    std::cout << "play once again (y/n)? " ;
    char again ;
    return std::cin >> again && ( again == 'y' || again == 'Y' ) ;
}

int main()
{
    do
    {
        // play the game once

        // if( the game has ended )
            continue ; // http://en.cppreference.com/w/cpp/language/continue

        // ...

        // if( the game has ended here )
            continue ;

        // ...

    }
    while( play_once_again() ) ;
}
closed account (48T7M4Gy)
So where's the problem?

Anyway they all do the same thing if you incorporate a while loop.

I'm guessing but I get the distinct impression you need to get the logic and flow straightened out which is best done with a pencil and paper diagram/flowchart.
Topic archived. No new replies allowed.