Do You Wish to Continue in C

Press 1 to continue and 2 to quit

I'm learning C++ at school.

But i'm trying to make some own fun stuff, and i want to include

Press1 to continue
Press 2 to quit

But i don't really have an idea.

cout << endl << "Do you want to continue? (Yes/No)" << endl;
cin >> chyesno;

while (chyesno == 'No')
{
cout << endl << "U have chosen to quit the program!" << endl << endl;
break; (what do i need to put here? Break;)
}

I would make a smaller loop inside a large loop body to check for valid choices.
Then exit the appropriate loop on one of the options.

                          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                            running =                            true;                            while                            ( running )                            // Get out your running shoes.                            {                            // Code here!                            bool                            loop =                            true;                            while                            ( loop )   {                            // Here we ask if they want to keep going:                            char                            choice;                            // Use a char.                            // Options                            std::cout <<                            "Enter 1 to continue"                            << std::endl;     std::cout <<                            "Enter 2 to quit"                            << std::endl;     std::cout <<                            "Your choice? "; std::cin >> choice;                            // Now we do some if statement checking                            if                            (choice ==                            '1')       loop =                            false;                            // Gets you out of the {} of the smaller while loop                            else                            if                            (choice ==                            '2')       running =                            false, loop =                            false;                            // end both loops                            else                            std::cout <<                            "\nI don't know what you wanted to do."                            << std::endl << std::endl;                            // repeat smaller loop                            } }                        

Replace break with return 0;

Edit: I would prefer something like this:

                          1
2
3
4
5
6
7
8
                                                      do                            {                            // rest of code here                            char                            choice;     cout <<                            "Do you want to quit?\n[Y]es\t[N]o\n";     cin >> choice; }                            while                            (choice ==                            'y'                            || choice ==                            'Y');                        

It would be easily modifiable if you insist on using numbers.

Last edited on

I tried to do this, but if i chose 2 it doesnt quit the whole program.

return 0; aint working because i used void main() at start

still, many thanks :D

EDIT:

std::cout << "Enter 1 to continue" << std::endl;
std::cout << "Enter 2 to quit" << std::endl;
std::cout << "Your choice? "; std::cin >> choice;

If I press 1 this keeps coming?

Last edited on

Well I only gave you a snippet. If you still see it, its continuing the main loop then hits the mini loop again. Add some text above like a std::cout << "Main Loop" << std::endl;

You should be repeating the main loop everytime you press 1, and then it will ask if you want to do it again once the main loop is completed.

But i mean, when you press 1 --> it will make the triangle it is supposed to make. (not to continue the loop)
and when you press 2 --> is just stops the program

@ browni, thats not really working with my script :(

is that what quit means? To end the program?

I tought that quit was stopping the program

rometotalw wrote:
because i used void main() at start

That's non standard. Never use void main(). You should probably ditch whatever tutorial you're using, because if it's using void main() who knows what other bad/non standard practices it's teaching you as well.

You have the loop as the body of the program right?

If this is how it looks, when you enter 2, it will end the program. I tested it. Works.

                          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
                                                      int                            main() {                            bool                            running =                            true;                            while                            ( running )                            // Get out your running shoes.                            {                            // Code here!                            // Code                            // Code                            // Code                            // Code                            // Code                            // Code                            // Code                            // Code                            // Code                            // Code                            // Code                            // Code                            // Code                            bool                            loop =                            true;                            while                            ( loop )     {                            // Here we ask if they want to keep going:                            char                            choice;                            // Use a char.                            // Options                            std::cout <<                            "Enter 1 to continue"                            << std::endl;       std::cout <<                            "Enter 2 to quit"                            << std::endl;       std::cout <<                            "Your choice? "; std::cin >> choice;                            // Now we do some if statement checking                            if                            (choice ==                            '1')         loop =                            false;                            // Gets you out of the {} of the smaller while loop                            else                            if                            (choice ==                            '2')         running =                            false, loop =                            false;                            // end both loops                            else                            std::cout <<                            "\nI don't know what you wanted to do."                            << std::endl << std::endl;                            // repeat smaller loop                            }                            // end of while (loop)                            }                            // end of while (running)                            // DO NOT CODE HERE UNLESS YOU ONLY WANT TO SEE THIS WHEN THE PROGRAM CLOSES.                            return                            0;                            // END                            }                        

I'm sorry, it aint working with me.

If you want i can PM you my code. (it's in dutch) but maybe you can see what's wrong?

and where you typ "do not code here unless you only want to see this when the program closes"
I must code there something.

EDIT: i changed it a little, now it works. I removed the loop because whenever i typ 1 it will continue BUT it will also start all over again.

Last edited on

Topic archived. No new replies allowed.

johnsonforengs.blogspot.com

Source: https://cplusplus.com/forum/beginner/34810/

0 Response to "Do You Wish to Continue in C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel