I need some assistance with a program I wrote and had to alter. Here are the initial instructions and what I initially had then my reworking and what I have now and any assistance would be helpful.

Write a C++ program to help you balance your checkbook at the end of the month. The program should have the user enter the initial balance followed by a series of transactions. For each transaction, first have the user enter a transaction type. The valid transaction types are:

C - process a check.
D - process a deposit.
E - do end of month processing and exit the program.
For checks and deposits, the user should be prompted to enter the transaction amount.

Service Charges

There is a $0.25 service charge for each check written. Keep a running total of the service charges.
Service charges are not deducted from the account balance until the end of the month.
Output

After each transaction, print

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

Here is the code:

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
    char choice = ' ';
    double balance;
    double amount;
    double totalServiceCharge;
    double serviceCharge = .25;
    double numService = 0;

    cout<<fixed<<showpoint<<setprecision(2);//displays 2 decimal points

    cout<<"Checkbook Balancing program"<<endl;
    cout<<"Please enter your beginning balance: ";//set balance
    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: ";//setting up transaction type
        cin>>choice;

        if (choice == 'c' )
        {
            cout<<"Enter transaction amount: ";//loop for deposits
            cin>>amount;//withdrawal amount
            cout<<"Processing check for "<<amount<<endl;
            balance = balance - amount;
            cout<<"Balance: "<<balance<<endl;
            cout<<"Service charge: $"<<serviceCharge<<" for a check"<<endl;
            numService++;
            totalServiceCharge = numService * serviceCharge;//sets up
            //total trans amount and keeps track if any service charge
            cout<<"Total service charges: $"<<totalServiceCharge<<endl;
        }//if 'c'

        else if (choice == 'd')
        {
            cout<<"Enter transaction amount: ";//loop takes care of deposits
            cin>>amount;//amount for deposit
            cout<<"Processing deposit for "<<amount<<endl;
            balance = balance + amount;
            cout<<"Balance: "<<balance<<endl;
            totalServiceCharge = numService * serviceCharge;

            cout<<"Total service charges: $"<<totalServiceCharge<<endl;
        }//else if 'd'
        else if (choice == 'e')
        {
            cout<<"Processing end of the month "<<endl;//loop tallies up fees, withdrawals and deposits
            totalServiceCharge = numService * serviceCharge;
            cout <<"Final Balance: "<<balance - totalServiceCharge<<endl;
        }//else if 'e'
        else
        {
            cout<<"Please enter a valid transaction type"<<endl;//as long as E is not selected
        }//else
    }//while choice!='e'
    return 0;
}//main

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 do check processing and another function to do deposit processing.
Add additional service charges (see below).
The program commands are as follows (see the sample program dialog near the bottom of this page).

Transaction command Meaning

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.

I have reworked this code and now I have this and not sure what I did wrong:

#include <iostream>
#include <iomanip>
using namespace std;

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;
}

Please can I get some guidance.

Recommended Answers

All 7 Replies

It would help a lot if you told us what was going wrong; while we can surmise a lot from the code, to be as accurate as possible, we would need to know the exact error messages you are getting.

That having been said, I can see three major problems, offhand, two of which would lead to a compile time error, the other to a runtime error.

The first is a common mistake made by many new C++ programmers, which is that you are confusing function prototypes and function calls, and possibly function implementations as well. A function prototype is a description of the function; it gives the name of the function, the return type, and the name and type of its parameters, like so:

double choiceC(double balance, double totalServCharge)

You would place this before any calls to the function are made. The header of a function implementation looks very similar, and in fact must match the prototype, at least as far as the function name and the types are concerned:

double choiceC(double balance, double totalServCharge)
{
   // the body of the function ...
}

This can be before or after the function is used, so long as you have a prototype for the function before the calls. Finally (and this is where you are having trouble), a function call consists of the name of the function and a list of actual parameters (i.e., arguments), but does not include the types of the parameters.

choice(myBalance, myServiceCharges);

Note that the number and types of the arguments have to match the parameters, but the names of the arguments do not (though they can).

