Squares Program

Can anyone help me? I have no idea what to do..
This is my assignment:

Write a program to do the following:
• request that the user input a number between 1 and 10. Handle out-of-range inputs by informing the user of an error and repeating the input request. Loop until you get a valid input. How you allow the user to exit is up to you.
• For valid input, output a square made of hyphens and vertical bars, with the user input determining the length of a side. For example, a square of side=3 is
---
| |
---
For a size of 1, the program should output a single asterisk. It is up to you how a size 2 square is built, as long as it is two characters wide and two lines high.The program should work for squares of all sizes between 1 and 10. It should be designed so that it can be extended to larger squares by changing the existing code, without adding any new executable lines.




This is what I have so far...

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
int main(int argc, char** argv) {
	
	int num, i;
	char space = ' ';
	
	cout << "Enter a number between 1 and 10. ";
	cin >> num;
	
	i = num;
	
	if(num == 1)
	{
		cout << "*" << endl;
	}
	else if ((num >= 1) && (num <= 10))
	{
		for (num=0;num<i;num++)
		{
			cout << "-";
		}
		cout << endl;
		for (num=0;num<i;num++)
		{
			cout << "|"<< space * num << "|" << endl;	
		}
		for (num=0;num<i;num++)
		{
			cout << "-";
		}
	}
	return 0;
}
Last edited on
Can you at least do some of this yourself? Can you take user input for example... Show some attempt and we will help with what you're stuck with. We are not going to do the entire project for you, nor can we guide you well with what specific things you are stuck with after you simply post your assignment.
This is what I have so far...


#include <iostream>

using namespace std;

int main(int argc, char** argv) {

int num, i;

cout << "Enter a number between 1 and 10. ";
cin >> num;

i == num;

if(num == 1)
{
cout << "*" << endl;
}
else if ((num >= 1) && (num <= 10))
{
for (num=0;num<=i;num++)
{
cout << "-";
}
}

return 0;
}
Please use code tags in future! Here is some help, with a few minor points added in comments and a few major points in comments.
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
#include <iostream>

using std::cout; //This is much better than using namespace std;
using std::cin;  //using namespace std is exposing much more than you actually need.

int main(int argc, char** argv) {

int num, i;
//Maybe you can modify this so that it is contained in a while loop, which will not exit
//until the user has put in a number from 1-10?
cout << "Enter a number between 1 and 10. ";
cin >> num;

i = num; // '=' is the assignment operator i.e. Assign value in int num to int i.
// '==' compares two numbers i.e. is i equal to num? 

if(num == 1)
{
    cout << "*\n";//"\n" for a newline is better than std::endl, which flushes the stream too (it's little excessive).
}
else if ((num >= 1) && (num <= 10))
{
    for (num=0;num<=i;++num)
    {
        cout << "*"; //You should see your loop now outputs some stars in a line. 

//Can you figure how to make it print out a square instead? 
        //A hint - You need to output a newline and then the same row of stars num times. 
    }
}

return 0;
}


Last edited on
Thanks that helps, but I can do every thing except for the actual coding for the square inside the for loop.
If the number is 4 then the output should be:
----
| |
| |
----

How do I code the spaces and new lines for this without changing it for every new input?
Alright this is what I have now..

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
int main(int argc, char** argv) {
	
	int num, i;
	char space = ' ';
	
	cout << "Enter a number between 1 and 10. ";
	cin >> num;
	
	i = num;
	
	if(num == 1)
	{
		cout << "*" << endl;
	}
	else if ((num >= 1) && (num <= 10))
	{
		for (num=0;num<i;num++)
		{
			cout << "-";
		}
		cout << endl;
		for (num=0;num<i;num++)
		{
			cout << "|"<< space * num << "|" << endl;	
		}
		for (num=0;num<i;num++)
		{
			cout << "-";
		}
	}
	return 0;
}
Last edited on
You can't multiply space times num, it will output a number..
Well how do I output that amount of spaces? I would need to subtract 2, as well as for the vertical lines; however, subtracting two is not working.
Why are you talking about spaces?

To start with, forgetting the bars and hyphens requirement, can you make a user defined size square, say out of asterixes?
I'm talking about the spaces in the middle of the square.
I can get the square to look like this:

----
| |
| |
| |
| |
----

but whenever I try to change the code to move the second vertical bar over to the side of the square instead of in the middle, or I try to make it 2 less vertical bars than the number input it makes my square go crazy..
Topic archived. No new replies allowed.