# A question about for loops

This is a discussion on A question about for loops within the C++ Programming forums, part of the General Programming Boards category; I am using a for loop to count down from 10 to 0 it's working to count down from 10 ...

1. ## A question about for loops

I am using a for loop to count down from 10 to 0 it's working to count down from 10 to 1 but when the program cames to the 0 then the program freezes by any reason can anyone help me find out what's wrong.

Code:
```#include <iostream>
using namespace std;
int main()
{
int number[2];
cout << "Enter number: ";
cin >> number[0];
if (number[0] == 1)
{
for (int i = 10; i < 20; i--) //Print the numbers from 10 down to zero
{
if (i > 0) //If i greater than zero then print this
{
cout << "This is your " << i <<  " try! " << endl;
cin >> number[1];
if (i == 0) //If i is equal to zero then exit
{
cout << "You exited!" << endl;
}
if (number[1] == 2)
{
cout << "You Won!" << endl;
}
}
}
}
return 0;
}```

2. your program is a bit messed up, but the answer to your question is you have an infinite for loop.

Try outputting i after the outer if statement closing brace within the loop and see what happens

3. What is your program supposed to do exacly?

4. OP's program is supposed to represent a game where there is a 'winning number' and the user has ten attempts to guess it correctly. The output is attempting to count down the number of tries remaining / used after each guess

5. but the answer to your question is you have an infinite for loop
Not quite an infinite loop, due to wrap-around, but a very long loop for sure.

6. @rogster001: Thanks

@OP:

1)
> intnumber[2];
> cin >> number[0];

Why would you use an array under such circunstances? Considering the input is supposed to be solely 1 number, you'd be better off doing it this way:
int number;
cin>>number;

2)
>
if(number[0] == 1)

I don't understand this piece of code. The program is supposed to close if the user inserts a number other than 1?

3)
Your loop itself is kinda messy. You'd be better off doing something like this:

Code:
```//why from 0 to 9(Or from 1 to 10)? i equals to the number of tries and should beggin with the 1st try, which is i=1 or i = 0 + 1
for (int i = 0; i < 10; i++)
{
//Logicly the number of attempts goes from the lowest to the highest, ain't that right?
//eg. 1st attempt, then 2nd attempt, etc etc.
switch (i)
{
case 0:
std::cout << "This is your 1st attempt\n";
break;
case 1:
std::cout << "This is your 2nd attempt\n";
break;
case 2:
std::cout << "This is your 3rd attempt\n";
break;
default:
std::cout << "This is your " << i + 1 <<"th attempt\n";
break;
}

//Here you get to input a number everytime the loop goes
int number;
std::cout << "Enter a number: ";
std::cin >> number;
std::cin.ignore();

//The following if conditions check whether the 10 attempts have been reached or if the user inserted the correct number (2)
if (number == 2)
{
std::cout<<"You won!";
break;
}

if (i == 9)
{
std::cout<<"You lose";
break;
}
}```

7. So your loop start at ten, goes down (i.e. 10, 9, 8 ...) and keeps going so long as the number is less than 20.
Is the problem obvious yet?

8. //Logicly the number of attempts goes from the lowest to the highest, ain't that right? //eg. 1st attempt, then 2nd attempt, etc etc.
Yes thats right, but the OP had the logic / language back to front, in more ways than one. Probably the idea was to count down like having a number of lives remaining

9. Hmm I changed my code a bit and this one works to print you exited when the number is 0 but I want the program to stop when number is 0 but instead the program continues to count down -1, -2 I want when the code is 0 you have no more tries and you have lost. The changed code looks like this:

Code:
```#include <iostream>
using namespace std;
int main()
{
int number[2];
cout << "Enter number: ";
cin >> number[0];
if (number[0] == 1)
{
for (int i = 10; i < 20; i--) //Print the numbers from 10 down to zero
{
if (i > 0) //If i greater than zero then print this
{
if (number[1] == 2)
{
cout << "You Won!" << endl;
}
}
cout << "This is your " << i <<  " try! " << endl;
cin >> number[1];
if (i == 0) //If i is equal to zero then exit
{
cout << "You exited!" << endl;
}
}
}
return 0;
}```

10. Originally Posted by DecoratorFawn82
I want the program to stop when number is 0
Tell it to do so then. Spell out the head of your loop in plain English. What does your loop say? What do you want it to say?

"for an integer named 'i' set to 10, as long as ... , execute the loop's body, then ..."

11. Read iMalc's reply again, and get rid of the array. Declare new variables as needed close to where you need them.

12. you still have the logic in your for loop wrong!