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;
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.
#include <iostream>
usingnamespace 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.