can someone help me with this program

Hello all, I'm trying to create this program for my c++ homework, but I can't for the life of me figure out how to actually write it.

Enter a beginning balance :900
Commands:
C - process a check
D - process a deposit
E - end the program

Enter transaction type: C
Enter transaction amount: 50.25
Processing check for $50.25
Balance: $849.75
Service charge: $.25 for a check
Total service charges: $.25

Enter transaction type: C
Enter transaction amount: 250
Processing check for $250.00
Balance: $599.75
Service charge: $0.25 for a check
Total service charges: $.50

Enter transaction type: D
Enter transaction amount: 200
Processing deposit for $200.00
Balance: $799.75
Total service charges: $.50

Enter transaction type: E
Processing end of month
Final balance: $799.25



Input validation:
If the transaction type is invalid, print an informative error message. Ignore the current transaction and have the user enter the next transaction.
The transaction amount should be a positive number (larger than zero). If it is not, print an informative error message. Ignore the current transaction and have the user enter the next transaction.

Other requirements:
Do not use global variables in any assignment. A global variable is a variable that is declared outside any function. Note: global constants are okay.
All dollar amounts should be printed with 2 decimal places.


I can get the the commands to display, and the individual command functions to run on their own, but I cannot get the functions to interrelate to each other, nor can I get it to continue to loop through the commands until the 'E' char is given.

I don't want someone to write the program for me, but I would like some help with direction and how I should go about writing the program.
Last edited on
Care to show your code you have now? We can just build onto it.
#include <iostream>
#include <iomanip>
using namespace std;

int main (int argc, char * const argv[])
{
char choice = ' ';
int balance;
int amount;
int totalServiceChagre;
double serviceCharge = .25;
int numService = 0;

cout<<fixed<<showpoint<<setprecision(2);

cout << "Checkbook Balancing program" <<endl;
cout<<"Please enter your beginning blance: ";
cin >>balance;
cout <<endl;

cout << "Please enter a transaction code using lowercase 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;

cout << "Transaction type: ";
cin >>choice;

while (choice != 'e')
{
if (choice == 'c')
{
cout <<"Enter transaction amount: ";
cin >>amount;
cout<<"Processing check for "<<amount<<endl;
balance = balance - amount;
cout <<"Balance: "<<balance<<endl;
cout<<"Service charge: $"<<serviceCharge<<" for a check"<<endl;
totalServiceChagre = (numService++) * serviceCharge;

cout <<"Total service charges: $"<<totalServiceChagre<<endl;
return 0;
}

else if (choice == 'd')
{
cout <<"Enter transaction amount: ";
cin >>amount;
cout<<"Processing deposit for "<<amount<<endl;
balance = balance + amount;
cout <<"Balance: "<<balance<<endl;
totalServiceChagre = numService * serviceCharge;

cout <<"Total service charges: $"<<totalServiceChagre<<endl;
return 0;
}

else if (choice == 'e')
{
cout<<"Processing end of the month "<<endl;
totalServiceChagre = numService * serviceCharge;
cout <<"Final Balance: "<<balance - totalServiceChagre<<endl;
return 0;
}
else
{
cout<<"Please enter a valid transaction type"<<endl;
return 0;
}
}
return 0;
}

I've got plenty of it working.

the main problem that I'm having is that 'numService' wont increment up when 'numService++' is listed and I cannot figure out how to re-prompt the user for their transaction type.
Some thing's I see from a glance, you probably want to move the menu inside the while loop, and get rid of the return 0s in the if statements. As for your numService not working, try making it it's own line.
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
#include <iostream>
#include <iomanip>
using namespace std;

int main (int argc, char * const argv[])
{
char choice = ' ';
int balance;
int amount;
int totalServiceChagre;
double serviceCharge = .25;
int numService = 0;

cout<<fixed<<showpoint<<setprecision(2);

cout << "Checkbook Balancing program" <<endl;
cout<<"Please enter your beginning blance: ";
cin >>balance;
cout <<endl;

while (choice != 'e')
{
cout << "Please enter a transaction code using lowercase 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;

cout << "Transaction type: ";
cin >>choice;	

if (choice == 'c')
{
cout <<"Enter transaction amount: ";
cin >>amount;
cout<<"Processing check for "<<amount<<endl;
balance = balance - amount;	
cout <<"Balance: "<<balance<<endl;	
cout<<"Service charge: $"<<serviceCharge<<" for a check"<<endl;
numService++;	
totalServiceChagre = numService * serviceCharge;

cout <<"Total service charges: $"<<totalServiceChagre<<endl;
}//if 'c'

else if (choice == 'd')
{
cout <<"Enter transaction amount: ";
cin >>amount;
cout<<"Processing deposit for "<<amount<<endl;
balance = balance + amount;	
cout <<"Balance: "<<balance<<endl;	
totalServiceChagre = numService * serviceCharge;

cout <<"Total service charges: $"<<totalServiceChagre<<endl;
}//else if 'd'

else if (choice == 'e')
{
cout<<"Processing end of the month "<<endl;	
totalServiceChagre = numService * serviceCharge;
cout <<"Final Balance: "<<balance - totalServiceChagre<<endl;
}//else if 'e'
else
{
cout<<"Please enter a valid transaction type"<<endl;
}//else
}//while choice!='e'
return 0;
}//main 


Disclaimer, I am using a Chromebook and therefore, I can not test the code, but visually, it looks like it should work. Let me know...
everything but the numService incrementing.

Also, how would your recommend me forcing the 'amount char' to be > 0?
Last edited on
Since totalServiceChagre (sic) is an int, you need at least 4 checks processed before you'll see it change (c++ rounds integers toward 0). That may be why you say numService isn't incrementing.

To make 'amount' positive, you could make it unsigned or use an if statement to reject a negative input. The latter is likely better, since you could tell the user "that's a negative number".
Might as well make your ints doubles since type int doesn't support decimals afaik
Since totalServiceChagre (sic) is an int, you need at least 4 checks processed before you'll see it change (c++ rounds integers toward 0). That may be why you say numService isn't incrementing.


yeah... by just changing all of the 'int' variables into double fixed the entire problem.

Now I just need to figure out the 'if' statement incase of a negative number, yay!
Topic archived. No new replies allowed.