问题
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 got so far:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main(int argc, char* argv[])
{
cout << fixed << setprecision(2);
if (argc != 4)
{
cerr << "Usage: " << argv[0] << " <number> <operator> <number>" << endl;
exit(0);
}
else
{
double firstNumber = atoi(argv[1]);
char theOperator = argv[2][0];
double secondNumber = atoi(argv[3]);
switch (theOperator)
{
case'+':
{
cout << "The answer is " << firstNumber + secondNumber << endl;
break;
}
case '-':
{
cout << "The answer is " << firstNumber - secondNumber << endl;
break;
}
case '*':
{
cout << "The answer is " << firstNumber * secondNumber << endl;
break;
}
case '/':
{
if (secondNumber == 0)
{
cout << "Can not devide by a ZERO" << endl;
break;
}
else
{
cout << "The answer is " << firstNumber / secondNumber << endl;
break;
}
}
}
}
}
I came up with a problem that my project does the calculation wrong for decimal numbers. For example, if I do 2.5 + 1.25, the result is 3. Can anyone please help me? I'm new to C++
回答1:
The other answers are correct, but I'm going to pitch using strtod instead of atof in the name of Eris and because it's easier to check the input for correctness.
char * endp;
double firstNumber;
if (argv[1][0] != '\0')
{ // input string is not empty
firstNumber = strtod (argv[1], &endp);
if (*endp != '\0')
{ // number didn't end at the end of the string. String not a number.
cout << "First number was not a number, dude." << endl;
return 0;
}
}
else
{
cout << "Can't do much without a number, dude." << endl;
return 0;
}
With atof, the program can be called with "I'm the very model of a modern major-general" and produce results. Bad results, but results all the same.
Oh, and warning on calling exit
in C++. The program stops dead right there, and no destructors are called. No harm here, but not a good habit to get into.
回答2:
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.
回答3:
The problem is how you transform the string in numbers: you are using atoi
which like its name suggests transforms the input in a integer.
The solution is to simply replace this by atof
which will read correctly floating-point numbers.
回答4:
In the lines
double firstNumber = atoi(argv[1]);
and
double secondNumber = atoi(argv[3]);
atoi
converts the parameter into an int
(hence truncating after the decimal point). You need to convert it into a double
. You should instead use std::atof, or, if using C++11, may also use std::stod(argv[1]);
.
回答5:
In addition to @meneldal suggestion, you can also use stringstream in C++ to convert from string to number
double value;
stringstream(someString) >> value;
来源:https://stackoverflow.com/questions/30090223/simple-calculator-using-command-line-with-c