0

私のプログラムは正しい形式で実行されますが、ユーザーが 6 を入力して終了するまでプログラムを実行できるように、すべてをループに入れることになっています。ユーザーが購入するたびに、マシン内の飲み物から1が差し引かれることになっています。売り切れの場合は「sold out」となります。したがって、ループが繰り返され、マシンが獲得した合計金額が表示されます。プログラム全体を while ループに入れる方法がわかりません (私は推測しています) このように助けてください

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;


struct Drink
{
    string drinkName;
    double cost;
    int numberInMachine;
};

struct Drink options[] = {{"Cola", .75, 0}, {"Root Beer", .75, 2},
                      {"Lemon-Lime", .75, 10},
                      {"Grape Soda", .80, 3}, {"Cream Soda", .80, 20}};

int getChoice();
double showTransaction(int);

int main()
{
    int choice;
    double moneyEarned = 0.0;

    choice = getChoice();
    moneyEarned = showTransaction(choice);

    cout <<"The machine earned: $" << moneyEarned << endl;


    //getChoice();
    //showTransaction(options, NUM_DRINKS, choice);


    system("pause");
    return 0;
}

int getChoice()
{
    int choice;

    cout << "Enter the number(1-6) of the drink you would like: " << endl;
    cout << "Drink Name         Cost            " << endl; 
    cout << "1. Cola            .75             " << endl;
    cout << "2. Root Beer       .75             " << endl;
    cout << "3. Lemon-lime      .75             " << endl;
    cout << "4. Grape Soda      .80             " << endl;
    cout << "5. Cream Soda      .80             " << endl;
    cout << "6. Quit " << endl;

    cout << " Enter the number of your selection: ";
    cin >> choice;

    while(choice != 1 && choice != 2 && choice !=3 && choice != 4 
                      && choice != 5 && choice != 6)
    {
            cout << "Please enter a valid number 1-6" << endl;
            cin >> choice;
    }

    return choice;
}
double showTransaction(int choice) 
{
    double moneyIn;

    if(options[choice - 1].numberInMachine < 1)
    {
        return 0.0;
    }

    cout << options[choice - 1].drinkName << "costs $" 
         << options[choice - 1].cost << endl;
    cout << "Enter money inserted up to $1.00: ";
    cin >> moneyIn;
    while(moneyIn < options[choice - 1].cost)
    {
        cout << "The money entered is not enough, please enter more: ";
        cin >> moneyIn;
    }
    cout << "Your change is: $" << (moneyIn - options[choice - 1].cost) 
                 << endl;

    return moneyIn;

}
4

1 に答える 1