См. Ниже,
cout << "Please choose a function: ";
cin >> choice;
удалить выбор cin >>; потому что вы получаете выбор из getChoice (int, int); функция. так что здесь это не нужно.
и функция getChoice (int, int),
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;
}
У вас нет ввода в то время как l oop, поэтому, если вы введете выбор в меньше чем мин или больше, чем максимум, l oop становится бесконечным, потому что нет никаких изменений в переменной выбора. Так что берите ввод в l oop.
В main ()
, если выбор, равный 1 или 2, идет к бесконечному l oop, из-за этого
while (choice == 1)..
и в main ()
double kilosToPounds(weight);
это не способ вызова функции и у вас не было веса для вычисления.
полный код:
#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;
}