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.
#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;
}
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.
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".
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!