【问题标题】:Why do my stream input operations get skipped over?为什么我的流输入操作会被跳过?
【发布时间】:2014-10-22 03:11:57
【问题描述】:

我有这段代码,运行时将在选项列表中显示。我的问题是当我输入数字 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 已经生效。选项 2 是我遇到问题的地方。当我输入数字 2 并单击输入时,它会显示总成本直至项目成本。它应该询问用户总费用,一旦输入,它将进行计算并显示支付的金额。
  • 我试图调试我的项目,它说 cost = ?还有 cin = ?
  • 你听说过switch吗?
  • 我读过但没用过。不知道它是如何工作的

标签: c++ iostream


【解决方案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";`

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

【讨论】:

  • 对不起,我不明白你的意思。我只是一个学生。请详细说明。谢谢
  • @Crazy1923 很公平 - 我已经编辑了我的答案以使其更加“温和” - 希望能帮助你更多。干杯。
猜你喜欢
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
相关资源
最近更新 更多