Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3

Author Topic: C++ Program issue  (Read 3002 times)

Tobel

  • Bay Watcher
    • View Profile
    • Tobel Plays Youtube
C++ Program issue
« on: February 15, 2011, 05:27:38 pm »

Hi there.

I've been pestering my TA's for a few days about the problem with my current program, but they've not responded yet. I'm approaching the crunch period (due tomorrow at noon) and I would really like to get this thing figured out. I was hoping someone could look at the code and point me in the direction of finding what I screwed up.

The basic objective of the program is to record and track transactions that the users enter. There will only be five types of input.
Initial balance: integer
-1 (terminates the program)

Three types of transactions:
ATM follows this format: 2008 1 22 -200 0 (5th number is always a 0)
Checks are: 2008 1 23 -150 3356 (check number varies)
Deposits are: 2008 1 25 200 (No 5th number)

The year will always be 2008, but the month/date will vary, along with the amount. Deposit is always positive, atm/check is negative.

The problem right now is that the program isn't terminating when the -1 is entered. I've tried a do while loop, a while loop, I've tried throwing an if statement in the body of the loop to break when -1 is detected. You have to use -1 for all 5 entries before it breaks.


Code: [Select]
int main()


{
    double startBalance, endBalance, maxBalance, minBalance, amountAtm, amountDep, amountCheck, amount, totalLoops, balance;
    int year, month, day, startMonth, spCol,  startYear, startDay, lastMonth, lastYear, lastDay, numAtm, numCheck, numDep;

    numDep = 0;
    numAtm = 0;
    numCheck = 0;
    amountAtm = 0;
    amountDep = 0;
    amountCheck = 0;
    totalLoops = 0;
    startBalance = 0;
    startYear = 0;
    startDay = 0;
    startMonth = 0;

    cin >> startBalance;
    balance = startBalance;
    maxBalance = balance;
    minBalance = balance;

    while(year != -1)
            {

    cin >> year;
    cin >> month;
    cin >> day;
    cin >> amount;

    if(amount < 0)
        {
            cin >> spCol;
            if(spCol == 0)
                numAtm ++, amountAtm = (amountAtm + amount), balance = (balance + amount);
            else
                numCheck ++, amountCheck = (amountCheck + amount), balance = (balance + amount);
        }
    else
        numDep ++, amountDep = (amountDep + amount), balance = (balance + amount);


    if(balance > maxBalance)
        maxBalance = balance;
    else
        if(balance < minBalance)
            minBalance = balance;

    totalLoops ++;
    }
    lastYear = year;
    lastMonth = month;
    lastDay = day;
    endBalance = balance;

    cout << "Starting Balance: " << startBalance << endl;
    cout << "Ending Balance: " << endBalance << endl;
    cout << "\n";
    cout << "Start Transaction: " << startYear << " " << startMonth << " " << startDay << endl;
    cout << "Ending Transaction: " << lastYear << " " << lastMonth << " " << lastDay << endl;
    cout << "\n";
    cout << "Number of Deposits: " << numDep << " | Total: " << amountDep << endl;
    cout << "Number of Checks: " << numCheck << " | Total: " << amountCheck << endl;
    cout << "Number of ATMs: " << numAtm << " | Total: " << amountAtm << endl;
    cout << "\n";
    cout << "Minimum Balance: " << minBalance << endl;
    cout << "Maximum Balance: " << maxBalance << endl;

    return 0;

}
Logged

Frajic

  • Bay Watcher
    • View Profile
Re: C++ Program issue
« Reply #1 on: February 15, 2011, 05:41:41 pm »

I'm not a C++ programmer, but from your code I think the problem might lie in that the loop checks if year != -1 after the loop has finished. Isn't there some sort of function that exits a loop that you can use to end the program?
Logged
EoS company name: Vikings Inc.

Tobel

  • Bay Watcher
    • View Profile
    • Tobel Plays Youtube
Re: C++ Program issue
« Reply #2 on: February 15, 2011, 05:46:34 pm »

You can do an if (year == -1) break I imagine, I had done that earlier but the program kept breaking with any input.
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: C++ Program issue
« Reply #3 on: February 15, 2011, 05:48:14 pm »

My C is rusty as well, but I think this makes sense.  I just moved the reading of the year to outside the loop and made a duplicate entry at the end of the loop.  It also has the advantage that year isn't undefined when you first compare it.  :)

Spoiler (click to show/hide)
« Last Edit: February 15, 2011, 05:50:50 pm by Levi »
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: C++ Program issue
« Reply #4 on: February 15, 2011, 05:55:17 pm »

