Homework help

I'm in the middle of a distance learning class for C++ and as such, the professor chose a rather crappy textbook to have us learn out of.

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <iomanip>
using namespace std;

double choiceC (double balance, double totalServCharge);	
double choiceD (double balance, double totalServCharge);
double choiceE (double balance, double totalServCharge);

int main()
{
	char choice = ' ';							//user choice of transaction type
	double balance = 0.0;								//initial balance of the account
	double totalServCharge = 0.0;
	
	//set all amounts entered to have two decimal places displayed
	cout<<fixed<<showpoint<<setprecision(2);
	
	cout << "Checkbook Balancing program - Ver. 2.0" <<endl;
	cout << "Please enter your beginning blance: ";
	cin >> balance;
	cout << endl;
	
	//main menu
	cout << "Please enter a transaction code using uppercase letters. "<<endl;
	cout << "C - Process a Check"<<endl;
	cout << "D - Process a Deposit"<<endl;
	cout << "E - Do end of month processing and exit program"<<endl;
	
	//how the program chooses what transaction to use
	while (choice != 'E')
	{		
		
		cout << "Transaction type: ";
		cin >>choice;	
		
		//choice "c"
		if (choice == 'c')
		{
			choiceC(double balance);
		}
		
		//choice 'd'
		else if (choice == 'd')
		{
			choiceD(double balance, double totalServCharge);
		}
		else if (choice == 'e')
		{
			choiceE(double balance, double totalServCharge);
		}
		else
		{
			cout << "please enter a valid transaction type" << endl;
		}
	}
	return 0;
}

double choiceC(double balance, double totalServCharge)
{
	int numServCharge = 0;
	int numLessEight = 0;
	int numLessZero = 0;
	const double SERVICE = .25;
	const double LESSEIGHT = 5.0;
	const double LESSZERO = 25.0;
	double amount = 0.0;
	
	cout << "please enter your tansaction amount: ";
	cin >> amount;
	
	//error statement if amount is negative
	if (amount < 0) 
	{
		cout << "a negative is a non-valid entry please try your transaction again"<<endl;
		cout << "Please enter your transaction amount :";
		cin >> amount;				
	}
	//corect input by user
	else if (amount > 0)
	{
		cout<<"Processing check for: $"<<amount<<endl;
	}
	//calculations for check balance
	balance = balance - amount;
	//balance condition statements 
	if (balance < 800)
	{
		cout << balance <<endl;
		cout <<"Balance: "<<balance<<endl;
		cout << "Service charge: $" << SERVICE << " for a check"<<endl;
		cout << "serivce charge: $" << LESSEIGHT << " for a balance less than $800"<<endl;
		numServCharge++;
		numLessEight = 1;
	}
	else if (balance < 0)
	{
		cout <<"Balance: "<<balance<<endl;
		cout << balance <<endl;
		cout << "Service charge: $" << SERVICE << " for a check"<<endl;
		cout << "Service charge: $" << LESSZERO << " for a balance less than $0.0" << endl;
		numServCharge++;
		numLessZero++;
		totalServCharge = (numServCharge * SERVICE) + (numLessEight * LESSEIGHT) + (numLessZero * LESSZERO);
		cout << totalServCharge <<endl;
		
		return balance, totalServCharge;
	}
}
	
double choiceD(double balance, double totalServCharge)
	{
		double amount = 0.0;
		cout <<"Enter transaction amount: ";
		cin >>amount;
		
		//error statment for negative amount
		if (amount < 0) 
		{
			cout << "a negative is a non-valid entry please try your transaction again"<<endl;
			cout << "Please enter your transaction amount :";
			cin >> amount;				
		}
		else if (amount > 0)
		{
			cout<<"Processing check for: $"<<amount<<endl;
		}
		
		cout<<"Processing deposit for: $"<<amount<<endl;
		
		//calculate deposit amount
		balance = balance + amount;	
		
		cout <<"Balance: "<<balance<<endl;	
		
		//calculate the total amount of service charges		
		cout <<"Total service charges: $"<<totalServCharge<<endl;
		return balance, totalServCharge;
	}
	
double choiceE(double balance, double totalServCharge)
	{
		cout <<"Processing end of the month "<<endl;	
		
		cout <<"Your total service charges were: $" <<totalServCharge <<endl;
		cout <<"Final Balance: "<<balance - totalServCharge<<endl;
		
		//if final balance is negative
		if (balance < 0)
		{
			cout << "You owe us money!";
		}	
		return 0;
	}


The assignment is thus:


Modify your checkbook balancing program from assignment 2 as follows:

The user should enter the transaction type and amount (if required) on a single line. In other words, there should not be a separate prompt message for the transaction amount.
Use a separate function to process each transaction type (check or deposit).
Add additional service charges (see below).
The program commands are as follows (see the sample program dialog near the bottom of this page).

Transaction commands
C amount - Process a check for amount dollars, where amount is a floating-point number.

D amount - Process a deposit for amount dollars, where amount is a floating-point number

E - End the program.



