每次我运行代码并故意输入错误的输入以获取输出Please enter a valid choice!
时,它都会执行,但随后程序结束。
这是一个模拟自动售货机的C ++程序。我正在使用Visual Studios。我尝试在开关外使用while循环,使用for循环,使用if语句,以及其他我不太确定的东西(使用我在google上找到的东西)。
#include "pch.h"
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
struct Drinks
{
string name;
double cost;
int number;
};
class Simulation
{
private:
Drinks softDrink[5];
double money;
bool enoughMoney;
int amount;
double total;
void dailyReport();
void inputMoney(int);
public:
Simulation();
void displayChoices();
void buyDrink(int);
~Simulation();
};
Simulation::Simulation()
{
for (int x = 0; x < 5; x++)
{
if (x == 4)
{
softDrink[x].cost = 1.50;
}
else
{
softDrink[x].cost = 1.00;
}
softDrink[x].number = 20;
}
softDrink[0].name = "Cola";
softDrink[1].name = "Root-beer";
softDrink[2].name = "Orange soda";
softDrink[3].name = "Grape soda";
softDrink[4].name = "Bottled water";
money = 0.0;
amount = 0;
total = 0.0;
}
void Simulation::displayChoices()
{
cout << fixed << setprecision(2);
for (int x = 0; x < 5; x++)
{
cout << x + 1 << "." << softDrink[x].name << " : $" << softDrink[x].cost << endl;
}
}
void Simulation::inputMoney(int choice)
{
cout << "The cost for " << softDrink[choice - 1].name << " is $" << softDrink[choice - 1].cost << endl;
cout << "How much money will you insert? ";
cin >> money;
if (money < softDrink[choice - 1].cost)
{
cout << "Can not deliver beverage, not enough money" << endl;
enoughMoney = false;
}
else
{
enoughMoney = true;
}
}
void Simulation::buyDrink(int choice)
{
char purchaseChoice;
inputMoney(choice);
if (enoughMoney)
{
cout << "Do you want to make a purchase for this drink?(Y/N) ";
cin >> purchaseChoice;
while (purchaseChoice != 'Y' && purchaseChoice != 'N')
{
cout << "Please enter correct input, purchase drink (Y/N)? ";
cin >> purchaseChoice;
}
if (purchaseChoice == 'Y')
{
if (softDrink[choice - 1].number <= 0)
{
cout << "We are sold out" << endl;
cout << "Here is your money back $" << money << endl;
}
else
{
cout << "Here is your beverage" << endl;
total += softDrink[choice - 1].cost;
softDrink[choice - 1].number -= 1;
}
if (money > softDrink[choice - 1].cost)
{
money -= softDrink[choice - 1].cost;
cout << "Here is your change $" << money << endl;
}
}
else
{
cout << "Here is your money back $" << money << endl;
}
}
}
Simulation::~Simulation()
{
for (int x = 0; x < 5; x++)
{
cout << "There are " << softDrink[x].number << " " << softDrink[x].name << " left in the machine" << endl;
}
cout << "The total amount of money collected during the day is $" << total << endl;
}
int main()
{
int choice;
string line;
Simulation machine;
cout << fixed << setprecision(2);
do
{
cout << endl << "Drink Machine Menu" << endl;
machine.displayChoices();
cin >> choice;
switch (choice)
{
case 1: machine.buyDrink(choice);
break;
case 2: machine.buyDrink(choice);
break;
case 3: machine.buyDrink(choice);
break;
case 4: machine.buyDrink(choice);
break;
case 5: machine.buyDrink(choice);
break;
default: cout << endl << "Please enter a valid choice!" << endl << endl;
cin >> choice;
}
} while (choice != 6 && choice >= 1 && choice <= 5);
return 0;
}
我希望它不断告诉用户Please enter a valid choice!
,但是它只输出一次然后退出程序
答案 0 :(得分:2)
您可以将while条件更改为
while (choice != 6);
,除非输入6,否则它不会退出循环(如果要在输入时退出)。 其余部分在交换机中处理,您无需再检查一下。