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.
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.
#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).
}
elseif ((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;
}
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?
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..