Square Display

A practice question from the C++ textbook.

Write a program that asks for a positive integer no greater than 15. The program should then display a square on the screen using the character 'X'. The number entered by the user will be the length of each side of square. For example, if the user enters 5, the program should display the following:

XXXXX
XXXXX
XXXXX
XXXXX
XXXXX

Here's what I have so far from reading up to chapter 5:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main ()
{
	int number;
	cout<<"Enter a positive integer no greater than 15: "<<" "<<endl;
	cin>>number;
	for (int number=1; number<=15; number++)
	{
		for (int number=1; number<=15; number++)
		{
			cout<<'X';
		}
		cout<<endl;
	}
system("pause");
return 0;
}


I need help figuring out what I am doing wrong and how I can fix it.
Last edited on
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.

Last edited on
This is the new code with all the corrections that you have pointed out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main ()
{
	int number;
	cout<<"Enter a positive integer no greater than 15: "<<" "<<endl;
	cin>>number;
	if (number < 1)
        number = 1;
    else if (number > 15)
        number = 15;
	for (int i=0; i <number; i++)
	{
		for (int j=0; j <number; j++)
		{
			cout<<'X';
		}
		cout<<endl;
	}
system("pause");
return 0;
}


Okay 'i' stands for outer loop, or in this case row. And then 'j' is the inner loop, which in this cases means columns. Ahh that's what I was missing, and the if statements.

Thank you very much for your pointers and the example question. It helped a lot.


number = 5
number = 6
number = 7
number = 8
number = 9
number = 123


Since you put int number = 5, then it will start at 5. And then it will continue to display numbers 5-9, 9 is the limit due to number<10, number++ is telling the program to continue adding one everytime until it reaches the limit you set. Then the last cout will display 123. I think I get it now for the most part. Thanks again.

Although the if and else statement is still confusing me a bit. I need more practice on those.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream.h>
int main()
{
    int a, b, c;
    cout<<"enter the number\n";
    cin>>a;
    if(a<15&&(a>0))
    {
    for(b=1;(b<=a);++b)
    {
                     for(c=1;(c<=a);++c)
                     {
                     cout<<"x";
                     }
                     cout<<"\n";
                     }
                     system("pause");
                     }
                     else
                     cout<<"error\n";
                     return 0;
                     }
                     
                     

well , according to me ,this is exactly what you need!
@abeginner23235616

Close, but not exactly right.
(a) if the user input is 15 (which is valid), the error message is displayed.
(b) system ("pause") is called only after displaying the square. When there is an error message, the program immediately terminates.
(c) The indentation needs a lot of attention. Proper indentation aids legibility, and makes the logical flow more clear which can help to avoid logic errors.
Topic archived. No new replies allowed.