Looping problem

This is a discussion on Looping problem within the C++ Programming forums, part of the General Programming Boards category; I am trying to keep my balance and make it loop for 5 times then give the current balance. What ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    Looping problem

    I am trying to keep my balance and make it loop for 5 times then give the current balance. What did I do wrong? Also is this what a five-element one-dimensional array is? I dont have my book with me so I'm running into problems. thanks

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    
    	float InitBal;
    	float CheckAmount;
    	float DepositAmount;
    	float CurBal;
    
    
    	cout << "Enter Initial Balance of 1st Customer that banks with Bank Of Bryan:";
    	cin >> InitBal;
    	cout << "Write A Check For A Specified Amount:";
    	cin >> CheckAmount;
    	cout << "Deposit A Specified Amount Into The Checking Account:";
    	cin >> DepositAmount;
    	
    		CurBal = InitBal - CheckAmount + DepositAmount;
    
    		for(int i=0;i<=5;i++)
    
    		{	
    		cout << "The current balance of this customer is:" << CurBal << endl;
    		}
    
    
    	return 0;
    
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>i <= 5;
    i < 5;

    And no, you aren't doing anything with arrays at all.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    A 5 element one dimensional array is declared as:
    Code:
    float myArray[5];
    Once you have filled the array elements with data, you will iterate through them like such:
    Code:
    for (int x = 0; x < 5; x++)
    {
        cout << "Value : " << myArray[x] << endl;
    }
    You should be very careful with the loop. In the code you provided (below), if you did have a five element array you would have moved one past the bounds of the array. Remember you declare the array as such : myArray[5], which means you can access the elements with an index of 0 through to 4. Your loop in fact iterates 6 times, 0, 1, 2, 3, 4, 5 due to your condition in the for loop <= 5. It should be < 5.
    Code:
    for(int i=0;i<=5;i++)
    
    		{	
    		cout << "The current balance of this customer is:" << CurBal << endl;
    		}

  4. #4
    ___
    Join Date
    Jun 2003
    Posts
    806
    Doesn't he need to declare i as a variable?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    He is.

    for( int i...
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    this is probably just me, but i wouldn't do that... it's easy to miss and you might forget you already have i defined, if you want to use it in a later loop... I would either declare it with everything else at the top or use a pointer and delete it after the loop...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >> you might forget you already have i defined, if you want to use it in a later loop...
    Actually, i's scope is only inside of the loop, so you don't really need to worry about that.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by major_small
    this is probably just me, but i wouldn't do that... it's easy to miss and you might forget you already have i defined, if you want to use it in a later loop... I would either declare it with everything else at the top or use a pointer and delete it after the loop...
    Actually, what would happen would be a redefinition of the same variable. In which case, it would take the one with the closest scope (the one declared in the loop in this case).

    Why on earth would you declare a pointer and delete it after the loop? That makes no sense.

    [edit]Curses, foiled again.[/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by XSquared
    >> you might forget you already have i defined, if you want to use it in a later loop...
    Actually, i's scope is only inside of the loop, so you don't really need to worry about that.
    really? i never really tried it... makes sense now that I think about it...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21