Simple calculator using command line with C++

后端 未结 5 532
遇见更好的自我
遇见更好的自我 2021-01-26 10:57

I\'m writing a project that we do simple calculator from command line. The users input in this format programname firstNumber operator secondNumber. Here what I

5条回答
  •  天命终不由人
    2021-01-26 11:34

    Here is the problem:

    double firstNumber = atoi(argv[1]);
    char theOperator = argv[2][0];
    double secondNumber = atoi(argv[3]);
    

    atoi converts a string to an int, so 2.5 will be converted to 2 and 1.25 will be converted to 1, that's why you get the result 2 + 1 = 3. Use atof instead.

提交回复
热议问题