Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a char to double?

Status
Not open for further replies.

stu74uk

Technical User
Jun 10, 2002
8
GB
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 << &quot;*****************************&quot; << endl;
cout << &quot;** calculator **&quot; << endl;
cout << &quot;*****************************&quot; << endl;

do
{
cout << &quot;Please enter a number: &quot;;
cin >> first_input;
x = atof(first_input);

valid = (x >= '0' && x <= '9');
if(valid)
cout << &quot;Correct data!\n&quot;;
else
cout << &quot;In-correct data\n&quot;;
}while(!valid);
 
>> char first_input;
>> cin >> first_input;
>> x = atof(first_input);

Don't you need more than a single character to read in a number greater than 9?

-pete
 
x is declared as a double but you are comparing it to character values ( x >= '0'... ). Try having a character value compared ( first_input >= '0' && first_input <= '9' ) then converting it to double only if the condition is true.
 
Also, you should set variable &quot;valid&quot; to true or false in the proper places in your pair of if statements.
 
Also, you should set variable &quot;valid&quot; to true or false in the proper places of your if/else statement.
 
Use _ecvt/_fcvt/_gcvt for double to char
and strtod, wcstod for char to double
You can chage the code accodrdingly
hope it helps
Let me know
 
This is NOT elegant, but might help a little:


// cin_test.cpp : Defines the entry point for the console application.
//

#include &quot;stdafx.h&quot;
#include <iostream.h>
#include <iomanip.h>
#include <ctype.h>
#include <stdlib.h>
#include <conio.h>

// 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(int argc, char* argv[])
{

double x, y;

cout << &quot;*****************************&quot; << endl;
cout << &quot;** my calculator **&quot; << endl;
cout << &quot;*****************************&quot; << endl;

// setup so U can input into a fixed fld. including decimals.
cout << setiosflags(ios::fixed); // or ios::floatfield) ;

FOOBAR:
try
{
cout << &quot;Please enter first number: &quot;;
cin >> x;

cout << &quot;Please enter second number: &quot;;
cin >> y;
}
catch (...) // catch all errors. Need more robust err checking.
{
cerr << &quot;fixed number input err. Rethink and try again.&quot; << endl;
goto FOOBAR;
}

cout <<&quot;x = &quot; << x << endl;
cout <<&quot;y = &quot; << y << endl;

cout << &quot;add x + y :&quot; << add (x, y) << endl;
cout << &quot;subtract x - y :&quot; << subtract(x, y) << endl;
cout << &quot;multiply x * y :&quot; << multiply(x, y) << endl;
cout << &quot;divide x / y :&quot; << divide (x, y) << endl;

cout << &quot;---------------- all done --------&quot; << endl;

cout << &quot;Please enter first number: &quot; << endl;

char ch_x[7] = &quot;abcdef&quot; , *junk;
char ch_y[7] = &quot;ghijkl&quot; ;

cin >> ch_x;
x = strtod( ch_x, &junk ); // junk is stuff following legal digits.

cout << &quot;first_num: &quot; << x << &quot; - &quot; << junk <<endl;

cout << &quot;Please enter second number: &quot; << endl;

cin >> ch_y;
y = strtod( ch_y, &junk ); // junk is stuff following legal digits.

cout << &quot;second_num: &quot; << y << &quot; - &quot; << junk << endl;

cout << &quot;add x + y :&quot; << add (x, y) << endl;
cout << &quot;subtract x - y :&quot; << subtract(x, y) << endl;
cout << &quot;multiply x * y :&quot; << multiply(x, y) << endl;
cout << &quot;divide x / y :&quot; << divide (x, y) << endl;

cout << &quot;---------------- all done again --------&quot; << endl;

getch(); // dummy get. .. wait to see results.


return 0;
}

//~~~~~~~~~~~~~~~~~~~~ good luck
 
Cheers for all your help, the atof function did end up working it just needed the '&' operator to return the address of the variable.

~Stu.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top