Click to See Complete Forum and Search --> : C++ lamer seeks help


nanode
01-24-2001, 02:21 AM
I was playing around w/ some real basic C++ stuff and thought I'd make a simple calc. proggie that takes the values as args. Problem is, even when I cast the args as ints, the value is all whacked.


//TireCalc.cpp
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
if(argc != 4) {
cout << "Usage: " << argv[0] << " [width]" << " [profile] " << " [rim]" << endl;
}
else {
int width = (int)argv[1];
int profile = (int)argv[2];
int rim = (int)argv[3];
cout << " width: " << width << " profile: " << profile << " rim: " << rim << endl;
double wallHeightInch = (width / 25.4) * (.01 * profile);
double totalHeightInch = (wallHeightInch * 2) + rim;

cout << "Total wheel diameter: " << totalHeightInch << endl;
}
}


running it shows this:

nanode@stout:~/playcode$ tc 215 70 14
width: -1073743860 profile: -1073743856 rim: -1073743853
Total wheel diameter: 9.07815e+14
nanode@stout:~/playcode$


where are those longs negative ints coming from? I'm obviously lazy and spoiled from using Java all this time.

Thanks for your help.

Strike
01-24-2001, 03:17 AM
Yeah, you are trying to cast a char array into an integer - nope. This is what atoi() is for. It's in stdlib.h. There should be a man page for it (if not, I can reproduce it here for you, I've got it).

nanode
01-24-2001, 12:58 PM
Thanks Strike.

I've come across so many little questions. Maybe reading Stroustrup's book cover-to-cover would be a worthy endeaver for me.

f'lar
01-24-2001, 02:09 PM
Only if you have a year or two of free-time, though it is very good to have for a reference.

[This message has been edited by f'lar (edited 24 January 2001).]

Strike
01-24-2001, 02:44 PM
I agree with f'lar. The only people who need to read Stroustrup cover to cover are people who want to design C++ compilers. It is, however, indeed a very handy reference.

nanode
01-24-2001, 03:11 PM
I wasn't completely serious about reading it cover-to-cover. I don't see any C++ projects in the near future at work, so it's gonna be tough to learn details of the language until I get some significant keyboard time.