# Looping problem

• 09-01-2003
romeoz
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; }```
• 09-01-2003
XSquared
>>i <= 5;
i < 5;

And no, you aren't doing anything with arrays at all.
• 09-01-2003
dalek
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;                 }```
• 09-01-2003
ZakkWylde969
Doesn't he need to declare i as a variable?
• 09-01-2003
XSquared
He is.

for( int i...
• 09-01-2003
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...
• 09-01-2003
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.
• 09-01-2003
quzah
Quote:

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.

Curses, foiled again.[/edit]

Quzah.
• 09-01-2003
major_small
Quote:

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...