Thus, in your code, where you have

        //choice "c"
        if (choice == 'c')
        {
            choiceC(double balance);
        }

you should instead have:

        //choice "c"
        if (choice == 'c')
        {
            choiceC(balance, totalServCharge);
        }

This brings us to a second issue: your return values. In C++, a function can have at most one return value; that value can be a primitive type, or a structure, or even an object, but there can only be one of them. When you write:

 return balance, totalServCharge;

the second return value, totalServCharge, will cause a compile time error.

The best solution to this - and the next problem - is to use reference parameters, as I'll explain shortly.

The last problem is that your functions aren't changing the values of balance and totalServCharge in the main() function. This is because when you pass a value to a function, by default you are passing a copy of that variable's value, not the variable itself. To change this behavior, you will want to use references, which pass the actual variable's location to the function (there's another option as well, namely passing a pointer, but references are better for our purposes). you can change a value parameter to a refernce parameter simply by adding an ampersand (&) before the name of the parameter in the parameter list:

void choiceC(double& balance, double& totalServCharge);

Note that I changed the return type to void, as you won't need to return a value if you do it this way.

All together, here is your code with my recommended changes. I haven't fixed any of the logical errors in the program, but it should at least get you on your way.

#include <iostream>
#include <iomanip>
using namespace std;

void choiceC(double& balance, double& totalServCharge);
void choiceD(double& balance, double& totalServCharge);
void 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 balance: ";
    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;
}

void 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 transaction 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;
    }
    //correct input by user
    else if (amount > 0)
    {
        cout<<"Processing check for: $"<<amount<<endl;
    }
    //calculations for check 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 << "service 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;
    }
}

void 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 += amount;

    cout <<"Balance: "<<balance<<endl;

    //calculate the total amount of service charges
    cout <<"Total service charges: $"<<totalServCharge<<endl;
}

void 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!";
    }
    balance = 0;
}

As Schol says, help us to help you.

What does your program do that you don't want it to; what does it not do that you think it should; what input do you type into the keyboard to demonstrate the bad behaviour; what output do you get on screen.

Sorry about that, I had to pull a double shift so sorry it took so long to respond but what I want the program to do is have a person pick what transaction type they want to conduct then process said transaction and then keep a running total of the service fees, and apply them when the customer closes out the month by pressing E

The valid transaction types are:

C - process a check.
D - process a deposit.
E - do end of month processing and exit the program.
For checks and deposits, the user should be prompted to enter the transaction amount.

Service Charges

There is a $0.25 service charge for each check written. Keep a running total of the service charges. Service charges are not deducted from the account balance until the end of the month.
If the account balance falls below $800.00 at any time during the month, there is a $5.00 service charge for the month.This fee is only charged ONCE per month.
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.
All service charges should be deducted from the account balance at the end of the month.

Output:

After each transaction, print

the command data to confirm the transaction
the resulting account balance
the total service charges accrued so far
At the end of the month, deduct the service charges and print the final balance. Your program output should look something like this example.
********************************************************************************
Checkbook Balancing Program should output this:

Enter the 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
********************************************************************************

I got the original program to work but the second program seems to compile but then it just sits there, it won't do too much after that and I am not able to find why it is freezing up.

Which compiler are you using, and have you tried compiling the second version of the program without running it at the same time (usually by selecting 'Build' or 'Compile' rather than 'Run')?

using xcode

If the account balance falls below $800.00 at any time during the month, there is a $5.00 service charge for the month. This fee is only charged ONCE per month.

You will need to keep a 3rd variable (perhaps a bool 'flag') to handle this.

Passing all this info back and forth, suggests a struct is the 'way to go' to hold all the present info

struct Account
{
   double balance;

   // reset fees to zero at end of month 
   // but first subtract from balance
   // after checking ... if below == true

   double fees; // keep running total for month

   bool below; // if true at end of month
               // add 5 to monthly fee total
               // then ... reset to false
} ;

Note:
-> a struct can contain functions to process the data there
-> a struct can have a constructor to set inital values

If you do not wish to use a struct, you can add a 3rd variable, and pass in by reference, so that the value is updated in the 'calling scope'.

