Join 136,299 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,450 people online right now. Registration is fast and FREE... Join Now!
I am trying to write a simple program that will let you choose from 3 metric conversions. I don't know what I am doing wrong. It will only let you choose the first option and then what ever is entered after first calculation continues to the next two calculations. I don't know what is missing. I am at "beginner" at best. Any suggestions would be appreciated.
CODE
#include <iostream> using namespace std;
int main() { //declare variables for calculation selections float calc_select, again;
//Ask user to choose cout<< "\n\n What do you want to convert?\n"; cout<< "\n\n Enter:\n"; cout<< " 1 for feet to meters,\n"; cout<< " 2 for gallons to liters,\n"; cout<< " 3 for miles to kilometers.\n"; //store selection cin>> calc_select; //switch (calc_select); { //if calc_select == 1 if (calc_select == 1) cout<< " Enter a number of feet to convert to meters.\n"; cin>> feet; meters = (feet * 0.3048); cout <<feet<< " feet equals " <<meters<< " meters.\n";
//if calc_select = 2 if (calc_select == 2) cout<< " Enter a number of gallons to convert to liters.\n"; cin>> gallons; liters = (gallons * 3.78541178); cout <<gallons<< " gallons equals " <<liters<< " liters.\n";
//if calc_select = 3 if (calc_select == 3) cout<< " Enter a number of miles to convert to meters.\n"; cin>> miles; kilometers = (miles * 1.609344); cout <<miles<< " miles equals " <<kilometers<< " kilometers.\n";
//if calc_select = invalid
//cout<< " Invalid selection! Must enter a 1, 2, 3!\n";
start: cout << "What do you want to convert?\n"; cout << "\n\n Enter:\n"; cout << " 1 for feet to meters,\n"; cout << " 2 for gallons to liters,\n"; cout << " 3 for miles to kilometers.\n"; cout << " Select now: "; cin>> calc_select; cin.ignore(); if (calc_select == 1) { cout<< " Enter a number of feet to convert to meters: "; cin>> feet; cin.ignore(); meters = (feet * 0.3048); cout << "\n"; cout <<feet<< " feet equals " <<meters<< " meters.\n"; goto start; }
else if (calc_select == 2) { cout<< " Enter a number of gallons to convert to liters: "; cin>> gallons; cin.ignore(); liters = (gallons * 3.78541178); cout << "\n"; cout <<gallons<< " gallons equals " <<liters<< " liters.\n"; goto start; }
else if (calc_select == 3) { cout<< " Enter a number of miles to convert to meters: "; cin>> miles; cin.ignore(); kilometers = (miles * 1.609344); cout << "\n"; cout <<miles<< " miles equals " <<kilometers<< " kilometers.\n"; goto start; }
else { cout<< " Invalid selection! Must enter a 1, 2, 3!\n"; }
//Ask user to choose cout<< "\n\n What do you want to convert?\n"; cout<< "\n\n Enter:\n"; cout<< " 1 for feet to meters,\n"; cout<< " 2 for gallons to liters,\n"; cout<< " 3 for miles to kilometers.\n"; //store selection cin >> calc_select; switch ( calc_select ) { case 1: //if calc_select == 1 cout<< "Enter a number of feet to convert to meters.\n"; cin>> feet; meters = (feet * 0.3048); cout <<feet<< " feet equals " <<meters<< " meters.\n"; break; //you must use break if you don't want to execute the instructions //for case 2:
case 2: //if calc_select == 2 cout<< "Enter a number of gallons to convert to liters.\n"; cin>> gallons; liters = (gallons * 3.78541178); cout <<gallons<< " gallons equals " <<liters<< " liters.\n"; break;
case 3: //calc_select == 3 cout<< "Enter a number of miles to convert to meters.\n"; cin>> miles; kilometers = ( miles * 1.609344 ); cout << miles << " miles equals " << kilometers << " kilometers.\n"; break; default: ////calc_select == invalid cout << "Invalid selection! Must enter a 1, 2, 3!\n";
Thanks for your help. That looks and works great! The instructor said we couldn't use "goto statements." Is there a way to loop it back to the main menu without the goto statement?
QUOTE(Korupt @ 1 Sep, 2008 - 08:22 AM)
try this (also added a throughout loop with the start: label and the goto statement)
start: cout << "What do you want to convert?\n"; cout << "\n\n Enter:\n"; cout << " 1 for feet to meters,\n"; cout << " 2 for gallons to liters,\n"; cout << " 3 for miles to kilometers.\n"; cout << " Select now: "; cin>> calc_select; cin.ignore(); if (calc_select == 1) { cout<< " Enter a number of feet to convert to meters: "; cin>> feet; cin.ignore(); meters = (feet * 0.3048); cout << "\n"; cout <<feet<< " feet equals " <<meters<< " meters.\n"; goto start; }
else if (calc_select == 2) { cout<< " Enter a number of gallons to convert to liters: "; cin>> gallons; cin.ignore(); liters = (gallons * 3.78541178); cout << "\n"; cout <<gallons<< " gallons equals " <<liters<< " liters.\n"; goto start; }
else if (calc_select == 3) { cout<< " Enter a number of miles to convert to meters: "; cin>> miles; cin.ignore(); kilometers = (miles * 1.609344); cout << "\n"; cout <<miles<< " miles equals " <<kilometers<< " kilometers.\n"; goto start; }
else { cout<< " Invalid selection! Must enter a 1, 2, 3!\n"; }
cin.get(); }
I initially had the "switch" and "break" in there but I couldn't get it to work. I tried it without, but it didn't work quite right either. Thank you for explaining the changes you made. How do I make this go back to the original menu and let the user make another selection?
QUOTE(sensui @ 1 Sep, 2008 - 08:38 AM)
If you want to use the switch instruction try the following code ( and read the comments ):
cpp
#include <iostream> using namespace std;
int main() { //declare variables for calculation selections int calc_select; //this must be an int not a float if you want to work with switch
//Ask user to choose cout<< "\n\n What do you want to convert?\n"; cout<< "\n\n Enter:\n"; cout<< " 1 for feet to meters,\n"; cout<< " 2 for gallons to liters,\n"; cout<< " 3 for miles to kilometers.\n"; //store selection cin >> calc_select; switch ( calc_select ) { case 1: //if calc_select == 1 cout<< "Enter a number of feet to convert to meters.\n"; cin>> feet; meters = (feet * 0.3048); cout <<feet<< " feet equals " <<meters<< " meters.\n"; break; //you must use break if you don't want to execute the instructions //for case 2:
case 2: //if calc_select == 2 cout<< "Enter a number of gallons to convert to liters.\n"; cin>> gallons; liters = (gallons * 3.78541178); cout <<gallons<< " gallons equals " <<liters<< " liters.\n"; break;
case 3: //calc_select == 3 cout<< "Enter a number of miles to convert to meters.\n"; cin>> miles; kilometers = ( miles * 1.609344 ); cout << miles << " miles equals " << kilometers << " kilometers.\n"; break; default: ////calc_select == invalid cout << "Invalid selection! Must enter a 1, 2, 3!\n";
[I hope this is not a stupid question...but... what does "cin.ignore();" do in this program. I have never seen that or "cin.get();". Can you please explain what that does? Thanks so much!
quote name='sensui' date='1 Sep, 2008 - 08:38 AM' post='410821'] If you want to use the switch instruction try the following code ( and read the comments ):
cpp
#include <iostream> using namespace std;
int main() { //declare variables for calculation selections int calc_select; //this must be an int not a float if you want to work with switch
//Ask user to choose cout<< "\n\n What do you want to convert?\n"; cout<< "\n\n Enter:\n"; cout<< " 1 for feet to meters,\n"; cout<< " 2 for gallons to liters,\n"; cout<< " 3 for miles to kilometers.\n"; //store selection cin >> calc_select; switch ( calc_select ) { case 1: //if calc_select == 1 cout<< "Enter a number of feet to convert to meters.\n"; cin>> feet; meters = (feet * 0.3048); cout <<feet<< " feet equals " <<meters<< " meters.\n"; break; //you must use break if you don't want to execute the instructions //for case 2:
case 2: //if calc_select == 2 cout<< "Enter a number of gallons to convert to liters.\n"; cin>> gallons; liters = (gallons * 3.78541178); cout <<gallons<< " gallons equals " <<liters<< " liters.\n"; break;
case 3: //calc_select == 3 cout<< "Enter a number of miles to convert to meters.\n"; cin>> miles; kilometers = ( miles * 1.609344 ); cout << miles << " miles equals " << kilometers << " kilometers.\n"; break; default: ////calc_select == invalid cout << "Invalid selection! Must enter a 1, 2, 3!\n";
You can put the menu and the switch statement in a do while loop like this:
cpp
do { //Ask user to choose cout<< "\n\n What do you want to convert?\n"; cout<< "\n\n Enter:\n"; cout<< " 1 for feet to meters,\n"; cout<< " 2 for gallons to liters,\n"; cout<< " 3 for miles to kilometers.\n"; //store selection cin >> calc_select; switch ( calc_select ) { case 1: //if calc_select == 1 cout<< "Enter a number of feet to convert to meters.\n"; cin>> feet; meters = (feet * 0.3048); cout <<feet<< " feet equals " <<meters<< " meters.\n"; break; //you must use break if you don't want to execute the instructions //for case 2:
case 2: //if calc_select == 2 cout<< "Enter a number of gallons to convert to liters.\n"; cin>> gallons; liters = (gallons * 3.78541178); cout <<gallons<< " gallons equals " <<liters<< " liters.\n"; break;
case 3: //calc_select == 3 cout<< "Enter a number of miles to convert to meters.\n"; cin>> miles; kilometers = ( miles * 1.609344 ); cout << miles << " miles equals " << kilometers << " kilometers.\n"; break; default: ////calc_select == invalid cout << "Invalid selection! Must enter a 1, 2, 3!\n";
}; } while ( 1 );
Note that to end your application you must click the x button on top right of the window. Or, instead of while( 1 ) you can use a sentinel value to stop the program like this:
Thank you so much for your suggestions!!! Does the sentinel value let the program run until the number of times you enter? 10 in the example? The "do while" loop seems to be what I am looking for. Thanks again for your help!
QUOTE(sensui @ 4 Sep, 2008 - 02:14 AM)
You can put the menu and the switch statement in a do while loop like this:
cpp
do { //Ask user to choose cout<< "\n\n What do you want to convert?\n"; cout<< "\n\n Enter:\n"; cout<< " 1 for feet to meters,\n"; cout<< " 2 for gallons to liters,\n"; cout<< " 3 for miles to kilometers.\n"; //store selection cin >> calc_select; switch ( calc_select ) { case 1: //if calc_select == 1 cout<< "Enter a number of feet to convert to meters.\n"; cin>> feet; meters = (feet * 0.3048); cout <<feet<< " feet equals " <<meters<< " meters.\n"; break; //you must use break if you don't want to execute the instructions //for case 2:
case 2: //if calc_select == 2 cout<< "Enter a number of gallons to convert to liters.\n"; cin>> gallons; liters = (gallons * 3.78541178); cout <<gallons<< " gallons equals " <<liters<< " liters.\n"; break;
case 3: //calc_select == 3 cout<< "Enter a number of miles to convert to meters.\n"; cin>> miles; kilometers = ( miles * 1.609344 ); cout << miles << " miles equals " << kilometers << " kilometers.\n"; break; default: ////calc_select == invalid cout << "Invalid selection! Must enter a 1, 2, 3!\n";
}; } while ( 1 );
Note that to end your application you must click the x button on top right of the window. Or, instead of while( 1 ) you can use a sentinel value to stop the program like this:
I am trying to write a simple program that will let you choose from 3 metric conversions. I don't know what I am doing wrong. It will only let you choose the first option and then what ever is entered after first calculation continues to the next two calculations. I don't know what is missing. I am at "beginner" at best. Any suggestions would be appreciated.
CODE
#include <iostream> using namespace std;
int main() { //declare variables for calculation selections float calc_select, again;
//Ask user to choose cout<< "\n\n What do you want to convert?\n"; cout<< "\n\n Enter:\n"; cout<< " 1 for feet to meters,\n"; cout<< " 2 for gallons to liters,\n"; cout<< " 3 for miles to kilometers.\n"; //store selection cin>> calc_select; //switch (calc_select); { //if calc_select == 1 if (calc_select == 1) cout<< " Enter a number of feet to convert to meters.\n"; cin>> feet; meters = (feet * 0.3048); cout <<feet<< " feet equals " <<meters<< " meters.\n";
//if calc_select = 2 if (calc_select == 2) cout<< " Enter a number of gallons to convert to liters.\n"; cin>> gallons; liters = (gallons * 3.78541178); cout <<gallons<< " gallons equals " <<liters<< " liters.\n";
//if calc_select = 3 if (calc_select == 3) cout<< " Enter a number of miles to convert to meters.\n"; cin>> miles; kilometers = (miles * 1.609344); cout <<miles<< " miles equals " <<kilometers<< " kilometers.\n";
//if calc_select = invalid
//cout<< " Invalid selection! Must enter a 1, 2, 3!\n";
}
}
You forgot the curly brackets {}, so it just carried on into the next if. I would have used if else, and a final else to catch the cases when the user does something unexpected.