Service Charges

There is a $0.25 service charge for each check written.

If the account balance falls below $800.00 at any time during the month, there is a $5.00 service charge for the month. NOTE: This fee is only charged once at most!

If processing a check results in a negative balance, there is a $25 service charge (insufficient funds charge). This $25 fee is charged for each check that results in a negative balance.

Note: all service charges should be deducted from the account balance at the end of the month.


Output:

For each transaction, print

the command data (to confirm the transaction)
the resulting account balance
the total service charges accrued so far



what am I doing wrong?

The program runs up to the point where the user selects a transaction type but then stalls and does not actually go to and run the functions.
Last edited on

what am I doing wrong?


this:
http://cplusplus.com/forum/beginner/1/

Incredibly verbose post, yet you didn't ask a concrete question - what exactly is your problem? Also, use [ code ] tags for code.
Last edited on
Fixed the original post.

Sorry about that.
I fixed some of the code, but it still won't compile.

I do not understand why using this:

choiceC(double balance);

kicks back the error for all of the functions that I have nested in the while statement.

"expected primary-expression before 'double'"
Last edited on
closed account (10oTURfi)
if function requires 2 arguments as seen at line 5, u must pass it 2 values, else overload the function???
Last edited on
True, my bad at copying it onto here, but I get the same error on all the different choice options.
1
2
choiceD(double balance, double totalServCharge)
choiceE(double balance, double totalServCharge)


I get the same error, only doubled for the two different variables sent to the functions
closed account (10oTURfi)
Consider this 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <iomanip>
using namespace std;

double choiceC (double balance, double totalServCharge);	
double choiceD (double balance, double totalServCharge);
double choiceE (double balance, double totalServCharge);

int main()
{
	char choice = ' ';							//user choice of transaction type
	double balance = 0.0;								//initial balance of the account
	double totalServCharge = 0.0;
	
	//set all amounts entered to have two decimal places displayed
	cout<<fixed<<showpoint<<setprecision(2);
	
	cout << "Checkbook Balancing program - Ver. 2.0" <<endl;
	cout << "Please enter your beginning blance: ";
	cin >> balance;
	cout << endl;
	
	//main menu
	cout << "Please enter a transaction code using uppercase letters. "<<endl;
	cout << "C - Process a Check"<<endl;
	cout << "D - Process a Deposit"<<endl;
	cout << "E - Do end of month processing and exit program"<<endl;
	
	//how the program chooses what transaction to use
	while (choice != 'E')
	{		
		
		cout << "Transaction type: ";
		cin >>choice;	
		
		//choice "c"
		if (choice == 'C')
		{
			choiceC(balance, totalServCharge);
		}
		
		//choice 'd'
		else if (choice == 'D')
		{
			choiceD(balance, totalServCharge);
		}
		else if (choice == 'e')
		{
			choiceE(balance, totalServCharge);
		}
		else
		{
			cout << "please enter a valid transaction type" << endl;
		}
	}
	return 0;
}

double choiceC(double balance, double totalServCharge)
{
	int numServCharge = 0;
	int numLessEight = 0;
	int numLessZero = 0;
	const double SERVICE = .25;
	const double LESSEIGHT = 5.0;
	const double LESSZERO = 25.0;
	double amount = 0.0;
	
	cout << "please enter your tansaction amount: ";
	cin >> amount;
	
	//error statement if amount is negative
	if (amount < 0) 
	{
		cout << "a negative is a non-valid entry please try your transaction again"<<endl;
		cout << "Please enter your transaction amount :";
		cin >> amount;				
	}
	//corect input by user
	else if (amount > 0)
	{
		cout<<"Processing check for: $"<<amount<<endl;
	}
	//calculations for check balance
	balance = balance - amount;
	//balance condition statements 
	if (balance < 800)
	{
		cout << balance <<endl;
		cout <<"Balance: "<<balance<<endl;
		cout << "Service charge: $" << SERVICE << " for a check"<<endl;
		cout << "serivce charge: $" << LESSEIGHT << " for a balance less than $800"<<endl;
		numServCharge++;
		numLessEight = 1;
	}
	else if (balance < 0)
	{
		cout <<"Balance: "<<balance<<endl;
		cout << balance <<endl;
		cout << "Service charge: $" << SERVICE << " for a check"<<endl;
		cout << "Service charge: $" << LESSZERO << " for a balance less than $0.0" << endl;
		numServCharge++;
		numLessZero++;
		totalServCharge = (numServCharge * SERVICE) + (numLessEight * LESSEIGHT) + (numLessZero * LESSZERO);
		cout << totalServCharge <<endl;
		
		return balance, totalServCharge;
	}
}
	
