Thread: clean up?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    9

    clean up?

    The following is a code where just threw together a lot of stuff i leaned recently. Is there a way to clean it up to reduce size (in number of lines and kbs)?
    It originally was the 'please enter your age:' code from the tuts (on this site, i believe). I attached the file so you can see what it does, i'm happy about it =p i guess everyone likes their first program, even if they didn't write everything in it.

    Code:
    #include <iostream>
    #include <conio.h>
    #include <stdio.h> 
    #include <windows.h> 
    using std::cout;
    using std::cin;
    using namespace std;	
    
    int main()				//Most important part of the program!
    
    {
    
      cout<<"Enter any age 0-??? at the prompt to get a response."<<endl;
      cout<<"Try different numbers to get different responses."<<endl;
      int age;				//Need a variable...
      
      
          {
                HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
                WORD wOldColorAttrs;
                  CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
    
                                                /*
                                                * First save the current color information
                                                */
               GetConsoleScreenBufferInfo(h, 
    
    &csbiInfo);
               wOldColorAttrs = csbiInfo.wAttributes; 
    
                                                  /*
                                                   * Set the new color information
                                                   */
               SetConsoleTextAttribute ( h, FOREGROUND_RED| FOREGROUND_INTENSITY );
    
               printf ( "Now enter your age:" );
    
                                          /*
                                          * Restore the original colors
                                          */
                SetConsoleTextAttribute ( h, wOldColorAttrs);
    
        }
    
      cin>>age;				//The input is put in age
      if(age<35)				//If the age is less than 45
      {
         cout<<"You are young!";     //Just to show it works
      }
      else if(age<65)		//I use else just to show an example 
      {
         cout<<"You are old!";		//Just to show you it works...
      }
      else if (age<80)                 //If the age is less than 80
      {
        cout<<"You are really old!";
      
      }  
      else if (age<100)              //If the age is less than 100
      {
        cout<<"You are really SUPER old!";
      
      }
      else if (age<=200)               //If the age is less than 200
      {
        cout<<"How are you STILL kickin!?";
      
      }
        else if(age>200)               //just else
      {
        cout<<"You've defied God. Congrats!";
      
      }
    
      cin.ignore();         //Until int age2; this is all statements to user.
      cin.get();
      cout<<"See! Now enter a different number!"<< endl;  
      cout<<"0-34 is one response."<< endl;
      cout<<"35-64 is another."<< endl;
      cout<<"65-79 is another. Have fun!"<< endl;
      cout<<"81-99 is another..."<< endl;
      cout<<"100-200 is just crazy! And you can go beyond!"<< endl;       //statements to user are done
    
        int age2;				//VARIABLE here; Same code as int age;
        
        {
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
      WORD wOldColorAttrs;
      CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
    
      /*
       * First save the current color information
       */
      GetConsoleScreenBufferInfo(h, &csbiInfo);
      wOldColorAttrs = csbiInfo.wAttributes; 
    
      /*
       * Set the new color information
       */
      SetConsoleTextAttribute ( h, FOREGROUND_RED| FOREGROUND_INTENSITY );
    
      printf ( "Now enter your age:" );
      
      /*
       * Restore the original colors
       */
      SetConsoleTextAttribute ( h, wOldColorAttrs);
    
        }
    
        cin>>age2;	
          if(age2<35)				//If the age is less than 45
      {
         cout<<"You are young!";     //Just to show it works
      }
      else if(age2<65)		//I use else just to show an example 
      {
         cout<<"You are old!";		//Just to show you it works...
      }
      else if (age2<80)                 //If the age is less than 80
      {
        cout<<"You are really old!";
      }  
      else if (age2<100)              //If the age is less than 100
      {
        cout<<"You are really SUPER old!";
      }
      else if(age2<=200)               //just else
      {
        cout<<"How are you STILL kickin!?";
      }
      else if(age2>200)               //just else
      {
        cout<<"You've defied God. Congrats!";
      }
      cin.ignore();
      cin.get();
      cout<<"See!? Thanks for using this proggy!"<< endl;
      cout<<"Press enter to exit the program.";
      getch();
      return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there a way to clean it up to reduce size (in number of lines and kbs)?
    Yes.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    9
    Well, do you think you or someone could be cool and not a smart aleck and show me? =) Thank you.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Well, do you think you or someone could be cool and not a smart aleck and show me? =)
    Hey, I answered your question, didn't I? The best way to clean up your code is to modularize it:
    Code:
    #include <cstdio> 
    #include <iostream>
    #include <windows.h> 
    
    using namespace std;
    
    void f ( int age )
    {
      if(age<35)
        cout<<"You are young!";
      else if(age<65)
        cout<<"You are old!";
      else if (age<80)
        cout<<"You are really old!";
      else if (age<100)
        cout<<"You are really SUPER old!";
      else if (age<=200)
        cout<<"How are you STILL kickin!?";
      else if(age>200)
        cout<<"You've defied God. Congrats!";
    }
    
    void console_thingie ( HANDLE& h, WORD& w, CONSOLE_SCREEN_BUFFER_INFO& c )
    {
      GetConsoleScreenBufferInfo(h, &c);
      w = c.wAttributes; 
      SetConsoleTextAttribute ( h, FOREGROUND_RED| FOREGROUND_INTENSITY );
    }
    
    void console_reset ( HANDLE h, WORD w )
    {
      SetConsoleTextAttribute ( h, w );
    }
    
    void do_age ( HANDLE h, WORD w )
    {
      int age;
    
      cout<<"Now enter your age:";
      cin>> age;
      cin.ignore();
      console_reset ( h, w );
      f ( age );
    }
    
    void shtuff()
    {
      HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
      WORD w;
      CONSOLE_SCREEN_BUFFER_INFO c;
    
      console_thingie ( h, w, c );
      do_age ( h, w );
    }
    
    int main()
    {
      cout<<"Enter any age 0-??? at the prompt to get a response."<<endl;
      cout<<"Try different numbers to get different responses."<<endl;
      shtuff();
      cout<<"See! Now enter a different number!"<< endl;  
      cout<<"0-34 is one response."<< endl;
      cout<<"35-64 is another."<< endl;
      cout<<"65-79 is another. Have fun!"<< endl;
      cout<<"81-99 is another..."<< endl;
      cout<<"100-200 is just crazy! And you can go beyond!"<< endl;
      shtuff();
      cout<<"See!? Thanks for using this proggy!"<< endl;
      cout<<"Press enter to exit the program.";
      cin.get();
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Establishing 'make clean' with GNU make
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 04-11-2009, 09:10 AM
  2. how to know that the buffer is not clean??
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 01-21-2009, 09:56 PM
  3. prepro. clean tool / #id#def mess
    By tomsky in forum Linux Programming
    Replies: 2
    Last Post: 09-08-2005, 07:18 AM
  4. clean out a buffer
    By threahdead in forum C Programming
    Replies: 8
    Last Post: 02-23-2003, 01:04 PM
  5. make clean
    By Jaguar in forum Linux Programming
    Replies: 5
    Last Post: 12-27-2002, 06:44 PM