Pointer Problems

I've got a program that I've got all coded out. It's just to demonstrate the basics of pointers but I can't get it to compile. I think there is just one problem I'm having but I can't figure it out. I've read articles and I feel like I'm doing it right.
________________________________

#include <iostream>
using namespace std;

/****************
*
*
****************/
void biggerBalance(float dinner,
float movie,
float iceCream,
float *pBal)
{
float date;
float tax;

date = dinner + movie + iceCream;

tax = *pBal * .15;

*pBal - date;
*pBal - tax;

return;
}

/**********************************************************************
*
*
***********************************************************************/
int main()
{
float samsBalance;
float suesBalance;
float *pBalance;

float dinner;
float movie;
float iceCream;

cout << "What is Sam's balance? ";
cin >> samsBalance;
cout << "What is Sue's balance? ";
cin >> suesBalance;
cout << "Cost of the date:\n";
cout << "\tDinner:\t";
cin >> dinner;
cout << "\tMovie:\t";
cin >> movie;
cout << "\tIce Cream:\t";
cin >> iceCream;

if (samsBalance > suesBalance)
pBalance = &samsBalance;
else
pBalance = &suesBalance;

biggerBalance(dinner, movie, iceCream, &pBalance);

cout << "Sam's balance: $" << samsBalance << endl;
cout << "Sue's balance: $" << suesBalance << endl;


return 0;
}
______________________________________

The error I'm getting is

pointerBasics.cpp:75: error: cannot convert \u2018float**\u2019 to \u2018float*\u2019 for argument \u20184\u2019 to \u2018void biggerBalance(float, float, float, float*)\u2019

Line 75 is the bigger balance function is main.
pBalance is already a pointer, &pBalance is now a pointer-to-a-pointer-to-a-float.

(You have some problems inside biggerBalance too, but I'll let you work them out. I suggest turning on compiler warnings. If using gcc, add -Wall to your compilation line).
Haha, I just found my tax problem. Thank you.

So the way I understand it is the & gives me the address of that variable and * resolves the address to the data.

This is my revised code but I'm still getting the same issue. If I'm passing the pointer into biggerBalance with the * ... I don't know. I guess I'm not sure what I'm asking.

#include <iostream>
#include <iomanip>
using namespace std;

/****************
*
*
****************/
void biggerBalance(float dinner,
float movie,
float iceCream,
float *pBalance)
{
float date;
float tax;

date = dinner + movie + iceCream;

tax = date * .15;

*pBalance - date;
*pBalance - tax;

}

/**********************************************************************
*
*
***********************************************************************/
int main()
{
float samsBalance;
float suesBalance;
float *pBalance;

float dinner;
float movie;
float iceCream;

cout << "What is Sam's balance? ";
cin >> samsBalance;
cout << "What is Sue's balance? ";
cin >> suesBalance;
cout << "Cost of the date:\n";
cout << "\tDinner: ";
cin >> dinner;
cout << "\tMovie: ";
cin >> movie;
cout << "\tIce cream: ";
cin >> iceCream;

if (samsBalance > suesBalance)
pBalance = &samsBalance;
else
pBalance = &suesBalance;

biggerBalance(dinner, movie, iceCream, *pBalance);

cout << "Sam's balance: $" << &samsBalance << endl;
cout << "Sue's balance: $" << &suesBalance << endl;


return 0;
}

Additionally in the void biggerBalance function, I could call *pBalance anything I want couldn't I?
Last edited on
Check these line:
1
2
3
4
5
6
7
8
9
10
11
//biggerBalance:
//These lines do arithmetic, but don't store the return value anywhere.
*pBalance - date;
*pBalance - tax;

//main:
//This line will cause an error. pBalance is a pointer, there is no need to dereference it.
biggerBalance(dinner, movie, iceCream, *pBalance);
//These lines print the address of the variables.
cout << "Sam's balance: $" << &samsBalance << endl;
cout << "Sue's balance: $" << &suesBalance << endl;


awalrus00 wrote:
So the way I understand it is the & gives me the address of that variable and * resolves the address to the data.
When used as unary operators, yes; & returns the address of (pointer to) a variable, and * dereferences a pointer.

awalrus00 wrote:
If I'm passing the pointer into biggerBalance with the *
Actually, by using * on a float* you are dereferencing it, and passing in a float.

awalrus00 wrote:
Additionally in the void biggerBalance function, I could call *pBalance anything I want couldn't I?
If by that you mean any valid identifier not already defined in scope, then yes. (It does not need to have the same name as the variable/value being passed to it.)

[ code ] Please format your post by inclosing your code with tags... [ /code ]
Thank you! That was fantastic help. I didn't understand that putting the asterisk twice dereferenced it and turned it back into a variable. That's what was giving me the error. It was trying to pass it as a variable rather than just using the address of the pointer.

Additionally I was referencing the pointer when I just wanted to output their account balance in the last two cout statements.

Here is the fixed code.

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
#include <iostream>
using namespace std;

/****************
 * 
 * 
 ****************/
void biggerBalance(float dinner,
   float movie,
   float iceCream,
   float *pBalance)
{
   float date;
   float tax;

   date = dinner + movie + iceCream;

   *pBalance -= date;
}

/**********************************************************************
* 
* 
***********************************************************************/
int main()
{
   float samsBalance;
   float suesBalance;
   float *pBalance;

   float dinner;
   float movie;
   float iceCream;

   cout << "What is Sam's balance? ";
   cin >> samsBalance;
   cout << "What is Sue's balance? ";
   cin >> suesBalance;
   cout << "Cost of the date:\n";
   cout << "\tDinner:    ";
   cin >> dinner;
   cout << "\tMovie:     ";
   cin >> movie;
   cout << "\tIce cream: ";
   cin >> iceCream;

   if (samsBalance > suesBalance)
      pBalance = &samsBalance;
   else
      pBalance = &suesBalance;

   biggerBalance(dinner, movie, iceCream, pBalance);

   cout << "Sam's balance: $" << samsBalance << endl;
   cout << "Sue's balance: $" << suesBalance << endl;
   
   
   return 0;
}


Thanks again for all the help.
Last edited on
Topic archived. No new replies allowed.