'Cannot get menu selection to run a function. C++
I wish to create code that will call a function from a menu selection. I wish the menu to choose the action of converting kilos and pounds. The menu selection in my current code is not accomplishing this task. How can I get the menu selection to work? I want it to convert kilos to pounds, convert pounds to kilos, and quit the program. The menu selections should loop with the selection of 3 to quit the program.
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void displayMenu();
int getChoice(int min, int max);
double kilosToPounds(double);
double poundsToKilos(double);
/***** main *****/
int main()
{
double weight = 0;
int choice = 0;
displayMenu();
cout << "Please choose a function: ";
cin >> choice;
choice = getChoice(1, 3);
while (choice == 1)
{
double kilosToPounds(weight);
}
while (choice == 2)
{
double kilosToPounds(weight);
}
while (choice == 3);
{
return 0;
}
}
/***** displayMenu *****/
void displayMenu()
{
int padding = 8;
cout << "Program to convert weights:\n\n"
<< right
<< setw(padding) << "" << "1. Convert kilograms to pounds\n"
<< setw(padding) << "" << "2. Convert pounds to kilograms\n"
<< setw(padding) << "" << "3. Quit\n";
}
/***** getChoice *****/
// THIS IS THE SAME FUNCTION YOU WROTE EARLIER IN THIS SET
// OF LAB EXERCISES. JUST FIND IT AND PASTE IT HERE.
int getChoice(int min, int max)
{
int choice;
// Get and validate the input
cin >> choice;
while (choice < min || choice > max)
{
cout << "Invalid input. Enter an choice between 1 & 3: ";
}
return choice;
}
/***** kilosToPounds *****/
double kilosToPounds(double weight)
{
double kilo = weight / 2.2;
cout << "This item weighs " << kilo << " kilos.\n";
return 0;
}
/***** poundsToKilos *****/
double poundsToKilos(double weight)
{
double pounds = weight * 2.2;
cout << "This item weighs " << pounds << " pounds.\n";
return 0;
}
Solution 1:[1]
- Are you sure you want to use a while loop here?
while (choice == 1)
{
double kilosToPounds(weight);
}
- If choice == 1, how and when will you escape the loop?
Also, you might want to do something with the value you are computing:
double weightInPounds = kilosToPounds(weight);
Solution 2:[2]
There are several errors in your code:
- If you already have
getChoicefunction which takes user's input, why are taking user's input before call to thegetChoicefunction?
cout << "Please choose a function: ";
cin >> choice; // <-- not needed
- Why do you use
whileloop if you only want to check for a certain condition? Useifstatement instead.
if (choice == 1) {...}
instead of
while (choice == 1) {...}
- When calling a function, you don't need to specify its return type.
auto pounds = kilosToPounds(weight);
or just
kilosToPounds(weight);
instead of
double kilosToPounds(weight);
Solution 3:[3]
See Below,
cout << "Please choose a function: ";
cin >> choice;
remove the cin>>choice; because you are getting choice from getChoice(int,int); function. so it has no need here.
and In getChoice(int,int) function,
int getChoice(int min, int max)
{
int choice;
// Get and validate the input
cin >> choice;
while (choice < min || choice > max)
{
cout << "Invalid input. Enter an choice between 1 & 3: ";
}
return choice;
}
You have No input in the while loop, So if you enter choice in less than min or grater than max the loop goes infinite because there is no changes in choice variable.So take input in the loop.
In main()
if the choice equal to 1 or 2 it goes to infinite loop because of this
while (choice == 1)..
and In main()
double kilosToPounds(weight);
it is not the way to call the function and you did not have no weight to calculate.
full code:
#include <iostream>
#include <iomanip>
using namespace std;
void displayMenu();
int getChoice(int,int);
double kilosToPounds(double);
double poundsToKilos(double);
int main()
{
double weight = 0;
int choice = 0;
displayMenu();
cout << "Please choose a function: ";
choice = getChoice(1, 3);
if (choice == 1)
{
cin>>weight;
kilosToPounds(weight);
}
if (choice == 2)
{
cin>>weight;
kilosToPounds(weight);
}
if (choice == 3);
{
return 0;
}
}
void displayMenu()
{
int padding = 8;
cout << "Program to convert weights:\n\n"
<< right
<< setw(padding) << "" << "1. Convert kilograms to pounds"<<endl
<< setw(padding) << "" << "2. Convert pounds to kilograms"<<endl
<< setw(padding) << "" << "3. Quit"<<endl;
}
int getChoice(int min, int max)
{
int choice;
cin >> choice;
while (choice < min || choice > max)
{
cout << "Invalid input. Enter an choice between 1 & 3: ";
cin >> choice;
}
return choice;
}
double kilosToPounds(double weight)
{
double kilo = weight / 2.2;
cout << "This item weighs " << kilo << " kilos.\n"<<endl;
return 0;
}
double poundsToKilos(double weight)
{
double pounds = weight * 2.2;
cout << "This item weighs " << pounds << " pounds."<<endl;;
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | robthebloke |
| Solution 2 | NutCracker |
| Solution 3 | srilakshmikanthanp |
