zondag 3 augustus 2008

My first useful C app!

I started studying C when I got an old C handbook from my father's school, so this was my chance to start to learn to code for real ;).

My first useful result is as a small application that prompts the user to enter an octal, decimal or hexadecimal value and shows its notation in those three ones.
It's license is BSD, so you can do what you want with it.

Here's the source:

# include

main()
{
int number;
int decision;
int intputoct();
int inputdec();
int inputhex();
int decider();
void output(int);
printf ("This application converts values from and to octal, decimal and hexadecimal.\n");
while (1==1) {
decision=decider();
if (decision==8) {
number=inputoct();
output(number);
}
else if (decision==10) {
number=inputdec();
output(number);
}
else if (decision==16) {
number=inputhex();
output(number);
}
else
printf ("Bad input.\n");
}
}

decider()
{
int type;
printf ("First, choose the type of value to convert.\n8 for octal, 10 for decimal and 16 for hexadecimal.\n");
scanf ("%d", &type);
return (type);
}

inputoct()
{
int oct;
printf ("Now, enter the octal value:");
scanf ("%o", &oct);
return (oct);
}

inputdec()
{
int dec;
printf ("Now, enter the decimal value:");
scanf ("%d", &dec);
return (dec);
}

inputhex()
{
int hex;
printf ("Now, enter the hexadecimal value:");
scanf ("%x", &hex);
return (hex);
}

void output(int number)
{
printf ("\nOCT:\t\t%o\nDEC:\t\t%d\nHEX:\t\t%x\n\n", number, number, number);
}