Infinite loop

Hello, I have been trying to get this right for a while but I just cant seem to get out of the loop. Any ideas on how I can break out of it? This is the problem:

Ask the user for a number less than 500. If the user enters a number equal or greater than 500, do not continue.
If the user entered a number 499 or less, display the message:
"PROGRAMMING IS FUN" the number of times depending on the number entered.
For every multiple of three, add " AND PROFITABLE!" to the output.
For example:
Enter a number:4
output:
PROGRAMMING IS FUN
PROGRAMMING IS FUN
PROGRAMMING IS FUN AND PROFITABLE!
PROGRAMMING IS FUN

This is what I have so far:

#include <iostream>

using namespace std;

int main()

{
int number;
int counter = 1;

cout << "Please enter a number less than 500" << endl;
cin >> number;


while (number < 500)

{

for (int counter2 = 1; number >= 0; counter++)
{
cout << "PROGRAMING IS FUN!" << endl;

if (counter % 3 == 0)

cout << "PROGRAMING IS FUN AND PROFITABLE!" << endl;
counter++;
if (number > counter2)
break;

}
}

return 0;
}






The problem is your for loop.

 
for (int counter2 = 1; number >= 0; counter++)


number will always be >= 0, so the condition is always true and you loop will never terminate.

It should be:
 
for (int counter2 = 1; counter <= number; counter++)


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.

Your "if(counter % 3 == 0)" has no brackets around it, nothing will be output from this if statement

EDIT:: If you want your loop to go the amount of times equal to the number entered, your final "if" statement needs to read
1
2
if (number == counter2)
     break;

Last edited on
Thank you for the quick response Parasin. I fixed the loop and placed brackets. Now it will not do anything. Anything else you find wrong with it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

int main()
{

  int number;
    int counter = 1;

    cout << "Please enter a number less than 500" << endl;
    cin >> number;


    while (number < 500)

    {

        for (int counter2 = 1; number <= 0; counter++)
        {
            cout << "PROGRAMING IS FUN!" << endl;



        if (counter % 3 == 0)
            {
            cout << "PROGRAMING IS FUN AND PROFITABLE!" << endl;
            counter++;
}
        if (number == counter2)
        break;

        }
    }



 return 0;
}
counter2 is never incremented in one of your loop, so it's pretty much useless. It's never going to be equal to number unless the user inputs 1.

Your for loop condition, number <=0, isn't going to help you either. You're asking the user to input a number between 0 and 499, so the only time your condition is true is if the user inputs 0. Try setting your condition to counter <= number.

Thank you annatar
Topic archived. No new replies allowed.