Thread: first C++ programm..

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Talking first C++ programm..

    Hi i have just started with C++ and did this small app...just wanted u guys to take a look at it and give me some point .And one more thing instead always typing using std:: etc etc...isnt there another way ..thanks

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    
    using std::cout;
    using std::endl;
    
    class Rectangle{
          public:
                 Rectangle();
                 int perimeter();
                 int area();
                 // set functions
                 void setlenght(int);
                 void setwidth(int);
                 // get functions
                 int getlenght();
                 int getwidth();
                 //draw function
                 void drawRec();
          private:
                  int lenght;
                  int width;
    };
    
    Rectangle::Rectangle(){ lenght = width = 1; }
    
    void Rectangle::setlenght( int l ){ lenght = ( l > 0 ) ? l : 0; }
    void Rectangle::setwidth( int w ){ width = ( w > 20 ) ? 20 : w; }
    
    int Rectangle::getlenght(){ return lenght; }
    int Rectangle::getwidth(){ return  width; }
    
    int Rectangle::area(){ return (lenght*width); }
    int Rectangle::perimeter(){ return 0; }
    
    void Rectangle::drawRec()
    {
         int array[25][25]={0} ;
    
         array[0][0] = 201;
         array[0][lenght-1] = 187;
         for ( int cnt_main = 1; cnt_main <= lenght-2; cnt_main++ )
             array[0][cnt_main] = 205;
    
    
         array[width-3][0] = 200;
         for ( int cnt_main = 1; cnt_main <= width-4; cnt_main++ )
             array[cnt_main][0]= 186;
    
    
         array[width-3][lenght-1] = 188;
         for ( int cnt_main = 1; cnt_main <= lenght-2; cnt_main++ )
             array[(width-3)][cnt_main] = 205;
    
         for ( int cnt_main = 1; cnt_main <= width-4; cnt_main++ )
             array[cnt_main][(lenght-1)] = 186;
    
        cout << "\n\n";
    
         for ( int cnt_main = 0; cnt_main <= width-1; cnt_main++, cout << "\n" ){
             for ( int cnt_sub = 0; cnt_sub <= lenght-1; cnt_sub++ ){
                 if(array[cnt_main][cnt_sub] == 201)
                     putchar(201);
                 else if ( array[cnt_main][cnt_sub] == 187 )
                    putchar(187);
                 else if ( array[cnt_main][cnt_sub] == 205 )
                    putchar(205);
                 else if ( array[cnt_main][cnt_sub] == 200 )
                    putchar(200);
                 else if ( array[cnt_main][cnt_sub] == 186 )
                    putchar(186);
                 else if ( array[cnt_main][cnt_sub] == 188 )
                    putchar(188);
                 else
                     cout << " ";
            }
       }
    
    }
    
    
    
    int main(void)
    {
       Rectangle t;
       int len, wid;
    
    
       cout << "Enter you length and width: ";
       cin >> len >> wid;
    
       t.setlenght(len);
       t.setwidth(wid);
    
       cout << "\nThe Area of rect with lenght "<< t.getlenght() << " and width is "
            << t.getwidth() << " is " << t.area() << endl;
    
       t.drawRec();
    
    
          system("PAUSE");
          return 0;
    }
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    as for the std:: stuff, you could do, "using namespace std;", without the quotes

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Hey thanks alot man .I needed that ..
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  4. #4
    booyakasha
    Join Date
    Nov 2002
    Posts
    208

    Re: first C++ programm..

    You spelt length wrong, but other than that you code looked good.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HexDump Programm....
    By Kdar in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 05:13 AM
  2. programm will not start
    By keeper in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2006, 06:02 AM
  3. printing with a c programm
    By -11Reaper11- in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2006, 10:31 AM
  4. windowsAPI programm
    By datainjector in forum Windows Programming
    Replies: 8
    Last Post: 03-11-2003, 11:08 PM
  5. I am looking for a programm!
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 11:51 AM