Ok, so I have an assignment that I have some questions on. Now, I can only use the while and if statement only, so no for loops or char types. Anyways, I have to write a program that completes a hollow square from 1-20 and with a q or Q to act as a sentinel. Now, when I try to compile it, I get nothing back. Any pointers on what I'm doing wrong. Here is the code below.
You can use while loops, you don't need to do that horrid thing with the if statements - you can simulate the behavior of a for loop using a while loop quite easily.
You are missing a lot of braces (at least 20 pairs, the braces for the n=1 and buf="Q" statements are optional as there is only 1 line of code to be executed).
Since you are creating squares (going by dimensions, a space is thinner than a *) every time, there is a much cleaner way of doing it using a couple of while loops and 1 if statement (rather than 20 if statements) - well ok, this has 3, but 1 is the check for quitting and the other is a range check.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
using std::getline;
#include <cstdlib>
int main()
{
string buf;
int n;
while (true) // yes this is an infinite loop, but allows multiple queries.
{
cout << "Enter a number between 1-20 to determine the size of the rectangle or press q to quit: " << endl;
cin >> buf;
n = atoi(buf.c_str());
cin.ignore(1000,10);
if (buf == "Q" || buf == "q")
break;
if (n>=1 && n<=20)
{
int rows=1;
{
while (rows<=n)
{
int columns=1; // set here as we want to reset this value every row
while (columns<=n)
{
if (columns==1 || columns==n || rows==1 || rows==n) //basically if it is on the perimeter
{
cout << "*";
}
else
{
cout << " ";
}
columns++; // add 1 to columns
}
cout << endl; // will only activate after every column in that row has been done.
rows++; // adds 1 to rows
}
}
}
}
return 0;
}
#include <iostream>
usingnamespace std;
void printSquare(int n)
{
int i = 0;
while (i < n)
{
cout << '*';
char c = ' ';
if (i == 0 || i == n - 1)
{
c = '*';
}
int j = 0;
while (j++ < n - 2)
{
cout << c;
}
if (n != 1)
{
cout << '*';
}
cout << endl;
i++;
}
}
int main()
{
char ans;
do
{
cout << "Enter the size of the square." << endl;
int n;
cin >> n;
printSquare(n);
cout << "Type q or Q to quit." << endl;
cin >> ans;
} while (tolower(ans) != 'q');
return 0;
}
A little thinking can save you a lot of writing.
Edit: also, how come you compile with VS but edit in Notepad? o.O
Just a minor point about the above post, there should be a check on one of the unconditional stars checking if n!=1, as it is, when you choose 1, it prints out "**".
Also, it would hardly take a billion years, 270 lines isn't much in serious project.
I don't know if manudude or josue noticed this but he clearly said he can't use for loops he can only use while loops. Though it would be a fairly easy transition between the two.
You can use while loops, you don't need to do that horrid thing with the if statements - you can simulate the behavior of a for loop using a while loop quite easily.
I know. That's what sucks so bad, is that the instructor is "introducing" us to the for loop and char statement next week. Meaning I can't use it for this one. It's just so confusing trying to coordinate string statements and words to match a particular pattern. I just don't get it. I understand complex formulas and calculations using the while and if statements, but how do you coordinate those strings in such a way?
This isn't that complicated... All you have to do is break it down into steps.... Think how you would type the square...
first you have the top row...
then you have 'x' rows that only have end/beginning borders...
Finally you have your last row...
The only variable here is length: how long are these rows? So I hope you can take it from there...
jackbruns28
I understand complex formulas and calculations using the while and if statements...
um... complex? I really don't think this surpasses basic algebra. The math gets a lot more complicated than this later when you start working on other interesting projects...
also, char declars a character. just like string declars a string. the difference between the two is that one is a class, and the other is not.