在C ++中使用While循环菜单

时间:2016-07-15 21:57:16

标签: c++

我在为学校工作的程序中执行这个do-while循环菜单时遇到了问题。我已经检查过,就我而言,我已经正确地编写了代码。但是,在测试时,如果我键入' y'或者' n'结果是一样的:菜单流下来100次不停,直到我退出程序。我对我做错了什么以及如何让它每次都能正确显示菜单有任何想法吗?提前致谢。

#include <iostream>
#include <iomanip>
#include <string>
#include "CashRegister.h"
#include "InventoryItem.h"
using namespace std;

int main()
{
// Variables
int selection, numUnits, cont;
double price;

// Use the first constructor for the first item
InventoryItem item1;
item1.setCost(5.0);
item1.setDescription("Adjustable Wrench");
item1.setUnits(10);

// Use the second constructor for the second item
InventoryItem item2("Screwdriver");
item2.setCost(3.0);
item2.setUnits(20);

// Use the third constructor for the remaining items
InventoryItem item3("Pliers", 7.0, 35);
InventoryItem item4("Ratchet", 10.0, 10);
InventoryItem item5("Socket Wrench", 15.0, 7);


do
{
    cout << "#\t" << "Item\t\t\t" << "qty on Hand" << endl;
    cout << "------------------------------------------------------------------" << endl;
    cout << "1\t" << item1.getDescription() << "\t" << setw(3) << item1.getUnits() << endl;
    cout << "2\t" << item2.getDescription() << "\t\t" << setw(3) << item2.getUnits() << endl;
    cout << "3\t" << item3.getDescription() << "\t\t\t" << setw(3) << item3.getUnits() << endl;
    cout << "4\t" << item4.getDescription() << "\t\t\t" << setw(3) << item4.getUnits() << endl;
    cout << "5\t" << item5.getDescription() << "\t\t" << setw(3) << item5.getUnits() << endl;
    cout << "Which item above is being purchased? ";
    cin >> selection;

    // Validate the selection
    while (selection < 1 || selection > 5)
    {
        cout << "Error, please make a valid item selection: ";
        cin >> selection;
    }

    cout << "How many units? ";
    cin >> numUnits;

    // Validate the quantity of units to make sure it isn't a negative value
    while (numUnits < 0)
    {
        cout << "Error, please enter a valid quantity: ";
        cin >> numUnits;
    }

    // Use a switch statement to figure out which cost to pull
    switch (selection)
    {
    case 1: {price = item1.getCost();
        item1.changeUnits(numUnits); }
        break;
    case 2: {price = item2.getCost();
        item2.changeUnits(numUnits); }
        break;
    case 3: {price = item3.getCost();
        item3.changeUnits(numUnits); }
        break;
    case 4: {price = item4.getCost();
        item4.changeUnits(numUnits); }
        break;
    case 5: {price = item5.getCost();
        item5.changeUnits(numUnits); }
        break;
    }

    // Create a CashRegister object for this particular selection
    CashRegister transaction(price, numUnits);

    // Display the totals
    cout << fixed << showpoint << setprecision(2);
    cout << "Subtotal: $" << transaction.getSubtotal() << endl;
    cout << "Sales Tax: $" << transaction.getSalesTax() << endl;
    cout << "Total: $" << transaction.getPurchaseTotal() << endl;

    // Find out if the user wants to purchase another item
    cout << "Do you want to purchase another item? Enter y/n: ";
    cin >> cont;

} while (cont != 'n' && cont != 'N');

system("pause");
return 0;

}

1 个答案:

答案 0 :(得分:1)

除非您明确输入break这是&#39; n&#39;否则您的循环永远不会110 ASCII Codes中的字符或78中的字符“N&#39;因此,将cont声明从int cont;更改为char cont;,然后您再也无法获得无限循环,其条件对可能有效到那时break,除非你有另一个隐藏的逻辑错误,需要你调试它。