I think your problems stem from your use of
strtod()
. Let's take a look at the definition.
http://pubs.opengroup.org/onlinepubs/007908799/xsh/strtod.html
It takes a pointer, the address of a pointer and returns a double. The first pointer is the start of a char array and the second pointer is a reference to where strtod stopped processing within that char arrary.
For example, if your string is:
10ft
, and which is held at address 0x01001000. As chars take 1 byte, then:
0x01001000 has '1'
0x01001001 has '0'
0x01001002 has 'f'
0x01001003 has 't'
0x01001004 has 0
strtod converts "10" to the number 10.0, and fills in the endptr with the address of the "ft" as it didn't process that. If you're not interested in the end, you simple pass in NULL, and strtod() doesn't attempt to return this stuff to you.
Now all that's terribly complicated. Back in the old days we used to use
atof()
, which is probably all you need.
strtod()
is useful because it helps in more advanced parsers.
http://www.freebsd.org/cgi/man.cgi?query=atof&apropos=0&sektion=0&manpath=FreeBSD+8.2-RELEASE&arch=default&format=html
A string is an object. But you can get a char array representation by using method
c_str()
. So your code becomes:
1 2 3 4 5 6 7
|
cout << "INCOME TAX CALCULATOR - <q to quit>" << endl;
cout << endl;
cout << "Please enter your income" << endl;
string input;
getline(cin, input);
double income = strtod(input.c_str(), NULL);
|
I haven't explained pointers, but I hope this helps.