Pointer Queries

Hi All,

With the below code, can anyone help me with the following?

1) Why the line I have commented besides does not appear the first time
the program goes through the loop?

2) Finally, why does cout << endString << endl;
display the value stored at that address? I thought you would have
needed to type cout << *endString << endl;

If anyone has any advice as to how I could make this program better or if I overlooked potential areas where my code could misbehave, I am more than happy to hear about them.

Thanks so much for your help!

Kind Regards,

Giri

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{

	void taxCalculator(double grossIncome);


	cout << "INCOME TAX CALCULATOR - <q to quit>" << endl;
	cout << endl;
	cout << "Please enter your income" << endl;

	
	
		string input;
		
		getline(cin, input);
		char * cstr = new char [input.size()+1];
		strcpy (cstr, input.c_str()); 

		char * endString;
		double income;
		income = strtod (cstr, &endString);
		
		
		
		while ((income == 0) || (*endString != NULL)) 
			{
				cout << "Please enter a valid number!" << endl;
				input.clear();
				getline(cin,input);
				strcpy (cstr, input.c_str());				
				income = strtod (cstr, &endString);
				cout << endString << endl; // why doesn't this appear the first time I type "1234lol" for example

			}
		
		taxCalculator(income);	
	

	cin.get();
	cin.get();

	return 0;

	

}

void taxCalculator(double grossIncome)
{
	if (grossIncome <= 5000)
		{
			cout << "No Tax Payable!" << endl;
		}


	else if ((grossIncome > 5000) && (grossIncome <= 15000))
		{
				double incomeAfterTax = (5000 * 0) + ((grossIncome - 5000) * 0.10);
				cout << "Your tax payable is $" << incomeAfterTax << endl;

		}

	else if ((grossIncome > 15000) && (grossIncome <= 35000))
		{

		double incomeAfterTax = (5000 * 0) +  ((10000) * 0.10) + ((grossIncome - 10000) * 0.15);
		 cout << "Your tax payable is $" << incomeAfterTax << endl;
		}

	else if (grossIncome > 35000)
		{

		double incomeAfterTax = (5000 * 0) +  ((10000) * 0.10) + (20000 * 0.15) + ((grossIncome - 35000) * 0.20);
		cout << "Your tax payable is $" << incomeAfterTax << endl;

		}
	
	return;
}
Anyone? :)
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.
Topic archived. No new replies allowed.