Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-13-2002, 11:12 AM   #1 (permalink)
w00t
Totally Inept
 
w00t's Avatar
 
Join Date: Jul 2002
Location: The Great Northwest
Posts: 195
w00t is on a distinguished road
How to add an error statement

How do I add a statement so that when i input a number other than 1-5, the program doesn't fly into a tangent and instead gives an error message?
Also, is there anything cool that I can add to this program to make it better?


Code:
#include <iostream.h>

typedef unsigned short int USHORT;
typedef unsigned long int ULONG;
enum BOOL {FALSE, TRUE};
enum CHOICE {DrawRect = 1, GetArea,
   GetPerim, ChangeDimensions, Quit};
// Rectangle Class Declaration
class Rectangle
{
   public:
   //constructors
   Rectangle(USHORT width, USHORT Height);
   ~Rectangle();

   //accessors
   USHORT GetHeight() const { return itsHeight;}
   USHORT GetWidth() const  { return itsWidth; }
   ULONG GetArea () const  { return itsHeight * itsWidth; }
   ULONG GetPerim () const { return 2*itsHeight + 2*itsWidth; }
   void SetSize (USHORT newWidth, USHORT newHeight);

   // Misc. Methods
   void DrawShape() const;

   private:
     USHORT itsWidth;
     USHORT itsHeight;
};

//Class method implementations
 void Rectangle::SetSize(USHORT newWidth, USHORT newHeight)
{
   itsWidth = newWidth;
   itsHeight = newHeight;
}


Rectangle::Rectangle(USHORT width, USHORT height)
{
   itsWidth = width;
   itsHeight = height;
}

Rectangle::~Rectangle() {}

USHORT DoMenu();
void DoDrawRect(Rectangle);
void DoGetArea(Rectangle);
void DoGetPerim(Rectangle);

void main()
{
  //initalize a rectangle to 10,20
  Rectangle theRect(30,5);

  USHORT choice = DrawRect;
  USHORT fQuit = FALSE;

  while (!fQuit)
  {
    choice = DoMenu();
    if (choice < DrawRect || choice > Quit)
    {
      cout <<"\nInvalid Choice, please try again.\n\n";
      continue;
   }
   switch (choice)
   {
   case DrawRect:
     DoDrawRect(theRect);
     break;
   case GetArea:
     DoGetArea(theRect);
     break;
   case GetPerim:
     DoGetPerim(theRect);
     break;
   case ChangeDimensions:
     USHORT newLength, newWidth;
     cout <<"\nNew width:";
     cin  >> newWidth;
     cout << "New Height: ";
     cin  >> newLength;
     theRect.SetSize(newWidth, newLength);
     DoDrawRect(theRect);
     break;
   case Quit:
     fQuit = TRUE;
     cout <<"\nExiting...\n\n";
     break;
   default:
     cout <<"Error in choice!\n";
     fQuit = TRUE;
     break;
     }  //end switch
   }    //end while
}       //end main


USHORT DoMenu()
{
  USHORT choice;
    cout <<"\n\n***Menu*** \n";
    cout <<"(1) Draw Your Rectangle\n";
    cout <<"(2) Area\n";
    cout <<"(3) Perimeter\n";
    cout <<"(4) Resize\n";
    cout <<"(5) Quit\n";

 cin >> choice;
 return choice;
}

void DoDrawRect(Rectangle theRect)
{
  USHORT height = theRect.GetHeight();
  USHORT width = theRect.GetWidth();

  for (USHORT i = 0; i<height; i++)
   {
     for (USHORT j = 0; j<width; j++)
    cout <<"*";
    cout<< "\n";
  }
}


void DoGetArea (Rectangle theRect)
{
  cout <<"Area: " <<theRect.GetArea() << endl;
}

void DoGetPerim (Rectangle theRect)
{
cout <<"Perimeter: " <<theRect.GetPerim() << endl;
}
:rock:
__________________
Office Space:
Best Movie.
Ever.

Contrary to popular belief, the true function of a programmer
is to turn coffee into source code.
w00t is offline   Reply With Quote
Old 08-13-2002, 11:29 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
I think I made an answer to a question like this..
In your DoMenu() make a check for what you have read, see this thread on what we came up with last:
http://www.codenewbie.com/forum/show...=&threadid=253
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-13-2002, 12:26 PM   #3 (permalink)
w00t
Totally Inept
 
w00t's Avatar
 
Join Date: Jul 2002
Location: The Great Northwest
Posts: 195
w00t is on a distinguished road
Man I hate Kdevelop. Borland may be in windows, but it's worth it.
__________________
Office Space:
Best Movie.
Ever.

Contrary to popular belief, the true function of a programmer
is to turn coffee into source code.
w00t is offline   Reply With Quote
Old 08-13-2002, 02:26 PM   #4 (permalink)
w00t
Totally Inept
 
w00t's Avatar
 
Join Date: Jul 2002
Location: The Great Northwest
Posts: 195
w00t is on a distinguished road
Cool. So can i just throw that statement in anywhere in my program?
Pardon my ignorance
__________________
Office Space:
Best Movie.
Ever.

Contrary to popular belief, the true function of a programmer
is to turn coffee into source code.
w00t is offline   Reply With Quote
Old 08-13-2002, 10:54 PM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
As your DoMenu() is made now, it looks like:
Code:
USHORT DoMenu()
{
  USHORT choice;
    cout <<"\n\n***Menu*** \n";
    cout <<"(1) Draw Your Rectangle\n";
    cout <<"(2) Area\n";
    cout <<"(3) Perimeter\n";
    cout <<"(4) Resize\n";
    cout <<"(5) Quit\n";

 cin >> choice;
 return choice;
}
If you wan't to be sure, it only returns with an integer within the 1 - 5 selection, then make it:
Code:
USHORT DoMenu()
{
  USHORT choice = 0;
   while(choice < 1 || choice > 5)
   {
      cout <<"\n\n***Menu*** \n";
      cout <<"(1) Draw Your Rectangle\n";
      cout <<"(2) Area\n";
      cout <<"(3) Perimeter\n";
      cout <<"(4) Resize\n";
      cout <<"(5) Quit\n";
      cin >> choice;
      if(choice < 1 || choice > 5)
          cout << "Sorry your choice of (" << choice
                  << ") is not within the required limit."<<endl
   }
 return choice;
}
Note the above has not been testet..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding numbers doesn't add up !!! help arwilliams HTML, XML, Javascript, AJAX 3 09-30-2004 10:17 AM
MySQLDriverCS and VC++.NET milena Platform/API C++ 10 07-21-2004 11:56 AM
Add an Assembly Reference - Mono twixntwix MS Technologies ( ASP, VB, C#, .NET ) 4 07-08-2004 08:00 AM


All times are GMT -8. The time now is 09:04 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting