如果用户选择“是”,则执行while循环以重启程序

时间:2014-05-14 21:43:49

标签: c++

你好我正在学习c ++,我们还没有用C ++覆盖循环,但我觉得需要进入新的领域,所以我从头开始创建一个类似杂货店的程序,在程序结束时,它要求如果用户想要购买更多,如果他/她键入Y然后重新开始,如果他/她键入N然后它退出。当我到达程序结束并键入Y时,它会一直询问"您想继续购物吗?是[是]或否[N]"。那是什么?

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

char shoppingMenu()
{
    char chrSelect = ' ';

    cout << "%%%%%%%%%%%%" << endl;
    cout << "% Shopping %" << endl;
    cout << "%%%%%%%%%%%%" << endl << endl;
    cout << "Prepared Foods:" << endl;
    cout << "A.) Lettuce" << endl;
    cout << "B.) Noodles" << endl;
    cout << "C.) Bottled Water" << endl;
    //shopSelection = cin.get();
    cin >> chrSelect;
    return toupper(chrSelect);
}
double lettuce(int numWeight, double numQuant)
{
    system("CLS");
    cout << "%%%%%%%%%%%" << endl;
    cout << "% Lettuce %" << endl;
    cout << "%%%%%%%%%%%" << endl;
    cout << "Lbs: ";
    cin >> numWeight;
    cout << endl << "Quantity: ";
    cin >> numQuant;
    return numQuant * (numWeight * .75);
}
double noodles(double numQuant)
{
    system("CLS");
    cout << "%%%%%%%%%%%" << endl;
    cout << "% Noodles %" << endl;
    cout << "%%%%%%%%%%%" << endl << endl;
    cout << endl << "Number of 1/2lb packs: ";
    cin >> numQuant;
    return numQuant * 1.50;
}
double water(char chrSelection, int numQuant)
{
    system("CLS");
    cout << "%%%%%%%%%" << endl;
    cout << "% Water %" << endl;
    cout << "%%%%%%%%%" << endl << endl;
    cout << endl << "A.) 12pk;";
    cout << endl << "B.) 24pk;" << endl;
    cout << "Please enter [A] or [B]" << endl;
    cin >> chrSelection;
    //cout << '\b' << endl;
    chrSelection = toupper(chrSelection);
    cout << "Quantity: ";
    cin >> numQuant;
    if (chrSelection == 'A')
    {
        return numQuant * 1.49;
    }
    else if (chrSelection == 'B')
    {
        return numQuant * 2.49;
    }
}
int main()
{

    char selection = ' ';
    int numberLettuce = 0;
    double lettuceWeight = 0.0;
    int lettuceQuant = 0;
    double lettuceTotal = 0.0;
    int noodleQuant = 0;
    double noodleTotal = 0.0;
    char waterSelection = ' ';
    int waterQuant = 0;
    double waterTotal = 0.0;
    char keepShopping = ' ';

    cout << "#################" << endl;
    cout << "# Grocery Store #" << endl;
    cout << "#################" << endl << endl;
    do {
        cout << "How may i help you?" << endl;
        cout << "A.) Shopping Program" << endl;
        selection = cin.get();
        selection = toupper(selection);
        system("CLS");

        if (selection == 'A')
        {
            char shopSelection = shoppingMenu(); // Display Menu
            cout << "(Selection == " << selection << ")" << endl;

            if (shopSelection == 'A')
            {
                lettuceTotal = lettuce(lettuceWeight, lettuceQuant);
            }
            else if (shopSelection == 'B')
            {
                noodleTotal = noodles(noodleQuant);
            }
            else if (shopSelection == 'C')
            {
                waterTotal = water(waterSelection, waterQuant);
            }
            system("CLS");
            cout.precision(2);
            cout << fixed;
            cout << "   Lettuce total: $" << lettuceTotal << endl;
            cout << "   Noodle total: $" << noodleTotal << endl;
            cout << " + Water total: $" << waterTotal << endl;
            cout << "----------------------------" << endl;
            cout << " = $" << (lettuceTotal + noodleTotal + waterTotal);
        }
        cout << endl << endl << "Do you want to continue shopping? Yes[Y] or No[N]" << endl;
        cin >> keepShopping;
        keepShopping = toupper(keepShopping);
        system("CLS");
    }
    while (keepShopping == 'Y');
    system("pause");
}

2 个答案:

答案 0 :(得分:4)

问题是

cin >> keepShopping;

其中keepShoppingchar。键入 Y 输入时,您键入了两个字符。第一个进入keepShopping并且您的程序正确地循环回到do循环的顶部。然后selection = cin.get()读取输入缓冲区中的下一个字符 Enter keypress,它不是A,因此它会下降到if块的末尾并询问您是否想继续购物。

解决方案是避免使用cin,而是使用getline()

string str;
getline(cin, str);
keepShopping = str[0];

这将消耗您键入的所有内容,包括 Enter 按键,您将无法获得这些奇怪的输入缓冲效果。使用cin >>进行交互式输入确实非常麻烦,如果你避免使用它,你将为自己省去很多麻烦。

答案 1 :(得分:1)

Greg说的是正确的,但你也可以使用getch();但是你需要包含c-header,getch();将只读取键盘上的1个键击,所以如果你点击&#34; y&#34;它会自动识别它而无需按回车。

keepShopping = getch();

我不确定它位于哪个库中,但<stdio.h>等标准c库包含在#include <cstdio>中,然后您可以使用所有标准c函数如果你选择这个解决方案,试试吧。

相关问题