How to restart the program ?

(UPDATED) hey guys this is my code everything is working, but I want to go the extra mile, I would like for my program to restart automatically in case the user makes the wrong choice, also I would like if the user enters a distance less than zero , to tell the user that , that can't be done, that needs to put a number greater than zero try again, thanks I am a noob in c++ but looking to go the extra mile, thank guys!! =)




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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
int choice;//Menu Choice
double feet; //distance the sound will travel
double total; //answear !! 


//Dysplaying the menu and getting the users choice
    do {
	cout << " Welcome to Sound Wave Calculator"<<endl;
	cout << " Please select the medium which the sound would travel"<<endl;
	cout << "1. Through Air \n";
	cout << "2. Through Water\n";
	cout << "3. Through Steel\n";
	cout << "4. Quit the Program\n\n";
	cout << "Enter your choice: ";
	cin  >> choice;
}while ((choice < 1) || (choice > 4  ));
    
    // numeric output formatting
	cout << fixed << showpoint << setprecision(4);
	
	
//use the user choice to perform the correct set of actions 
        if (choice== 1)
	{	cout << "How far will the sound wave travel (in feet)? \n";
		cin  >> feet;
		total=  feet/1100;  
		cout << "The sound wave will travel " <<total<<" Seconds"<<endl;
		cout << "Goodbye"<<endl;
	}
		
	    else if (choice == 2)
     {	cout << "How far will the sound wave travel (in feet)? \n";
		cin  >> feet;
		total=  feet/4900;  
		cout << "The sound wave will travel " <<total<<" Seconds"<<endl;
		cout << "Goodbye"<<endl;
	}
	     
        else if (choice == 3)
     {	cout << "How far will the sound wave travel (in feet)? \n";
		cin  >> feet;
		total=  feet/16400;  
		cout << "The sound wave will travel " <<total<<" Seconds"<<endl;
		cout << "Goodbye"<<endl;
	}
        
        
        //quiting program
        else if (choice == 4)
	                    
	{	cout << "GOOD BYE"<<endl;  
		  
    
    }
   
    //program ended


    
    
    system("pause");
    return 0;
}
 
Last edited on

you need a while loop to loop through your code

while (choice > 0 && choice < 5)
{
//Dysplaying the menu and getting the users choice
cout << " Welcome to Sound Wave Calculator"<<endl;
cout << " Please select the medium which the sound would travel"<<endl;
cout << "1. Through Air \n";
cout << "2. Through Water\n";
cout << "3. Through Steel\n";
cout << "4. Quit the Program\n\n";
cout << "Enter your choice: ";
cin >> choice;

// numeric output formatting
cout << fixed << showpoint << setprecision(4);

//use the user choice to perform the correct set of actions
if (choice == 1)
{ cout << "How far will the sound wave travel (in feet)? \n";
cin >> feet;
total= feet/1100;
cout << "The sound wave will travel " <<total<<" Seconds"<<endl;
cout << "Goodbye"<<endl;
}

else if (choice == 2)
{ cout << "How far will the sound wave travel (in feet)? \n";
cin >> feet;
total= feet/4900;
cout << "The sound wave will travel " <<total<<" Seconds"<<endl;
cout << "Goodbye"<<endl;
}

else if (choice == 3)
{ cout << "How far will the sound wave travel (in feet)? \n";
cin >> feet;
total= feet/16400;
cout << "The sound wave will travel " <<total<<" Seconds"<<endl;
cout << "Goodbye"<<endl;
}
else if (choice == 4)

{ cout << "The valid choices are 1 through 4. \n"
<< "Please run the program again and select one of those.\n";


}

This would loop through your choices as long as choice is valid.
Thanks, this is what I did it worked, where should I put the
cout<<
to let the user know it was an invalid choice before it loops??




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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
int choice;//Menu Choice
double feet; //distance the sound will travel
double total; //answear !! 


//Dysplaying the menu and getting the users choice
    do {
	cout << " Welcome to Sound Wave Calculator"<<endl;
	cout << " Please select the medium which the sound would travel"<<endl;
	cout << "1. Through Air \n";
	cout << "2. Through Water\n";
	cout << "3. Through Steel\n";
	cout << "4. Quit the Program\n\n";
	cout << "Enter your choice: ";
	cin  >> choice;
}while ((choice < 1) || (choice > 4  ));
    
    // numeric output formatting
	cout << fixed << showpoint << setprecision(4);
	
	
//use the user choice to perform the correct set of actions 
        if (choice== 1)
	{	cout << "How far will the sound wave travel (in feet)? \n";
		cin  >> feet;
		total=  feet/1100;  
		cout << "The sound wave will travel " <<total<<" Seconds"<<endl;
		cout << "Goodbye"<<endl;
	}
		
	    else if (choice == 2)
     {	cout << "How far will the sound wave travel (in feet)? \n";
		cin  >> feet;
		total=  feet/4900;  
		cout << "The sound wave will travel " <<total<<" Seconds"<<endl;
		cout << "Goodbye"<<endl;
	}
	     
        else if (choice == 3)
     {	cout << "How far will the sound wave travel (in feet)? \n";
		cin  >> feet;
		total=  feet/16400;  
		cout << "The sound wave will travel " <<total<<" Seconds"<<endl;
		cout << "Goodbye"<<endl;
	}
        
        
        //quiting program
        else if (choice == 4)
	                    
	{	cout << "GOOD BYE"<<endl;  
		  
    
    }
   
    //program ended


    
    
    system("pause");
    return 0;
}
 




This'll work:


do {
cout << " Welcome to Sound Wave Calculator"<<endl;
cout << " Please select the medium which the sound would travel"<<endl;
cout << "1. Through Air \n";
cout << "2. Through Water\n";
cout << "3. Through Steel\n";
cout << "4. Quit the Program\n\n";
cout << "Enter your choice: ";
cin >> choice

if (choice < 1 || choice > 4)
cout << "please enter choice between 1 and 4" << endl;

}while ((choice < 1) || (choice > 4 ));


that worked perfect thanks, I feel bad to ask since you have helped me a lot, but the last thing I would like to do is for the program not to accept distances less than 0!! thanks!!!
Topic archived. No new replies allowed.