为什么我的流输入操作会被跳过?

时间:2014-10-22 03:11:57

标签: c++ iostream

我有这个代码,运行时会显示选项列表。我的问题是,当我输入数字2时,选项2程序不能正常工作。它只是直接询问支付金额,而不是先询问购买成本。

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
float circle (float a)
{
    float z;
    z = 3.141593 * (a * a);
    return (z);
}
float square (float b)
{
    float y;
    y = b * b;
    return (y);
}
float rectangle (float c, float d)
{
    float x;
    x = c * d;
    return (x);
}
float triangle (float e, float f)
{
    float w;
    w = (e * f) / 2;
    return (w);
}
void exit ()
{
    cout << "THANK YOU! GOODBYE!" << endl;
}
int main()
{
    int x;
    do
    {

        cout << "Please choose an option below: \n";
        cout << "1. Area of Shapes\n";
        cout << "2. Cost of your items\n";
        cout << "3. Flood Control\n";
        cout << "4. Fibonacci Numbers\n";
        cout << "5. Addition Table\n";
        cout << "6. Exit\n";
        cin >> x;
        if (x == 1)
        {
            system("cls");
            float n;
            float l;
            float m;
            float radius;
            float side;
            float length;
            float width;
            float base;
            float height;

            do
            {
                cout << "1 => Area of Circle" << endl;
                cout << "2 => Area of Square" << endl;
                cout << "3 => Area of Rectangle" << endl;
                cout << "4 => Area of Trian1gle" << endl;
                cout << "5 => Return to Main Menu" << endl;
                cout << "0 => Exit" << endl;
                cout << "Please enter number of your choice: ";
                cin >> n;
                system("cls");
                {
                    if (n == 0)
                    {
                        exit ();
                        system("pause");
                        return 0;
                    }
                    else if (n == 1)
                    {
                        cout << "Enter radius of the circle: ";
                        cin >> radius;
                        l = circle (radius);
                        cout << "Area of the circle is: " << l << endl;
                        system("pause");
                        system("cls");
                    }
                    else if (n == 2)
                    {
                        cout << "Enter side of the square: ";
                        cin >> side;
                        cout << "Area of the square is: " << square (side) << endl;
                        system("pause");
                        system("cls");
                    }
                    else if (n == 3)
                    {
                        cout << "Enter length of the rectangle: ";
                        cin >> length;
                        cout << "Enter width of the rectangle: ";
                        cin >> width;
                        m = rectangle (length, width);
                        cout << "Area of the rectangle is: " << m << endl;
                        system("pause");
                        system("cls");
                    }
                    else if (n == 4)
                    {
                        cout << "Enter base of the triangle: ";
                        cin >> base;
                        cout << "Enter height of the triangle: ";
                        cin >> height;
                        cout << "Area of the triangle is: " << triangle (base, height) << endl;
                        system("pause");
                        system("cls");
                    }
                    else if (n == 5)
                    {
                        exit ();
                    }
                    else
                        cout << "Invalid number. Please enter a valid number below" << endl;
                }
            }
            while (n != 0 && n != 5);
            cout << endl << endl;
            system("pause");
            system("cls");
        }

        else if (x == 2)
        {
            system("cls");
            string mystr;
            float cost = 0;
            float amount = 0;
            float total;

            cout << "Total Cost: P";
            getline (cin, mystr);
            stringstream(mystr) >> cost;
            cout << endl;
            total = cost * .06;
            cout << "Sales Tax Value: P" << total << endl;
            cout << endl;
            cout << "Cost of Item: P" << cost + total << endl;
            cout << endl;
            cout << "Amount Paid: P";
            getline (cin, mystr);
            stringstream(mystr) >> amount;
            cout << endl;
            cout << "Total Amount Purchased: P" << cost << endl;
            cout << "Sales Tax Value: P" << total << endl;
            cout << "Total Amount + Sales Tax: P" << cost + total << endl;
            cout << "Total Amount Paid: P" << amount << endl;
            cout << "Change: P" << amount - (cost + total) << endl;
            system("pause");
            cout << endl;
            cout << "THANK YOU! ENJOY YOUR MEAL!" << endl;


            system("pause");
            system("cls");
        }
        else if (x > 6)
            cout << "Invalid Input";
        else
        {
            system("pause");
            return 0;
        }
    }
    while (x != 6);
    system("pause");
    return 0;
}

修改

海报教育

你做

switch (n) {

  case 1:
    //... Code for n == 1  - If long put into another function. If using local variables put code bloc in braces
    break;
  case 2:
   // Diitto for n==2

  default: // No match
    // All other values of n not listed above
}

1 个答案:

答案 0 :(得分:1)

出了什么问题

假设您输入菜单选项:

2<Enter>

然后std::cin流的内容将是:

2\n

当你的菜单选择运行时......

cin >> x;

...它从行中读取一个数字,但不消耗任何尾随空格和换行符,因此剩余的状态内容可以表示如下:

\n

然后您的菜单选项2的代码开始运行:

        cout << "Total Cost: P";
        getline (cin, mystr);

... getline查看std::cin并找到上面提到的\n左侧,并说“嘿,空行 - 我会设置mystr一个空字符串“。请注意,按照您的意愿执行操作:即等待您输入更多输入并将 读入mystr

如何解决

在致电getline(cin, mystr)之前,您要删除输入菜单选项时输入的左侧\n。代码更改(添加错误处理):

#include <limits>

...
        cout << "Total Cost: P";
        std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
        if (!std::getline(std::cin, mystr))
        {
            std::cerr << "unable to read mystr\n";
            exit(1);
        }
        std::istringstream iss(mystr);
        iss >> cost;
        if (!iss)
        {
             std::cerr << "mystr doesn't contain a valid cost number\n";
             exit(1);
        }

您如何找到问题

当你遇到这样的问题时,尝试添加一些“跟踪”语句来打印出变量的值,并找出它们与你的期望有什么不同......这至少可以让你更好地了解如何隔离和描述问题,以及谷歌要修复它的内容。

std::out << "mystr '" << mystr << "'\n";`

尝试使用像我所说明的错误处理,以便在解析用户输入时出现问题时程序停止(或提示更好的输入)。