简单的计算器问题(不输出答案)

时间:2015-10-10 04:59:14

标签: c++

刚刚开始阅读C ++书籍,其中一个练习问题是编写一个小计算器,将四个算术运算中的一个作为输入,这两个运算的两个参数,然后打印出结果。

可悲的是,程序会一直运行,直到用户输入算术选项。 因此,如果我选择进行乘法运算,则会写入“乘法”,它只是停留在那里,之后什么都不做。 Image of the problem im having

#include <iostream>
#include <string>

using namespace std;

int main(){
    // Simple calculator program

    // Declaring three variables
    float numberOne;
    float numberTwo;
    string operationOption;

    // Asking the user which two numbers he/she will use
    cout << "Enter the first number you would like to apply a arithmetic operation to: ";
    cin >> numberOne;
    cin.ignore();
    cout << "Now enter the second number: ";
    cin >> numberTwo;
    cin.ignore();

    // Using cin to input users selection
    cout << "Enter the operation you want to perform." << endl;
    cout << "The options you have are: " << endl;
    cout << "Multiplication, Subraction, Division and Addition: " << endl;
    cin >> operationOption;
    cin.ignore();
    cin.get();
    // Where it all happens
    if ( operationOption == "Multiplication" ) {
        cout << "The first number multiplied by the second number is: " << numberOne * numberTwo << endl;
    } else if ( operationOption == "Division" ) {
        cout << "The first number divided by the second number is: " << numberOne / numberTwo << endl;
    } else if ( operationOption == "Subtraction" ) {
        cout << "The first number subtracted by the second number is: " << numberOne - numberTwo << endl;
    } else if ( operationOption == "Addition ") {
        cout << "The first number added to the second number is: " << numberOne + numberTwo << endl;
    } else {
            cout << "You entered an invalid option.";
    };

}

1 个答案:

答案 0 :(得分:0)

删除行:

cin.get();

将解决您的问题