For Loop inside A While loop

Hi im suppose to write a for loop inside a while loop where i ask the user to input a number between 0 and 501. and then when the number input is in the range im suppose to write a for loop that shows the even numbers between 0 and the number picked. This is what i have so far.

#include <iostream>
using namespace std;

int main()
{
// Display header (Name , program title, and program objective)
cout << "Miguel Lopez" << endl << "Quiz 2 - conditionals and loops""\n";

cout << "Successfully making a program with conditionals and loops""\n";

//Ask the user for a number between 0 and 501
1
2
3
 int number;
    int count = 1;
    while (count < 500)

{
 cout << "Enter a number between 0 and 501: ";
        cin >> number;
        if (number < 1 || number > 500)
        {
            cout << "\nOUT OF RANGE!";
        }

        
    }

return 0;
}
Last edited on
Use code tags.
Hello miguelminaj,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



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
#include <iostream>

using namespace std;

int main()
{
    // Display header (Name , program title, and program objective)
    cout << "Miguel Lopez" << endl << "Quiz 2 - conditionals and loops""\n";
    cout << "Successfully making a program with conditionals and loops""\n";

    //Ask the user for a number between 0 and 501
    int number;
    int count = 1;

    while (count < 500)
    {
        cout << "Enter a number between 0 and 501: ";
        cin >> number;

        if (number < 1 || number > 500)
        {
            cout << "\n     OUT OF RANGE!\n";
        }

        // <--- What do you do if the number is in range?
        // <--- And where does the value of "count" change??
    }

    return 0;
}


Andy
This might help but if you decide to use it make sure you check and test 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()
{
    // Display header (Name , program title, and program objective)
    cout
    << "Miguel Lopez\n"
    << "Quiz 2 - conditionals and loops\n"
    << "Successfully making a program with conditionals and loops\n";

    //Ask the user for a number between 0 and 501
    int number{0};
    bool keep_on_entering{true};
    while (
           keep_on_entering == true &&
           cout << "Enter a number between 0 and 501: " &&
           cin >> number
           )
    {
        if( (number < 1 || number > 500) )
        {
            keep_on_entering = true;
            cout << "OUT OF RANGE!\n";
        }
        else
        {
            keep_on_entering = false;

            for(int i = 0; i < number; i++)
            {
                // blah blah
            }
            cout << "finished\n";
        }
    }

    return 0;
}


Miguel Lopez
Quiz 2 - conditionals and loops
Successfully making a program with conditionals and loops
Enter a number between 0 and 501: 0
OUT OF RANGE!
Enter a number between 0 and 501: -2
OUT OF RANGE!
Enter a number between 0 and 501: 501
OUT OF RANGE!
Enter a number between 0 and 501: 500
finished
Program ended with exit code: 0
Last edited on
Hello miguelminaj,

I am not sure what you expect for output but I did come up with this:

Miguel Lopez
Quiz 2 - conditionals and loops
Successfully making a program with conditionals and loops

Enter a number between 0 and 501: 0

     OUT OF RANGE!

Enter a number between 0 and 501: 600

     OUT OF RANGE!

Enter a number between 0 and 501: 20
  2   4   6   8  10  12
 14  16  18

Enter a number between 0 and 501: 43
  2   4   6   8  10  12
 14  16  18  20  22  24
 26  28  30  32  34  36
 38  40  42

Enter a number between 0 and 501: 65
  2   4   6   8  10  12
 14  16  18  20  22  24
 26  28  30  32  34  36
 38  40  42  44  46  48
 50  52  54  56  58  60
 62  64



Andy
Consider:

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
#include <iostream>
#include <limits>
using namespace std;

int main()
{
	cout
		<< "Miguel Lopez\n"
		<< "Quiz 2 - conditionals and loops\n"
		<< "Successfully making a program with conditionals and loops\n";

	int number {};

	while ((cout << "Enter a number between 0 and 501: ") && (!(cin >> number) || number < 1 || number > 500)) {
		if (!cin) {
			cout << "Not a number\n";
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
		} else
			cout << "Not in range\n";
	}

	for (int i = 0; i <= number; i += 2)
		cout << i << ' ';

	cout << '\n';
}

Topic archived. No new replies allowed.