Hey guys,

I need help again :sad:. I'm doing really well in the class, it just seems like these past couple assignments have been really rushed (i.e., we went over chapter 5 the day after I finished reading chapter 4 =/). I'm getting sort of discouraged, because up to now I've been able to do everything right, and have actually had a few people ask me questions. I hope I can stop bothering everyone with questions twice a day. Anyways, here's the problem. Maybe someone could diagnose the logic error, as it compiles smoothly. I continue to get the defaul case in the switch...

Also, I know it's very sloppy, but if you have any tips on how to keep it neat, please let me know!! :mrgreen:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    const double INT_RATE = 0.05;
    const double SVC_CHGE = 25.00;
    const double MIN_BAL = 1000.00;

    double begBal, wAmt, dAmt, xactAmt, intPaid, bal;
    int acctNo, totWd, totDep;
    char xactCode;

    totWd = totDep = 0;
    bool svcChgd = false;

    ifstream inFile;
    ofstream outFile;

    inFile.open("inData.txt");
    /*if (inFile != "inData.txt")
        return 1;*/
    outFile.open("outData.txt");
    outFile << fixed << showpoint << setprecision(2);

    inFile >> acctNo >> begBal >> xactCode >> xactAmt;
    if (begBal < MIN_BAL)
    {
        begBal -= SVC_CHGE;
        svcChgd = true;
    }

    bal = begBal;

    while (!inFile.eof())
    switch (xactCode)
    {
        case 'D': case 'd':
            bal += xactAmt;
            totWd++;

        case 'I': case 'i':
            bal += xactAmt;
            intPaid = xactAmt;
        case 'W': case 'w':
            bal -= xactAmt;
            totWd++;
            if (bal < MIN_BAL && svcChgd == false)
            {
                bal -= SVC_CHGE;
                svcChgd = true;
            }
        default: 
            cout << "Invalid Transaction Code...\nTerminating Program..." << endl;
            return 1;
    }
outFile << bal;    //just testing the code... didn't work =/

}

If you're planning on helping, I'm sure you can tell what the program is supposed to do. If you need me to put down a disc. I can...

Variables:

const double INT_RATE = 0.05;     //interest rate
    const double SVC_CHGE = 25.00;  //service charge if bal falls below 1000
    const double MIN_BAL = 1000.00;   //minimum balance allowed

    double begBal, wAmt, dAmt, xactAmt, intPaid, bal;  //wAmt = total withd amt, xact = transaction amt, intPaid = interest paid
    int acctNo, totWd, totDep;  // totWd = total numbers of withd
    char xactCode;  // transaction code - either a W D or I
    bool svcChgd = false; // if already been charged a service fee, switch to true.

Thanks guys!

Recommended Answers

All 8 Replies

The only thing I can see wrong with your code is this loop:

while (!inFile.eof())

What is the point of it since you never read anything else from the file? Furthermore, if the end of the file is reached before that statement, the whole switch() statement is completely skipped.

Maybe this will help; here is a sample inData file:


66476521 2375.44
w 250.00
W 156.73
D 1200
w 102.35
w 75.13
d 1023.29
w 19.46
W 249.89
q 567.88
w 109.37
i 12.45


am i useing the while statement wrong?

am i useing the while statement wrong?

Not really, except that you don't read input inside the loop. What's the point, then?

It's like this (pseduocode):

loop until EOF is reached
    process input that we got earlier
end loop

See, it doesn't do anything. Probably how I might do it is avoid the EOF function:

while (inFile >> xactCode >> xactAmt) {
   // do transactions here
}

Of course, this doesn't take into account (no pun intended) error checking. But that's a base you can work with. (You could instead read in lines one at a time and then parse them for bad characters, if you wanted to make sure that the file was valid.)

Ok, I took a break, and I've tried again; this time doing some pseduocode first.