But ... the first part (of the 2nd step) ... is to get the first program (stage) ... working ... the way you wish.

See this example, for a common student way to handle input validation ...

// checkBook1.cpp //

// edited ... with input validation added, etc...

/*
Write a C++ program to help you balance your checkbook at
the end of the month. The program should have the user enter
the initial balance followed by a series of transactions.
For each transaction, first have the user enter a transaction
type. The valid transaction types are:

C - process a check.
D - process a deposit.
E - do end of month processing and exit the program.
For checks and deposits, the user should be prompted to
enter the transaction amount.

Service Charges

There is a $0.25 service charge for each check written.
Keep a running total of the service charges.
Service charges are not deducted from the account balance
until the end of the month.
Output

After each transaction, print

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

#include <iostream>
#include <iomanip>
#include <cctype> // re. tolower

using namespace std;

// if place these two Global constants here...
// program can be easily updated (here, at top)
const char* MENU =
    "Please enter a transaction code.\n" \
    "C - Process a Check.\n" \
    "D - Process a Deposit.\n" \
    "E - Do end of month processing and exit program.\n";

const double SERVICE_CHARGE = .25;


double takeInDbl( const char* msg )
{
    double val;
    while( true ) // loop forever ... until break
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;

        // else ... if reach here ...
        cout << "Invalid input ... numbers only!\n";
        cin.clear(); // clear error flasgs
        cin.sync(); // 'flush' cin stream ...
    }
    return val;
}

int takeInChar( const char* msg )
{
    cout << msg << flush;
    int reply = cin.get();
    if( reply != '\n' )
        while( cin.get() != '\n' ) ; // 'flush' cin stream
    return reply;
}
int takeInChr( const string& msg )
{
    cout << msg << flush;
    string reply;
    getline( cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}
bool more()
{
    if( tolower( takeInChr( "More (y/n) ?" ) ) == 'n' )
        return false;
    // else ...
    return true;
}


void processCustomer()
{
    double balance = takeInDbl( "\nPlease enter your "
                                "opening balance: " );
    cout<<endl;

    double fees = 0;
    unsigned int transactions = 0;

    char choice = ' ';
    while (choice != 'e')
    {
        cout << MENU;
        choice = tolower(takeInChar( "Transaction type: " ));

        double amount;
        if (choice == 'c' )
        {
            amount = takeInDbl( "\nEnter transaction amount: " );

            cout<<"Processing check for "<<amount<<endl;
            cout<<"Balance: "<<(balance -= amount)<<endl;
            cout<<"Service charge: $" << SERVICE_CHARGE
                <<" for a check\n" // firstly add in service charge
                <<"Accumlative fee this month: "
                << (fees += SERVICE_CHARGE) << endl;

            ++ transactions;

            //total trans amount and keeps track if any service charge
            cout<<"Total service charges: $"<<fees<<endl;

            cout<<"Total transactions so far this month: "
                << transactions<< endl;
        }
        else if (choice == 'd')
        {
            amount = takeInDbl( "\nEnter transaction amount: " );

            ++ transactions;

            cout<<"Processing deposit of "<<amount<<endl;
            cout<<"Balance: "<<(balance += amount)<<endl;
            cout<<"Accumlative fees this month: $"
                <<fees<<endl;

            cout<<"Total transactions so far this month: "
                << transactions<< endl;
        }
        else if (choice == 'e')
        {
            //loop tallies up fees, withdrawals and deposits
            cout<<"\nProcessing end of the month:\n";
            balance -= fees;
            cout <<"Final Balance: "<<balance <<endl;
            cout <<"Final fee this month: " << fees << endl;
            cout<<"Total transactions this month: "
                << transactions<< endl;

            fees = 0;
        }
        else
        {
            cout<<"\nNot a valid transaction type.\n";
        }

        cout << endl << endl;
    }
}



int main ()
{
    //displays 2 decimal points
    cout<<fixed<<showpoint<<setprecision(2);

    cout<<"Checkbook Balancing program"<<endl;

    do
    {
         processCustomer();
    }
    while( more() ) ;

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.