That works, but it's ugly. It'd be cleaner to just stick an if (year == -1) break; immediately after reading the year input. If that doesn't work as intended, check the initial value for year.
The reason you have to give 5 values in this case is that initially year == 0 so year != -1. Then the  program while execute the whole while loop before rechecking the predicate and thus you need to give all values.
This reminds me that you can probably also do an in-line expression in the predicate of the while loop: while ({cin >> year } != -1) (at least I think that's the proper syntax. The inline expression in this case should hopefully return the value assigned to year, but don't pin me on that)
« Last Edit: February 15, 2011, 06:01:26 pm by Virex »
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: C++ Program issue
« Reply #5 on: February 15, 2011, 06:06:43 pm »

I'm ashamed to admit I couldn't remember the break syntax for c++.  :)
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

eerr

  • Bay Watcher
    • View Profile
Re: C++ Program issue
« Reply #6 on: February 15, 2011, 06:09:11 pm »

cin>> year

before the loop
and at the very end, inside the loop.

If you want the year to be checked in time to stop the extra loop.
Logged

Tobel

  • Bay Watcher
    • View Profile
    • Tobel Plays Youtube
Re: C++ Program issue
« Reply #7 on: February 15, 2011, 06:21:50 pm »

Thanks for the advice! I'll throw that break right after the cin >> year if it equals -1 and see what we get.
Logged

DJ

  • Bay Watcher
    • View Profile
Re: C++ Program issue
« Reply #8 on: February 16, 2011, 05:52:22 am »

You can keep year input at the start if you really want.

while(year != -1)
cin >> year;
if(year != -1)
{
//the rest of the loop
}

So if -1 is entered, the rest of the loop won't execute. I think this looks better, breaks are kinda ugly IMO.
Logged
Urist, President has immigrated to your fortress!
Urist, President mandates the Dwarven Bill of Rights.

Cue magma.
Ah, the Magma Carta...

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: C++ Program issue
« Reply #9 on: February 16, 2011, 04:56:41 pm »

But an extra layer of indentation isn't?
Logged
Eh?
Eh!

Cecilff2

  • Bay Watcher
  • PikaaAAAAあああああ
    • View Profile
Re: C++ Program issue
« Reply #10 on: February 16, 2011, 05:29:51 pm »

Breaks are ugly.

I'd go with the

cin >> year
cout << "Whatever it's 2009!"
while(year != -1)
{
   cin >> other vars
   stuff
   cin >> year
}
« Last Edit: February 16, 2011, 05:34:38 pm by Cecilff2 »
Logged
There comes a time when you must take off the soft, furry slippers of a boy and put on the shoes of a man.
Unless of course they don't fit properly and your feet blister up like bubble wrap.
Oh ho ho, but don't try to return the shoes, because they won't take them back once you've worn them.
Especially if that fat pig Tony is at the desk.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: C++ Program issue
« Reply #11 on: February 16, 2011, 05:44:25 pm »

And doing the exact same thing twice (even redundantly here) isn't ugly? I already pointed out that it is possible to do an inline assignment here (though my syntax was wrong. It should've been while((cin >> year) != -1)), which imho would express exactly what you want to do here.
Logged

Cecilff2

  • Bay Watcher
  • PikaaAAAAあああああ
    • View Profile
Re: C++ Program issue
« Reply #12 on: February 16, 2011, 05:53:55 pm »

And doing the exact same thing twice (even redundantly here) isn't ugly? I already pointed out that it is possible to do an inline assignment here (though my syntax was wrong. It should've been while((cin >> year) != -1)), which imho would express exactly what you want to do here.

Pretty sure evaluating cin will result in true/false. (Input/no input)

But it'd work if he just changed the instructions to "Don't input anything for year to end the loop"

while(cin >> year)
{
   stuff
}
Logged
There comes a time when you must take off the soft, furry slippers of a boy and put on the shoes of a man.
Unless of course they don't fit properly and your feet blister up like bubble wrap.
Oh ho ho, but don't try to return the shoes, because they won't take them back once you've worn them.
Especially if that fat pig Tony is at the desk.

DJ

  • Bay Watcher
    • View Profile
Re: C++ Program issue
« Reply #13 on: February 16, 2011, 06:05:49 pm »

I like indentation :(
Logged
Urist, President has immigrated to your fortress!
Urist, President mandates the Dwarven Bill of Rights.

Cue magma.
Ah, the Magma Carta...

kg333

  • Bay Watcher
  • Derp.
    • View Profile
    • Steam Profile
Re: C++ Program issue
« Reply #14 on: February 27, 2011, 02:16:12 am »

cin>> year

before the loop
and at the very end, inside the loop.

If you want the year to be checked in time to stop the extra loop.

This is correct.  Your previous value for the year variable is being checked at the beginning of the loop.  If the test is false, all of your cin statements are executed before year is checked again.  Move the "cin >> year;" statement to the end of the loop to ensure it's checked in time, and make sure that you have an additional "cin >> year;" statement just before the loop so the value is valid for the first while test.

It's known as "sentinel logic", by the way, if you'd like to look into it further.

KG
Logged
Pages: [1] 2 3