Hello, and thanks in advance for your time!
I am very new to C++, and am attempting the classic squares program. I have the meat of it all done, but am having issues with a few certain parts.
First, I can't figure out how to do the sentinel value. I tried adding int sentinel = -1;
, and then using that at the end of my do-while loop instead of repeating size > 1, as while (size != sentinel);
, but that didn't work. I also tried using const int EXIT_VALUE = -1
, but it didn't work either. How else can I make the program exit?
Second, I am having an issue with an input of 0. I am supposed to print a blank line, which should just be simple as anything, but its giving me heck. I tried copying the bit of code near the end that deals with an input of 1, and just changing the number and the first cout line, but that didn't work. What happens when I include this, however, is that the program prints the same output for 0 as it does for 1, just a single + sign. What am I doing wrong for this simple piece of code?
Thank you all for your time and your help!
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(int, char**) {
int size;
int i;
beginning: ;
do {
cout << "Enter length between 0 and 64 (-1 to exit): ";
cin >> size;
// top of square
if (size > 1) {
cout << "+" ;
i = 0;
while (i < size-2) {
cout << '-';
i = i+1; }
cout << "+" << endl;
// sides of square
int j; j = 0;
while (j < size-2) {
cout << "|" ;
i = 0;
while (i < size-2) {
cout << " " ;
i = i+1; }
cout << "|" << endl;
j = j+1; }
// bottom of square
cout << "+" ;
i = 0;
while (i < size-2) {
cout << '-';
i = i+1; }
cout << "+" << endl;
cout << "" << endl;
}
} while (size > 1);
if (size = 1) {
cout << "+" << endl;
cout << "" << endl;
goto beginning; }
system("pause");
return 0;
}