在C ++中添加

时间:2014-11-18 19:42:47

标签: c++ function addition arithmetic-expressions

我开始用C ++编写一个计算器程序,而且我无法正常工作。我尝试过不同类型的数字,使用字符串然后转换为int,将函数封装在一个类中,但都无济于事。非常感谢帮助,我是C ++的新手(Java是我的主要语言)。谢谢!

#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int calcIt(int a, char b, int c){
    int result = 0;
        if(b == '+'){
            result =(a+b);
        }
    return result;
}
int main(){
    int aa;
    char bb;
    int cc;
    cout << "Int a: " << endl;
    cin >> aa;
    cout << "Operand: " << endl;
    cin >> bb;
    cout << "Int b: " << endl;
    cin >> cc;
    cout << "That is: " << calcIt(aa,bb,cc) << endl;
    return 0;
}

1 个答案:

答案 0 :(得分:4)

#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int calcIt(int a, char b, int c){
    int result = 0;
    if (b == '+'){
        result = (a + c);  // <<------ this was a + b
    }
    return result;
}
int main(){
    int aa;
    char bb;
    int cc;
    cout << "Int a: " << endl;
    cin >> aa;
    cout << "Operand: " << endl;
    cin >> bb;
    cout << "Int b: " << endl;
    cin >> cc;
    cout << "That is: " << calcIt(aa, bb, cc) << endl;
    return 0;
}