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 argv[*] to double

Status
Not open for further replies.

madmanrn

Programmer
Feb 17, 2001
2
US
Im making a cmd line based tempature converter program, and it needs to use doubles for more accurate tempature readings. I need to convert argv[1] to a double so it will work in the formula.

The code should be right, ignore all the messed up stuff in this piece of code, this is just my drawing board.

#include <iostream.h>
#include <ctype.h>
#include<stdio.h>
#include<stdlib.h>




int main(int argc, char* argv[])

{

char sys;
double deg, xdeg;
int crap;


if (argc != 3) {
cout << &quot;Example: dex.exe 80 f\n&quot;
<< &quot;Example: dex.exe 27 c\n\n&quot;;
return 0;
}

// char(argv[2]);
crap = atoi(argv[1]);
// cout << deg << argv[2];
deg = crap;

// deg = argv[1];
// sys = argv[2];



// sys = toupper(argv[2]);

// if (sys != 'C' && 'F') {
// cout << &quot;Example: dex.exe 80 f\n&quot;
// << &quot;Example: dex.exe 27 c\n\n&quot;;
// return 0;
// }

switch (sys) {

case 'F':
xdeg = (deg - 32.) * 5./9.;
cout << xdeg << &quot;C\n&quot;;
break;

case 'C':
xdeg = 32. + 9./5. * deg;
cout << xdeg << &quot;F\n&quot;;
break;
}

return 0;
}
 
double doubleVar;
sscanf(argv,&quot;%d&quot;,&doubleVar); John Fill
1c.bmp


ivfmd@mail.md
 
atof(argv[ i ]) will give you the floating point number and just a side note... the %d should be a %f i believe but we all make that mistake... I used %s instead of a %d last week hehe =)

Matt
 
Actually if you want a double, the format specifier should be %lf otherwise unpredictable results. At least that's what I've found before.
 
Normally when using C++ you don't need to use scanf or argv[]. That is a C thing!
In C++ you should be able to declare your variable as whatever type you desire and cin>> is capable of accepting all of the data types. so you could say...

double whateverVariable;
cin>>whateverVariable;

No conversions neccessary. I don't know maybe I am in the wrong forum, but I think this is the C++ forum, I realize that it is difficult to get away from C but there are some nice features in C++.
I hope this isn't annoying!
Sera
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top