Here's what I've got:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    const double INT_RATE = 0.05;
    const double MIN_BAL = 1000.00;
    const double SVC_CHG = 25.00;

    int totWd = 0 ;
    int totDep = 0 ;
    int acctNo ;

    double begBal ;
    double bal ;
    double wdAmt = 0.0 ;
    double depAmt = 0.0 ;
    double xactAmt ;
    double intPaid = 0.0;

    char xactCode ;

    bool svcChgd = false ;

    ifstream infile ;
    ofstream outfile ;

    infile.open("inData.txt") ;
    if (!infile)
    {
        cout << "!! INVALID INPUT FILE !!" << endl;
        return 1;
    }
    outfile.open ("outData.txt") ;

    infile >> acctNo >> begBal ;
    bal = begBal ;
    infile >> xactCode >> xactAmt ;

    while (infile)
    {
        switch (xactCode)
        {
        case 'D': case 'd':
            bal += xactAmt ;
            depAmt += xactAmt ;
            totDep++ ;
        case 'I': case 'i':
            bal += xactAmt ;
            intPaid += xactAmt ;

        case 'W': case 'w':
            bal -= xactAmt ;
            wdAmt += xactAmt ;
            totWd++ ;
            if ((bal < MIN_BAL) && (svcChgd = false))
            {
                bal -= SVC_CHG ;
                svcChgd = true ;
            }
        default:
            outfile << endl << "!! INVALID TRANSACTION CODE !!" << endl << endl ;
        }
    }

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

        cout << setfill('.') << left << setw(30) << "Account Number:  " << setfill(' ') << right << setw(10) << acctNo << endl << endl ;
        cout << setfill('.') << left << setw(30) << "Beginning Balance:  " << setfill(' ') << right << setw(10) << "$" << begBal << endl ;
        cout << setfill('.') << left << setw(30) << "End of Month Balance:  " << setfill(' ') << right << setw(10) << "$" << bal << endl ;
        cout << setfill('.') << left << setw(30) << "Interest Paid:  " << setfill(' ') << right << setw(10) << "$" << intPaid << endl ;
        cout << setfill('.') << left << setw(30) << "Total Ammount Deposited:  " << setfill(' ') << right << setw(10) << "$" << depAmt << endl ;
        cout << setfill('.') << left << setw(30) << "Total Number of Deposits:  " << setfill(' ') << right << setw(10) << totDep << endl ;
        cout << setfill('.') << left << setw(30) << "Total Ammount Withdrawln:  " << setfill(' ') << right << setw(10) << "$" << wdAmt << endl ;
        cout << setfill('.') << left << setw(30) << "Total Number of Withdrawls:  " << setfill(' ') << right << setw(10) << totWd << endl ;

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

        outfile << setfill('.') << left << setw(30) << "Account Number:  " << setfill(' ') << right << setw(10) << acctNo << endl << endl ;
        outfile << setfill('.') << left << setw(30) << "Beginning Balance:  " << setfill(' ') << right << setw(10) << "$" << begBal << endl ;
        outfile << setfill('.') << left << setw(30) << "End of Month Balance:  " << setfill(' ') << right << setw(10) << "$" << bal << endl ;
        outfile << setfill('.') << left << setw(30) << "Interest Paid:  " << setfill(' ') << right << setw(10) << "$" << intPaid << endl ;
        outfile << setfill('.') << left << setw(30) << "Total Amount Deposited:  " << setfill(' ') << right << setw(10) << "$" << depAmt << endl ;
        outfile << setfill('.') << left << setw(30) << "Total Number of Deposits:  " << setfill(' ') << right << setw(10) << totDep << endl ;
        outfile << setfill('.') << left << setw(30) << "Total Amount Withdrawn:  " << setfill(' ') << right << setw(10) << "$" << wdAmt << endl ;
        outfile << setfill('.') << left << setw(30) << "Total Number of Withdraws:  " << setfill(' ') << right << setw(10) << totWd << endl ;

        if (svcChgd = true)
            cout << endl << setfill('.') << left << setw(30) << "Service Charges Applied:  " << setfill(' ') << right << setw(10) << "$25.00" << endl ;
            outfile << endl << setfill('.') << left << setw(30) << "Service Charges Applied:  " << setfill(' ') << right << setw(10) << "$25.00" << endl ;
}

What is happening now, is I continue to get the
"!! INVALID INPUT FILE !!"
message. If I comment out the if (!infile) i get all the outputs but everything is 0.00 including the begBal.

I'm using the same inData.txt file as shown in the previous post... it is in the project directory.

Any help would be greatly appreciated! :D
Thanks

switch (xactCode)
        {
        case 'D': case 'd':
            bal += xactAmt ;
            depAmt += xactAmt ;
            totDep++ ;
        case 'I': case 'i':
            bal += xactAmt ;
            intPaid += xactAmt ;

        case 'W': case 'w':
            bal -= xactAmt ;
            wdAmt += xactAmt ;
            totWd++ ;
            if ((bal < MIN_BAL) && (svcChgd = false))
            {
                bal -= SVC_CHG ;
                svcChgd = true ;
            }
        default:
            outfile << endl << "!! INVALID TRANSACTION CODE !!" << endl << endl ;
        }

Look up the requirements for a switch/case segment. You have a very important piece missing in each case .

dang i missed the break; statements


I also moved the infile >> to under the while () statement... everything is working fine now.


Joe: Now I understand what you were trying to tell me earlier lol. Thanks for the help


thanks everyone for the help... this one is solved :D

check the files in the project director with Windows Explorer -- that inData file is not where you think it is.

Second problem: the individual cases in the while statements need to end with break statements, otherwise without it the code will just keep executing all the cases below it.

Third: how do you expect that while loop to terminate? Isn't it supposed to process each line of the inData.txt file? Should be coded like this:

while( infile >> xactCode >> xactAmt )
{
  // blaba
}

[edit] Ok, I was typing while you were posting corrected information. Since you've already fixed it just ignore my comments above. [/edit]

heheh, thanks!

btw: I was using a computer at school, which has "show file extensions" turned off. So I was looking for a inData.txt file but the file was actually called inData.txt.txt

silly computers.. >.<

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.