double choiceD(double balance, double totalServCharge)
	{
		double amount = 0.0;
		cout <<"Enter transaction amount: ";
		cin >>amount;
		
		//error statment for negative amount
		if (amount < 0) 
		{
			cout << "a negative is a non-valid entry please try your transaction again"<<endl;
			cout << "Please enter your transaction amount :";
			cin >> amount;				
		}
		else if (amount > 0)
		{
			cout<<"Processing check for: $"<<amount<<endl;
		}
		
		cout<<"Processing deposit for: $"<<amount<<endl;
		
		//calculate deposit amount
		balance = balance + amount;	
		
		cout <<"Balance: "<<balance<<endl;	
		
		//calculate the total amount of service charges		
		cout <<"Total service charges: $"<<totalServCharge<<endl;
		return balance, totalServCharge;
	}
	
double choiceE(double balance, double totalServCharge)
	{
		cout <<"Processing end of the month "<<endl;	
		
		cout <<"Your total service charges were: $" <<totalServCharge <<endl;
		cout <<"Final Balance: "<<balance - totalServCharge<<endl;
		
		//if final balance is negative
		if (balance < 0)
		{
			cout << "You owe us money!";
		}	
		return 0;
	}
Last edited on
what do you mean by:

You are calling functions on fucked up way


also, I guess this boils down to me not fully understanding the properties of C++ functions, but none of the variables are interrelating (i.e. balance is not affected throughout the entire program).

Since I cannot rely on my text book, what book on C++ should I invest in?
closed account (10oTURfi)
Kk the thing is:
When you prototype the function you do it like this:
int MYFUNC(int x);
and also when you build it you build it like this:
int MYFUNC(int x){ /*...*/ }
But when you call it, all you need to do is pass an int argument to it like this:
MYFUNC(MY_VAR);

MYFUNC(int MY_VAR); This is what you did btw, and that doesnt make much sense cuz compiler is aware of what type each argument must be

u see wut i did there?
Last edited on

u see wut i did there?


unfortunately no.

I'm completely lost with this subject and my book isn't helping me at all.

So with a prototype function I use something like this:
double choiceC(double c, double d);?

but when I use the function I put this:
choiceC(double balance, double totalServCharge);?

and I have no idea what you mean by the last bit about changing the variable with a reference to it as an argument.
Last edited on
closed account (10oTURfi)
Since you seem to know nothing about pointers yet I removed half of my post to keep it simple.
OK im gonna give you cool example:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

void PrintCrap(int x);//This is function prototype, and this is how it should look like

int main(){
	int a = 666;
	PrintCrap(a);//when you call a function you dont type int a, you just type a!!
}

void PrintCrap(int x){//a is passed as an int argument to PrintCrap function
	cout << "Value of a passed as int argument x is equal to: " << x << endl;
}


u see wut i did there?
Last edited on
closed account (10oTURfi)
And this is what YOU did:
1
2
3
4
5
6
7
8
double choiceD (double balance, double totalServCharge);
//...
int main(){
//...
choiceD(double balance, double totalServCharge);//ERROR! wtf u doin here :D
//...
}
double choiceD (double balance, double totalServCharge){/*...*/}


Compare my code to your code, and it should be easy to understand where is your mistake
Last edited on
Okay... I understand what you mean about the calling functions.

But what about changing variables?
Krofna

Thanks for your help with functions, still a little confusing, but I've got a better handle on these now.

However your help has been more error-inducing than fixing.

I cannot get balance to goto all three functions nor can I get the 'totalServCharge' to work with the other functions or get the variables to be effected by the functions and to stay altered.

I think that I may just be thinking way too hard about this.
closed account (10oTURfi)
eww I should stop trying to help people.
Im terrible at teaching


Anyway, one thing was bugging me about your program. Why are you returning values? What did you try to acomplish with that? I dont see a reason why those functions should return anything, beacuse you arent using that returned value. Why not valueless return type?
It's supposed to return the updated values for the variables: 'balance' and 'totalServCharge' so that they can be used in the other functions
closed account (10oTURfi)
That is what I meant by functions do not change variables nor does return update the value. And thats what I meant when I tried to explain you passing reference to functions. Im not gonna try to explain you that. Find a chapter about pointers in your book if you want to finish the program, I dont want to confuse you anymore. :)
Last edited on
This does not return two values:

return balance, totalServCharge;

It compiles and runs but it doesn't do what you expect it to. What it does is it evaluates the expression before the comma (i.e. balance), tosses out the result, evaluates the expression after the comma (i.e. totalServCharge) and returns that result. So, this function just returns totalServCharge.

If you want balance and totalServCharge to be modified in main, then you should pass them by reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//example pass by reference

#include <iostream>

void func(int& parameterByReference, int parameterByValue) //note the "&"
{
     parameterByReference = 5;
     parameterByValue = 5;
}

int main()
{
     int x = 10;
     int y = 10;

     func(x, y); //x passed by reference, y passed by value

     std::cout << x << std::endl; //prints 5, func modified x
     std::cout << y << std::endl; //prints 10, func did not modify y

     return 0;
}


Passing a parameter by reference allows the function to modify the value in the caller. Another way to accomplish this in C++ is to use pointers. When you pass a parameter by value (i.e. the default way), the function obtains a copy of that variable and just works on the copy.
Topic archived. No new replies allowed.