I've written a calculator program but at the moment I'm not getting the right calculation results. I want the program to convert the character input into a double data type.
I tried using the function atof but this didn't work. does anyone have any solutions?
double add(double x, double y)
{
return x + y;
}
double subtract(double x, double y)
{
return x - y;
}
double divide(double x, double y)
{
return x / y;
}
double multiply(double x, double y)
{
return x * y;
}
int main()
{
char first_input;
char second_input;
double x;
double y;
char op;
bool valid = true;
cout << "*****************************" << endl;
cout << "** calculator **" << endl;
cout << "*****************************" << endl;
do
{
cout << "Please enter a number: ";
cin >> first_input;
x = atof(first_input);
valid = (x >= '0' && x <= '9');
if(valid)
cout << "Correct data!\n";
else
cout << "In-correct data\n";
}while(!valid);
I tried using the function atof but this didn't work. does anyone have any solutions?
double add(double x, double y)
{
return x + y;
}
double subtract(double x, double y)
{
return x - y;
}
double divide(double x, double y)
{
return x / y;
}
double multiply(double x, double y)
{
return x * y;
}
int main()
{
char first_input;
char second_input;
double x;
double y;
char op;
bool valid = true;
cout << "*****************************" << endl;
cout << "** calculator **" << endl;
cout << "*****************************" << endl;
do
{
cout << "Please enter a number: ";
cin >> first_input;
x = atof(first_input);
valid = (x >= '0' && x <= '9');
if(valid)
cout << "Correct data!\n";
else
cout << "In-correct data\n";
}while(!valid);