(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!! =)
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;
//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.
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;
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!!!