Functions Program

I am having trouble with this program for school. Here are the directions he stated: You and I are working on a Bank program in which I have created the main and you are to create the functions. You are not to change anything in the main. Just add your code where needed. The specifications are : 1) You are only supposed to add four functions: printBalances, readInput, withdraw, deposit

printBalances
Precondition: take in two double values (account balances) as parameters
Postcondition: print the value of the two parameters.
(Assume the first is the checking account balance and the second is the savings account balance)

readInput
Precondition: prints menu choices and prompts the user to enter
number of menu choice
Postcondition: returns valid menu choice

withdraw
Precondition: take in a double (account balance) as a parameter
and Prompt the user to enter a withdrawal amount
Postcondition: update the account balance so that the change is
reflected outside of the function

deposit
Precondition: take in two double values (account balance and
deposit amount) as parameters. Update account balance.
Postcondition: return updated account balance

Error Cheking:
1) The user should not be allowed to overdraw either of their accounts.
2) All entered monetary amounts must be non-negative.


This is the code I have already came up with. After compiling i have errors stating that there aren't enough arguments for my functions. I have no idea why or what is wrong so I am in hopes of looking for some guidance but not answers. I am a beginner at this so I would really like to learn why something isn't right instead of just having answers given to me. Thanks :)

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

#include <iostream>
using namespace std;

double printBalances(double checking, double savings)
/* Pre conditions:take in two double values (account balances) as parameters
     Postcondition: print the value of the two parameters.
*/

int readInput(int choice)
/*Precondition: prints menu choices and prompts the user to enter number of menu choice
    Postcondition: returns valid menu choice
*/

double withdraw(double withdraw, double amount)
/*Precondition: take in a double (account balance) as a parameter and Prompt the user to enter a withdrawal amount
    Postcondition: update the account balance so that the change is reflected outside of the function
*/

double deposit(double amount, double deposit)
/*Precondition: take in two double values (account balance and deposit amount) as parameters. Update account balance.
    Postcondition: return updated account balance
*/

int main()
{
 double savings = 0, checking = 0, amount;
 int choice;
 char cont;
 do
 {
  system("cls");
  printBalances(checking, savings);
  choice = readInput();
  
  switch(choice)
  {
   case 1:
    withdraw(checking); 
    break;
   case 2:
    cout <<"How much would you like to deposit? $";
    cin>> amount;
    checking = deposit(checking, amount);
    break;
   case 3:
    withdraw(savings);
    break;
   case 4:
    cout <<"How much would you like to deposit? $";
    cin>> amount;
    savings = deposit(savings, amount);
    break;
  }
  printBalances(checking, savings);
  
  cout<<"Another transaction? Enter y for yes or n for no ";
  cin>>cont;
  
 }while(cont == 'y' || cont == 'Y'); 
 return 0;
}

double printBalance(double checking, double savings)
{
 cout <<"Your checking balance is: " << checking << endl;
 cout <<"Your savings balance is: " << savings << endl;
 
}

int readInput(int choice)

{
 cout <<" What would you like to do? "<< endl; 
 cin >> choice;
}


double withdraw(double withdraw, double amount)
{
 
  cout<< "Enter the amount you would like to withdraw: " <<endl;
  cin >> withdraw;
 
  amount = amount - withdraw;
  return amount;
  
  if( withdraw > amount)
   {
    cout << "You do not have enought money in you account for this transaction!" <<endl;
   }
  else 
   {
    cout <<"Balance remaining: " << amount <<endl;
   }
   
  
}

double deposit(double amount, double deposit)
{
 do{
  cout << "Enter amount you would like to deposit: " <<endl;
  cin >> deposit;
  
  amount = amount - deposit;
  return amount;
  
   } while (deposit > 0 );
}




It looks like your function declarations are missing the terminating semicolon. Can you post the error messages?

Great post, BTW.
Setting environment for using Microsoft Visual C++ 2003 Toolkit.
(If you have another version of Visual Studio or Visual C++ installed and wish
to use its tools from the command line, run vcvars32.bat for that version.)

