switch case stament with loops

hello,

my program is running a case/switch statement, I want to be able to make it loops and ask the user to either continue press yes/no.

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
48
49
50
51
52
53
54
# include <iostream>
#include <string>

using namespace std;

int main ()
{
	int choice, s;
	string name, title;

    cout<<"********* Welcome to my program **********"<<endl;
    
    
    cout<<"\t 1: name"<<endl;
    cout<<"\t 2: title"<<endl;
    cout<<"\t 3: speed"<<endl;
    cout<<"\t 4: Exit"<<endl;
    cout<<endl;
    cout<<"Please make your selection: "; 
    cin>>choice;
    cout<<endl;
    cin.get();

    if (choice<1 || choice>=5)
    {
        cout<<"You have enetered an invalid option, please try again..."<<endl;  
    }
    if  (choice == 4)  {exit(EXIT_FAILURE) ;}

   
    switch (choice)    
    {
        case 1: 
                cout<<"Please enter the  name: ";    
               getline (cin,name);
                break;
        case 2: cout<<"Please enter the title : ";
               getline (cin,title);
            break;
        case 3: cout<<"Please enter the speed (1-100): ";
                      cin>>s;
             break;
   
        }

    cout<<"Thank you  for playing "<<endl;
    cin.get();cin.get();

	return 0;
}





I want somewhere at the end to prompt the uset to continue press y or n
Last edited on
You'll want a do-while loop.
1
2
3
4
do
{
    //Code here.
} while (selection == 'y' || selection == 'Y')


Does this snippet help?

-Albatross
Do i need to identify selection as character?
No. You could declare it as an std::string... eh... it would be a good idea to declare it as a character, yes, if the only thing that will get the loop to rerun is a y or Y response from the user. If you want to also be able to type "Yes" or longer phrases to get it to rerun, then std::string.

-Albatross
Last edited on
I did this but still not what I want
Last edited on
Lines 50 and 51 should be inside your do-while loop. :)

-Albatross
// error C2061: syntax error : identifier 'cout'
}

} cout<<"do you want to continiue ? Yes or no";
cin>>selection;

while (selection =='y' || selection =='Y')
Last edited on
When I said inside the loop I said and meant inside the brackets. Oops. >_>

-Albatross
how about if I want to add No, for example, wanna continue? yes or no ( Y or N)
By default, the loop quits if you type anything other than Y and y. But, if you want the whole program to exit if you type N or n and bypass anything outside the loop, try returning 0 inside an if statement just outside the loop (after the while()).

-Albatross
Last edited on
Got it, thanks Albatross, you are the best :-)
Topic archived. No new replies allowed.