When I run my program, it displays the next line instead of the input.

I'm writing a program which should take two user balances, and will subtract the costs of the meals from the person with the higher balance. However, when the program goes to input the second balance, it displays the next cout line from another function. It is imperative that the pointers that are used in my program are still used, as this assignment is dedicated to using pointers. However I do realize that I am probably using the pointers inefficiently and that the names of many variables may be confusing/messy. Please help me find the errors, or any corrections that need to be made.

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


int promptBalance(int balances[])
{
   float samBal;
   float sueBal;
   cout << "What is Sam's balance? ";
   cin >> samBal;
   cout << "What is Sue's balance? ";
   cin >> sueBal;
   balances[1] = samBal;
   balances[2] = sueBal;
   return balances[2];
}

int computeLarger(int samBal, int sueBal)
{
   int * plargerBal;
   if (samBal > sueBal)
      plargerBal = &samBal;
   else
      plargerBal = &sueBal;
   return * plargerBal;
}

int chargeBalance(int * plarBal)
{
   float dinner;
   cout << "\nCost of the date:\n" << "\tDinner: ";
   cin >> dinner;
   float movie;
   cout << "\tMovie: ";
   cin >> movie;
   float iceCream;
   cout << "\tIce Cream: ";
   cin >> iceCream;
   *plarBal -= dinner;
   *plarBal -= movie;
   *plarBal -= iceCream;
}


int main()
{
   int balances[2];
   promptBalance(balances);
   float samBalance = balances[1];
   float sueBalance = balances[2];
   int largerBalance = computeLarger(samBalance, sueBalance);
   int * plarBalance;
   plarBalance = &largerBalance;
   chargeBalance(plarBalance);
   cout << "Sam's balance: " << samBalance << endl;
   cout << "Sue's balance: " << sueBalance;
   return 0;

}

In a two element array, element 0 and element 1 exist. Element 2 does not exist. Don't use balances[2]

computeLarger should return a pointer to the larger balance. This means you'll have to give that function two pointers.

It's badly named. It's more "returnPointerToLargerAmountFromTheseTwoPointers"
int* computeLarger(int* samBal, int* sueBal);

The function chargeBalance shouldn't return anything.

The function promptBalance shouldn't return anything.

The variables samBalance and sueBalance shouldn't exist.
Last edited on
Thanks a ton for the help @Repeater, you're a life saver.
Topic archived. No new replies allowed.