Xavier_NZX
2017-06-01 12:15
采纳率: 100%
浏览 840

菜鸟的C++出错了,求解答

#include "Calculator.h"
#include
#include

Calculator::Calculator(double i, double j, char *prt) {
operand1 = i;
operand1 = j;
operation = new char[20];
strcpy_s(operation, 10, prt);
};

double Calculator::doOperation() {
if (strcmp(operation, "div") == 0) {
if (operand2 == 0) {
std::cout << "operand2 can't be 0" << std::endl;
return 1;
}
return operand1 / operand2;
}
else if(strcmp(operation, "minus") == 0)
return operand1 - operand2;
else if(strcmp(operation, "multi") == 0)
return operand1 * operand2;
else if(strcmp(operation, "plus") == 0)
return operand1 + operand2;
};

using namespace std;
int main(int argc, char *argv[]) {

if (argc != 4) {

    cout << "You must give 4 arguments!" << endl;

    return 1;
}

double op1, op2,m;

char *opcode;

op1 = atof(argv[2]);

op2 = atof(argv[3]);

opcode = argv[1];

Calculator one(op1, op2, opcode);

m = one.doOperation();

cout << m << endl;

return 0;

}
图片说明
include的部分显示不出来,其实是没问题的,这个编辑器真难用

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 战在春秋 2017-06-02 00:48
    已采纳

    在构造函数中operand2没有接收到值。

    Calculator::Calculator(double i, double j, char *prt) {
    operand1 = i;  //这里是operand1
    operand1 = j;  //这里也是operand1
    operation = new char[20];
    strcpy_s(operation, 10, prt);
    };
    

    类似这样的错误,编译器不会报,小心一些就好了。

    用心回答每个问题,如果有帮助,请采纳答案好吗,谢谢~~~

    已采纳该答案
    打赏 评论
  • threenewbee 2017-06-01 16:12

    用atoi代替atof看看

    打赏 评论

相关推荐 更多相似问题