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;
}

#1: get rid of that goto statement (lines 12 and 52)-- its a sure way to get an F on that program.

To exit the do loop when -1 is entered, add this on line 17: if( size < 0) break; line 47: isn't 0 a valid input value? If so then while( size > -1 ) line 22: what will that do if I enter size of 1? Answer: nothing because (size - 2) = -1 and the initial value of i is 0. Also, you need to put brackets { and } around lines 22 - 24. I see you have several other places where you need to use brackets. Indenting code is not enough to make multiple lines all part of the same loop.

#1: get rid of that goto statement (lines 12 and 52)-- its a sure way to get an F on that program.

To exit the do loop when -1 is entered, add this on line 17: if( size < 0) break; line 47: isn't 0 a valid input value? If so then while( size > -1 ) line 22: what will that do if I enter size of 1? Answer: nothing because (size - 2) = -1 and the initial value of i is 0. Also, you need to put brackets { and } around lines 22 - 24.

Line 47: Yeah, 0 is valid, but it doesn't print out the same as everything else, like the square of size 1. If I change that to size > -1, it prints nothing for an input of 1 and skips the bit of code at the bottom that takes care of an input of 1 (lines 49-52).

Also, for the exit, I can't just have it exit if the input is less than 0, because for negative numbers besides -1 I need to print out an error message.

I wasn't really asking about what happens if i enter 1, because I figured that part out with the code at the bottom. I was asking how to make it print out a square of 0, and why I cant just use something resembling lines lines 49-52.

Line 22: Why do I need brackets there? I have them after while, enclosing everything in contained in the while loop.

Also, if I get rid of that goto, I don't know how else to get the code to loop if the input is 1, because that part is outside the do-while loop. So how would I work it out without a goto?

Thanks for your help!

line 22: Oh I see them now -- that's why I always put them on separate lines so that they stand out and can be easily seen. Don't hide them.

while (i < size-2)    
  {
         cout << '-';
         i = i+1; 
   }

line 49: if (size = 1) You are using the assignment operator not the boolean operator if (size == 1)

line 22: Oh I see them now -- that's why I always put them on separate lines so that they stand out and can be easily seen. Don't hide them.

while (i < size-2)    
  {
         cout << '-';
         i = i+1; 
   }

Yeah I need to get out of some bad formatting habits. I'll change all those.

line 49: if (size = 1) You are using the assignment operator not the boolean operator if (size == 1)

Ah, okay. i get so confused on when to use what. Thank you so much for all your help!!

That one confuses a lot of people -- just remember that on = is for assignment and two == is for asking a question.

just remember that on = is for assignment and two == is for asking a question.

I'm saving that in my C++ notes files for future reference. Thank you!! :)

Can I ask another question about this program?

I am supposed to print the number of inputs before exiting, as well as the average of those. I really don't know how to do this at all...is there a function I can use, or do I need to assign some integers like sum and average, or something else? I tried defining some integers like sum, counter, and average, but I couldn't figure out how to work with them correctly. I was getting negative numbers of inputs and averages that made no sense.

yes, you have to code your own, something like you mentioned. Did you initialize all those integers to 0 before attempting to use any of them ? If not they will start out to be any old random crap, whatever happens to be on the stack at the time.

If that doesn't fix the problem then post code.

yes, you have to code your own, something like you mentioned. Did you initialize all those integers to 0 before attempting to use any of them ? If not they will start out to be any old random crap, whatever happens to be on the stack at the time.

If that doesn't fix the problem then post code.

I think I only set two of them = 0...I don't know why, but I just didn't think the other needed it. I'll try that right now.

Okay...this is as much as I can get. Obviously its incorrect, because the program isn't printing anything at all, but I don't know what else to do. How can I use those the three lines that define the expressions for sum, counter, and average in a different way? I'm sure its just because I don't have somthing in the right spot.

I don't know how to put line numbers on...how do I do that?

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


int main(int, char**) {

   int size;
   int i;
   int counter=0;
   int sum=0;
   float average=0;
   
   for (size;counter;)   {
   beginning: ;
   
   do {
   
   cout << "Enter length between 0 and 64 (-1 to exit): ";
   cin >> size;
   cout << "Length entered: " << size;
   
   sum = sum + size;
   counter = counter + 1;
   average = sum/counter;
   
   if (size < -1 || size > 64) {
            cout << " is invalid." << endl;
            cout << "Length must be between 0 and 64 (inclusive), or enter -1 to exit." << endl;
            cout << "" << endl;
            goto beginning; }
   
   else if (size > -1 && size < 65)  {
            cout << "" << endl; }
   
   if (size == -1) { break;}
   
   // 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; } 
   
   if (size == 0) {
      cout << "" << endl;
      cout << "" << endl;
      goto beginning; }
      
   cout << counter << " squares printed. Average length: " << average << endl;
}   
   system("pause");
   return 0;
}

>>I don't know how to put line numbers on...how do I do that?

[code=cplusplus] // your code here

[/code]

put some print statements in various parts of the program to see that it is doing. Or learn to use your compiler's debugger so that you can execute the program one line at a time and see what it is doing.

Okay. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.