当我输入一串字符时,我的菜单会重复太多次

时间:2016-03-24 16:43:38

标签: c++

所以这是我的代码

do
{
    cout << "Welcome to our Coffee Shop! Here are the options: " << endl;
    cout << "C - Coffee ($1.50)" << endl;
    cout << "T - Tea ($1.00)" << endl;
    cout << "S - Soda ($1.00)" << endl;
    cout << "J - Juice ($1.50)" << endl;
    cout << "M - Manager Special ($2.00)" << endl;
    cout << "X - Finish Order" << endl;
    cout << "What drink would you like? Enter C, T, S, J, M, or X." << endl;
    cin >> input;

    //does certain actions for a certain character
    if(input)
    {
        switch(input)
        {
            case 'C':
            case 'c': total += coffeePrice;
                    coffeeCount++;
                cout << "Thanks! You have ordered coffee." << endl;
                break;
            case 'T':
            case 't': total += teaPrice;
                teaCount++;
                cout << "Thanks! You have ordered tea." << endl;
                break;
            case 'S':
            case 's': total += sodaPrice;
                sodaCount++;
                cout << "Thanks! You have ordered soda." << endl;
                break;
            case 'J':
            case 'j': total += juicePrice;
                juiceCount++;
                cout << "Thanks! You have ordered juice." << endl;
                break;
            case 'M':
            case 'm': total += specialPrice;
                specialCount++;
                cout << "Thanks! You have ordered the manager special." << endl;
                break;

            default:
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                break;

        }
    }
    else
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

    }

我一直在尝试制作一个只读取一个字符的菜单,然后做一些事情。问题是,当我输入一个字符串,其中包含多个c&#34; cccc&#34;菜单重复4次。或者,如果我输入的字符串中包含某些c和其他不正确的响应,例如&#34; ccddcc&#34;菜单会说两次咖啡,然后重复菜单两次,然后再次感谢咖啡两次。有没有办法阻止这种情况发生。我只是想让它忽略除了“c&#39;”之外的任何回复。或正确的菜单选项。

1 个答案:

答案 0 :(得分:0)

我认为这里的问题是,input&#39;是一个字符。因此,当你输入cccc时,它将读入第一个字符并继续前进,返回并看到std输入中还有更多的c等待处理,因此它将接下来的c和进程再次。我建议你将input var设为一个字符串,然后如果该字符串大于1,则保留或只使用第一个字符。

char input[256]; // Could probably be a string, but I am used to C for this
do
{
cout << "Welcome to our Coffee Shop! Here are the options: " << endl;
cout << "C - Coffee ($1.50)" << endl;
cout << "T - Tea ($1.00)" << endl;
cout << "S - Soda ($1.00)" << endl;
cout << "J - Juice ($1.50)" << endl;
cout << "M - Manager Special ($2.00)" << endl;
cout << "X - Finish Order" << endl;
cout << "What drink would you like? Enter C, T, S, J, M, or X." << endl;
cin >> input;

switch(input[0])
{
    case 'C':
    case 'c': total += coffeePrice;
            coffeeCount++;
        cout << "Thanks! You have ordered coffee." << endl;
        break;
    case 'T':
    case 't': total += teaPrice;
        teaCount++;
        cout << "Thanks! You have ordered tea." << endl;
        break;
    case 'S':
    case 's': total += sodaPrice;
        sodaCount++;
        cout << "Thanks! You have ordered soda." << endl;
        break;
    case 'J':
    case 'j': total += juicePrice;
        juiceCount++;
        cout << "Thanks! You have ordered juice." << endl;
        break;
    case 'M':
    case 'm': total += specialPrice;
        specialCount++;
        cout << "Thanks! You have ordered the manager special." << endl;
        break;
    default:
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        break;
    }
}
else
{
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

}