#include <iostream>
#include <iomanip>
usingnamespace 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'
elseif (choice == 'd')
{
choiceD(double balance, double totalServCharge);
}
elseif (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;
constdouble SERVICE = .25;
constdouble LESSEIGHT = 5.0;
constdouble 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
elseif (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;
}
elseif (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;
}
elseif (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.
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?
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
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>
usingnamespace 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;
}
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.
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?
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. :)
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.
//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.