There are two separate parts to this. First "
asks for a positive integer no greater than 15"
You need to validate the user's input to see that it is (a) positive and (b) no greater than 15. If the input is outside that range, then at the least, you need to force the input to a default value in the specified range, or perhaps better, use a loop, to keep asking the user to enter the value until they get it right (which can annoy the user, but is perhaps more correct).
For example, we could put something like this:
1 2 3 4
|
if (number < 1)
number = 1;
else if (number > 15)
number = 15;
|
The second part is, having now got our validated input number, then we need to use it to "
display a square on the screen".
The standard code for that part would use two nested loops, much as you currently have, but instead of the current line 8:
for (int number=1; number<=15; number++)
you would have something like this:
for (int i=0; i<number; i++)
... for the outer loop, and the inner loop would be similar, but using a different variable such as
int j
instead of
int i
There are a couple of points to note about your code. Not only do you always loop from 1 to 15 (thus ignoring the user's input), there's a more subtle error too.
That is, on line 5 of your code is defined
int number
. But on line 8 inside the for loop, a separate variable
int number
is defined. This is a completely new variable and has no connection with the earlier number. Again on line 10 there is another new
int number
defined. Thus the code has three completely different and independent variables, all of them called "number", but each having its own independent existence.
To verify what I just said, consider this code:
1 2 3 4 5 6
|
int number = 123;
for (int number = 5; number<10; number++)
{
cout << "number = " << number << endl;
}
cout << "number = " << number << endl;
|
What do you think will be output on lines 4 and line 6 of this code? If you're not sure, then please try it.