Pointers Help

Hello, I seem to be having some trouble learning pointers. (Specifically passing them through functions. I am trying to pass the declared pointers in askForCoins to TotalAndWrite. I'm not quite sure how to do this. Could anybody throw me some advice on this?
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
 #include <iostream>
#include <string>
using namespace std;

string askForName();
int askForCoins(string);
int TotalAndWrite();

int main()
{
	string userName, n;
	char choice;
	cout << "Hello! My name is Brendan Bohlin; Welcome to my Coin Counter Program!" << endl << endl;

	userName = askForName();
	cout << "Hi " << userName << "!" << endl << endl;
		do
		{
			askForCoins(userName);
			
			cout << "Would you like to run the program again?" << endl << "If you would like to run the program again, please enter any key." << endl;
			cout << "If you would like to end the program please enter 'N' or 'n'" << endl; 
				cin >> choice;
		}
		while ((choice != 'n')&&(choice !='N'));
	cout << "Goodbye!" << endl;
}

string askForName()
{
	string userName;
	cout << "Please enter your name:       ";
		getline (cin, userName);
	return userName;
			
}

int askForCoins(string userName)
{
	int penny, nickel, dime, quarter;
	int *ptrP = &penny;
	int *ptrN = &nickel;
	int *ptrD = &dime;
	int *ptrQ = &quarter;
	cout << "Please enter the number of QUARTERS, DIMES, NICKELS, and PENNIES" << endl << "that you have with you , " << userName << endl;
	cout << "Quarters: ";
		cin >> quarter;
	cout << endl << "Dimes: ";
		cin >> dime;
	cout << endl << "Nickels: ";
		cin >> nickel;
	cout << endl << "Pennies: ";
		cin >> penny;
	
}

int TotalAndWrite(int *ptrP, *ptrQ, *ptrN, *ptrD)
{
	int q, d, n, p;

	q = *ptrQ * 0.25;
	d = *ptrD * 0.10;
	n = *ptrN * 0.05;
	p = *ptrP * 0.01;
	
	cout << "Quarters: " << quarters << " $" << q << endl;
	cout << "Dimes: " << dimes << " $" << d << endl;
	cout << "Nickels: " << nickels << " $" << n << endl;
	cout << "Pennies: " << pennies << " $" << p << endl;
}



Thanks for any help!
-Torm04
hiya,
firstly your function prototypes need to match up with your actual function signatures. "TotalAndWrite" doesn't do this at the moment:

prototype:
int TotalAndWrite();
signature:
 
int TotalAndWrite(int *ptrP, *ptrQ, *ptrN, *ptrD)


also note that multiplying an integer by a fraction (lines 61 - 64), and then trying to put the answer into another integer will not give you your desired answer.

Your askForCoins() function doesnt really do anything as it doesnt return anything, nor does it do anything with the variables you ask from the user.

fix these first then you can tackle the pointer stuff.
Last edited on
Topic archived. No new replies allowed.