我将如何处理用C ++编写的计算器中的字母

时间:2017-09-06 00:52:18

标签: c++

嘿伙计们已经没有那么长的编程所以我不是很好而且我现在刚刚进入错误处理和异常等事情就像刚刚开始学习c ++一样,这与我学到的其他知识有点不同我想知道我是否能在这里得到一些帮助。

我开始制作一个计算器并且它运行良好但如果我输入一个字母而不是一个数字,例如如果我输入+ 1我想要它告诉我必须输入数字或类似的东西......我已经提前知道我的代码可能很乱,所以接受批评不会伤害我的感情。但无论如何,这是我到目前为止所拥有的

int main()
{

    double num1;
    double num2;
    char operation;
    char again;

    cout << "Welcome to Brent's Calculator.\n";
    cout << "Enter 'Value (operator) Value' ex. 2 + 2" << endl;
    cin >> num1 >> operation >> num2;
        cout << num1 << " "
            << operation << " "
            << num2 << " = ";

        do {
            switch (operation) {
            case '+':
                cout << num1 + num2 << endl;
                break;
            case '-':
                cout << num1 - num2 << endl;
                break;
            case '*':
            case 'x':
            case 'X':
                cout << num1 * num2 << endl;
                break;
            case '/':
            case '%':
                if (num2 == 0) {
                    cout << "You cant divide by 0" << endl;
                }
                else {
                    cout << num1 / num2 << endl;
                }
                break;
            default:
                cout << "not understood";
            }
            cout << "would you like to enter another calculation? (y/n) ";
            cin >> again;
            cin >> num1 >> operation >> num2;
            if (cin.fail()) {
                cout << "you need to enter numbers" << endl; //this part added mess with it tomorrow
                cin.clear();
                cin.ignore(999, '/n');
            }

            cout << num1 << " "
                << operation << " "
                << num2 << " = ";


        } while (again == 'y' || again == 'Y');

     //closing bracket of the else for cin.fail
    cin.get();
    return 0;
}

当然我对cin.fail的if语句不起作用

1 个答案:

答案 0 :(得分:1)

你的cin.fail因为小错误而无法正常工作

  

多字符字符常量

cin.ignore(999, '/n');

应该是

    cin.ignore(999, '\n');

更正后的代码

好了又试了一个解决方案

所以我的想法是使用continue ..并将num1和num2的值变为无效值(仅为&#39; +&#39;提供sol)。我使用了NULL但你可以使用你想要的任何其他乱码值

#include<iostream>
#include <math.h>       /* isnan, sqrt */    

using namespace std;

int main()
{

    double num1;
    double num2;
    char operation;
    char again;

    cout << "Welcome to Brent's Calculator.\n";
    cout << "Enter 'Value (operator) Value' ex. 2 + 2" << endl;
    cin >> num1 >> operation >> num2;
    cout << num1 << " "
    << operation << " "
    << num2 << " = ";

    do {
        switch (operation) {
            case '+':
                if ((num1 != NULL) && (num2 != NULL ))
                cout << num1+ num2 << endl;
                break;
            case '-':
                cout << num1 - num2 << endl;
                break;
            case '*':
            case 'x':
            case 'X':
                cout << num1 * num2 << endl;
                break;
            case '/':
            case '%':
                if (num2 == 0) {
                    cout << "You cant divide by 0" << endl;
                }
                else {
                    cout << num1 / num2 << endl;
                }
                break;
            default:
                cout << "not understood";
        }
        cout << "would you like to enter another calculation? (y/n) ";
        cin >> again;
        cin >> num1 >> operation >> num2;
        if (cin.fail()) {
            cout << "you need to enter numbers" << endl; //this part added mess with it tomorrow
            cin.clear();
            cin.ignore(999, '\n');
            num1 = NULL;
            num2 = NULL;
            continue;
        }

        cout << num1 << " "
        << operation << " "
        << num2 << " = ";


    } while (again == 'y' || again == 'Y');

    //closing bracket of the else for cin.fail
    cin.get();
    return 0;
}

更改后的输出

Welcome to Brent's Calculator.
Enter 'Value (operator) Value' ex. 2 + 2
44+44
44 + 44 = 88
would you like to enter another calculation? (y/n) y
a+b
you need to enter numbers
would you like to enter another calculation? (y/n) y
4+4.4
4 + 4.4 = 8.4
would you like to enter another calculation? (y/n) 

注意:当输入的数字为num1 = 0且num2 = 0时,将出现转角情况 0 + 0 = 0(在这种情况下最好使用其他标志)