-
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;
}
-
>Is there a way to clean it up to reduce size (in number of lines and kbs)?
Yes.
-
Well, do you think you or someone could be cool and not a smart aleck and show me? =) Thank you.
-
>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? :p 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();
}