🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #175

Re: Continuation....Basic Loop Problem.

#175
From: Michael J. Tobler
Date: Tue, 20 Oct 1998 00:00
143 lines
6498 bytes
In article <362A7138.EE71830A@uswest.net>, berard@uswest.net says...
> Ok, thanks to everyones help and a lot of improvision, I have been doing
> very good on my basic loop problem.  Except right now I can't get it to
> do 2 things.  One, I need to get the average size (depending on how many
> times someone enters the size of the box) of the boxes entered, as well
> as my output still is'nt exactly what it needs to be.  The spacing/ -'s
> are right going across, but not the |'s going down.  Here's what I got:
I'd like to take a different slant on things. How about re-organizing the 
source code to take on functions?  More code organization is a key to 
isolating things ...
> 
> #include <iostream.h>
> #include <iomanip.h>
First of all, this needs to be: "int main()", not "void main()"
> void main (void)
> {
>         int length = 0, average = 1, count, totaltest = 0, totallength =
> 0, side, box = 0, space;
> 
>         char answer;
>         const int START = 1;
>         while (box == 0)
>         {
>                 cout << "Enter in the length of the square.  Enter -1 to
> quit\t";
>                 cin >> length;
> 
>                 cout << setiosflags(ios::fixed)
>                          << setiosflags(ios::right);
> 
>                 if (length < 1 && length != -1 || length > 24)
>                 {
>                         cout << "Invalid data.  The lenght must be
> [1-24].\n"
Just above, you correct the user, but you SHOULD have told them what the 
constraints are to begin with. First rule, NEVER CORRECT THE USER. You 
never told them what was valid to begin with.

You should break this stuff off into another function.

>                                  << "Enter in the length of the square.
> Enter -1 to quit\t";
>                         cin >> length;
>                 }
> 
>                 else if (length == -1)
>                 {
>                         count = 0;
>                         while (count == 0)
>                         {
>                                 cout << "Would you like to repeat the
> program?\t";
>                                 cin >> answer;

You should force the answer to upper case (or lower). You could also 
default some of the values and take the special case for each answer.
>                                 if (answer == 'y' || answer == 'Y')
>                                 {
>                                         box = 0;
>                                         count = 1;
>                                         length = 0;
>                                         break;
>                                 }
>                                 else if ( answer == 'n' || answer ==
> 'N')
>                                 {
>                                         box = 1;
>                                         count = 1;
>                                         break;
>                                 }
>                                 else
>                                 {
>                                         cout << "You have entered an
> invalid choice. Please try again." << endl;
Maybe make the above as a function that doesnt return until a valid choice 
is given:
while( NULL == strchr( "YN", answer ) )
>                                         count = 0;
>                                         length = 0;
>                                 }
>                         }
>                 }
> 
>                 if ( answer == 'n' || answer == 'N')
>                         break;
> 
>                 space = length - 1;
> 
Good point for another function ....
>                 switch (length)
>                 {
>                         case 0:
>                                 break;
> 
>                         case 1:
>                                 totaltest++;
>                                 totallength = totallength + 1;
>                                 cout << "+\n";
>                                 box = 0;
>                                 break;
> 
>                         case 2:
>                                 totaltest++;
>                                 totallength = totallength + 2;
>                                 cout << "++" << endl <<"++" << endl;
>                                 box = 0;
>                                 break;
> 
>                         default:
>                                 totaltest++;
>                                 totallength = totallength + length;
>                                 side = length-2;  // adjustment for
> larger boxes
>                                 cout << "+";
>                                 count = 0;
> 
>                                 for (count = 0; count < side; count++)
>                                         cout << "-";
> 
>                                 cout << "+\n";
>                                 count = 0;
>                                 for (count = 0; count < side; count++)
>                                         cout << "|" << setw(space) <<
> "|" << endl;
> 
>                                 cout << "+";
>                                 count = 0;
>                                 for (count = 0; count < side; count++)
>                                         cout << "-";
> 
>                                 cout << "+\n";
>                                 box = 0;
DONT forget the "break ;"
>                 }
>         }
> }

-- 
<<<<<<<<<<  Blue Skies  >>>>>>>>>>>
<        Michael J. Tobler        >
<     mtobler@no-spam-ibm.net     >
< remove "no-spam-" when replying >
<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>
Technical Details
Message-ID: <MPG.1096f7868177b4139899b1@news3.ibm.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <362A7138.EE71830A@uswest.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114592,374ef867f4da62c7
X-Google-Attributes: gid114592,public
X-Google-Thread: 10d4ba,374ef867f4da62c7
X-Google-Attributes: gid10d4ba,public
X-Google-Thread: 109fba,374ef867f4da62c7
X-Google-Attributes: gid109fba,public
From: mtobler@no-spam-ibm.net (Michael J. Tobler)
Subject: Re: Continuation....Basic Loop Problem.
Date: 1998/10/20
Message-ID: <MPG.1096f7868177b4139899b1@news3.ibm.net>
X-Deja-AN: 403381838
References: <362A7138.EE71830A@uswest.net>
X-Notice: should be reported to postmaster@ibm.net
X-Complaints-To: postmaster@ibm.net
X-Trace: 21 Oct 1998 01:41:17 GMT, 32.100.175.30
Organization: MJT
Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++