Thank you for choosing the Visual C++ Toolkit 2003! Get started quickly by
building the code samples included in the "Samples" directory. Each sample
includes a short whitepaper discussing the Visual C++ features, and a batch
file for building the code.

Type "cl /?" for brief documentation on compiler options.

Visit http://msdn.microsoft.com/visualc/using/documentation/default.aspx for
complete compiler documentation.

C:\Program Files\Microsoft Visual C++ Toolkit 2003>cd %userprofile%

C:\Documents and Settings\Owner>cd Desktop

C:\Documents and Settings\Owner\Desktop>cl pro3.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

pro3.cpp
C:\Program Files\Microsoft Visual C++ Toolkit 2003\include\ostream(574) : warnin
g C4530: C++ exception handler used, but unwind semantics are not enabled. Speci
fy /EHsc
C:\Program Files\Microsoft Visual C++ Toolkit 2003\include\istream(828) : warnin
g C4530: C++ exception handler used, but unwind semantics are not enabled. Speci
fy /EHsc
C:\Program Files\Microsoft Visual C++ Toolkit 2003\include\istream(1064) : warni
ng C4530: C++ exception handler used, but unwind semantics are not enabled. Spec
ify /EHsc
pro3.cpp(9) : warning C4518: 'int ' : storage-class or type specifier(s) unexpec
ted here; ignored
pro3.cpp(9) : error C2146: syntax error : missing ';' before identifier 'readInp
ut'
pro3.cpp(14) : warning C4518: 'double ' : storage-class or type specifier(s) une
xpected here; ignored
pro3.cpp(14) : error C2146: syntax error : missing ';' before identifier 'withdr
aw'
pro3.cpp(19) : warning C4518: 'double ' : storage-class or type specifier(s) une
xpected here; ignored
pro3.cpp(19) : error C2146: syntax error : missing ';' before identifier 'deposi
t'
pro3.cpp(24) : warning C4518: 'int ' : storage-class or type specifier(s) unexpe
cted here; ignored
pro3.cpp(24) : error C2146: syntax error : missing ';' before identifier 'main'
pro3.cpp(33) : error C2660: 'readInput' : function does not take 0 arguments
pro3.cpp(38) : error C2660: 'withdraw' : function does not take 1 arguments
pro3.cpp(46) : error C2660: 'withdraw' : function does not take 1 arguments
pro3.cpp(81) : error C2556: 'double withdraw(double,double)' : overloaded functi
on differs only by return type from 'int withdraw(double,double)'
pro3.cpp(14) : see declaration of 'withdraw'
pro3.cpp(81) : error C2371: 'withdraw' : redefinition; different basic types
pro3.cpp(14) : see declaration of 'withdraw'
pro3.cpp(102) : error C2556: 'double deposit(double,double)' : overloaded functi
on differs only by return type from 'int deposit(double,double)'
pro3.cpp(19) : see declaration of 'deposit'
pro3.cpp(102) : error C2371: 'deposit' : redefinition; different basic types
pro3.cpp(19) : see declaration of 'deposit'

C:\Documents and Settings\Owner\Desktop>

lol here is all the error messages it says after comiling. Im using Windows Visual Basic Command Propmt btw
Yep -- add the missing semicolons to your function declarations and you'll get much further.
Thats before the int main. Like where my preconditions and postconditions are posted?
Yes. For example, line 5 should look like this:

double printBalances(double checking, double savings); <-- note trailing semicolon.
Okay that has cleared up ALOT of the error messages lol. Now I only have 3 stating:

Line 33: error C2660: 'readInput' : function does not take 0 arguments
Line 38: error C2660: 'withdraw' : function does not take 1 arguments
Line 46: error C2660: 'withdraw': function does not take 1 arguments
Look at how you declare/define readInput() on lines 10/71 and how you use it on line 34 above. You will need to reconcile that.

Same with withdraw. The way it is defined and used don't match.

You'll need to review the requirements to determine which is correct.
Okay I had to do some MAJOR redoing lol i think i have it now. Thanks :)

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
using namespace std;

