i have following code done, now after i menu item is chosen i want to be redirected to preform the actual conversion, Can some give example of how do that thanks.
lgonzo 0 Light Poster
#include <iostream>
using namespace std;
int ShowMenu();
int main()
{
int choice = 1;
// end if user picks 4 or with invalid entry, continue program otherwise
while ( choice != 9 )
{
do {
if (choice > 9 || choice <= 0)
{
cout << "\nAi da da! we don't have that option. Please select again. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
}
choice = ShowMenu();
} while ( choice < 0 || choice > 9 );
if ( choice != 9 )
switch (choice) // branch to an appropriate selection
{
case 1:
cout << "\nOption #1 was selected. distance english to metric. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 2:
cout << "\nOption #1 was selected. distance metric to english. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 3:
cout << "\nOption #2 was selected. weight english to metric. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 4:
cout << "\nOption #1 was selected. weight metric to english. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 5:
cout << "\nOption #3 was selected. volume english to metric. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 6:
cout << "\nOption #1 was selected. weight metric to english. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 7:
cout << "\nOption #4 was selected. pressure english to metric. \n";
cout << "\nPress <Enter> key to continue ...";
fflush(stdin);
cin.get();
break;
case 8:
cout << "\nOption #1 was selected. pressure metric to english. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
}
else //choice 9 was selected
{
cout << "\nprogram is over. Thanks for using.\n";
cout << "\nPress <Enter> key to end the program. ";
}
} // end of while
fflush(stdin);
cin.get();
return 0;
} // end of main
int ShowMenu()
{
int choice;
system("cls");
cout << " M E N U\n";
cout << " = = = =\n";
cout << " 1 distance english to metric.\n";
cout << " 2 distance metric to english.\n";
cout << " 3 weight english to metric.\n";
cout << " 4 weight metric to english.\n";
cout << " 5 volume english to metric.\n";
cout << " 6 volume metric to english.\n";
cout << " 7 pressure english to metric.\n";
cout << " 8 pressure metric to english.\n";
cout << " 9 End program \n";
cout << "\nEnter your choice: ";
while (!(cin >> choice) )
{
cout << "\nAre you drunk? that's invalid option."
<< "\nRe-Enter your choice: ";
cin.clear();
fflush(stdin);
}
return choice;
} // end function ShowMenu
kvprajapati 1,826 Posting Genius Team Colleague
>i want to be redirected to preform the actual conversion
Ask specific questions, don't ask people to do your homework.
mrnutty 761 Senior Poster
I'm gonna post your code here :
#include <iostream>
using namespace std;
int ShowMenu();
int main()
{
int choice = 1;
// end if user picks 4 or with invalid entry, continue program otherwise
while ( choice != 9 )
{
do {
if (choice > 9 || choice <= 0)
{
cout << "\nAi da da! we don't have that option. Please select again. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
}
choice = ShowMenu();
} while ( choice < 0 || choice > 9 );
if ( choice != 9 )
switch (choice) // branch to an appropriate selection
{
case 1:
cout << "\nOption #1 was selected. distance english to metric. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 2:
cout << "\nOption #1 was selected. distance metric to english. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 3:
cout << "\nOption #2 was selected. weight english to metric. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 4:
cout << "\nOption #1 was selected. weight metric to english. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 5:
cout << "\nOption #3 was selected. volume english to metric. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 6:
cout << "\nOption #1 was selected. weight metric to english. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 7:
cout << "\nOption #4 was selected. pressure english to metric. \n";
cout << "\nPress <Enter> key to continue ...";
fflush(stdin);
cin.get();
break;
case 8:
cout << "\nOption #1 was selected. pressure metric to english. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
}
else //choice 9 was selected
{
cout << "\nprogram is over. Thanks for using.\n";
cout << "\nPress <Enter> key to end the program. ";
}
} // end of while
fflush(stdin);
cin.get();
return 0;
} // end of main
int ShowMenu()
{
int choice;
system("cls");
cout << " M E N U\n";
cout << " = = = =\n";
cout << " 1 distance english to metric.\n";
cout << " 2 distance metric to english.\n";
cout << " 3 weight english to metric.\n";
cout << " 4 weight metric to english.\n";
cout << " 5 volume english to metric.\n";
cout << " 6 volume metric to english.\n";
cout << " 7 pressure english to metric.\n";
cout << " 8 pressure metric to english.\n";
cout << " 9 End program \n";
cout << "\nEnter your choice: ";
while (!(cin >> choice) )
{
cout << "\nAre you drunk? that's invalid option."
<< "\nRe-Enter your choice: ";
cin.clear();
fflush(stdin);
}
return choice;
} // end function ShowMenu
All you have to do now is make some function that does the calculation
and conversions. Then call it in the switch statement.
For example , something like this should work :
switch(getUserMenuChoice()){
case START_GAME : startGame(); break;
case END_GAME : cleanUpGame(); quitGame(); break;
//and so on
}
The all capital letters are some consts , for example it could be :
const int START_GAME = 0;
const int END_GAME = -1;
It helps make the code better to read and easier to understand. You should do something similar.
And the startGame() , cleanUpGame() and quitGame() are function.
You can do something similar. Also after you get the code working,
make your program better by removing redundant things. I can
see a few already in your code.
Edited by mrnutty because: n/a
lgonzo 0 Light Poster
>i want to be redirected to preform the actual conversion
Ask specific questions, don't ask people to do your homework.
lol dont want ppl do it for me, want an explanation to get me started
Edited by lgonzo because: n/a
lgonzo 0 Light Poster
thanks alot, very helpful.
im new to c++ and trying to cut down redundant statements
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.