//functions and its conditions
void printBalances(double checking, double savings);
/* Pre conditions:take in two double values (account balances) as parameters
Postcondition: print the value of the two parameters.
*/

int readInput();
/*Precondition: prints menu choices and prompts the user to enter number of menu choice
Postcondition: returns valid menu choice
*/

double withdraw(double& balance);
/*Precondition: take in a double (account balance) as a parameter and Prompt the user to enter a withdrawal amount
Postcondition: update the account balance so that the change is reflected outside of the function
*/

double deposit(double balance, double& amount);
/*Precondition: take in two double values (account balance and deposit amount) as parameters. Update account balance.
Postcondition: return updated account balance
*/

int main()
{
double savings = 0, checking = 0, amount;
int choice;
char cont;
do
{
system("cls");
printBalances(checking, savings);
choice = readInput();

switch(choice)
{
case 1:
withdraw(checking); 
break;
case 2:
cout <<"How much would you like to deposit? $";
cin>> amount;
checking = deposit(checking, amount);
break;
case 3:
withdraw(savings);
break;
case 4:
cout <<"How much would you like to deposit? $";
cin>> amount;
savings = deposit(savings, amount);
break;
}
printBalances(checking, savings);

cout<<"Another transaction? Enter y for yes or n for no ";
cin>>cont;

}while(cont == 'y' || cont == 'Y'); 
return 0;
}

void printBalances(double checking, double savings)//function to print balances
{
//used to format output values and print checking and savings balance
cout.setf(ios::fixed);
cout.setf(ios::left);
cout.precision(2);

cout<<"Your checking balance is: $" << checking <<"\n";
cout<<"Your savings balance is: $" << savings <<"\n\n";

}

int readInput()

{

int choice;
cout<<" What would you like to do?\n\n";
cout<<"(1) Withdraw from checking?\n";
cout<<"(2) Deposit into checking?\n" ;
cout<<"(3) Withdraw from savings?\n";
cout<<"(4) Deposit into savings? \n\n";
cout<<"Enter the number associated with the choice you would like: ";
cin >> choice;
return choice;



}


double withdraw(double& balance)//function for withdrawing
{
double withdraw = 0;
do
{
cout<<"Enter the amount you would like to withdraw: ";//ask for amount to withdraw
cin >> withdraw;//stores input
}while(withdraw < 0);
//do while loop ensures not negatives are being inputted

if (withdraw <= balance)
{
balance = balance - withdraw; //updates balance by subtracted balance from amount wished to withdraw
return balance;
} 
else
{
cout<<"You do not have enough money in your account for this transaction\n";
}//if loop to prevent overdrafts

}

double deposit(double balance, double& amount)//function for deposits
{
do{
balance = balance + amount;//updates balance by adding the amount deposited to the remaining balance

} while (deposit < 0 );// do while loop to ensure no negatives are being inputted
return balance;
}

Excellent!

I have a couple of small questions. They are all similar in nature and revolve around your use of references.

First, the requirement for withdraw() says this:
Precondition: take in a double (account balance) as a parameter
and Prompt the user to enter a withdrawal amount
Postcondition: update the account balance so that the change is
reflected outside of the function


That to me would indicate that the function prototype should look something like this:

void withdraw(&balance);

Since you are changing balance directly, is there any need to return a balance?

Also, in the deposit() function, why is amount passed as a reference rather than by value?
I didnt use voide withdraw(&balance): because the balance is always changing since the user can run the program multiple times so it would have to update each time a withdrawal is made.

I used void printBalance since it doesnt have to return a value back to the main. Its just simple printing to the screen

I didnt use voide withdraw(&balance): because the balance is always changing since the user can run the program multiple times so it would have to update each time a withdrawal is made.

Sure, but look at how you are calling withdraw() on lines 39 and 47 above. You don't use the return value at all.

I used void printBalance since it doesnt have to return a value back to the main. Its just simple printing to the screen

I think you missed my question. It wasn't about the return value. Why are you taking amount by reference rather than by value?
Topic archived. No new replies allowed.