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);
}

3 opmerkingen:

Double zei

I'd better start learning C++ if I were you. C++ is object-oriented, that may seem hard at first, but it gives quite cool possibilities. And C++ uses modern commands, like string, vector and cout.

Plus, KDE is written in C++. But you already know that, as a KDE-contributor ;)

Greets,
Double 12

Kristof Bal zei

I will, in fact I'm busy with it :). I already now the basic concepts of classes and OO.
The trouble is that I have a C book, but no (good) one for C++. I also don't like learning things from online tutorials.
Of course, I can print things out, but my printer won't like that ;).

Is there an easy-to-find book that combines C++ and Qt?

Greets
Kristof

Double zei

I'm learning from the book "Aan de slag met C++"...well, in fact, I did learn from it. When I started learning C++, I promised myself to read everything thoroughly, because before I always half-read programming books about Basic...finding myself with a half programming knowledge. ^^
But reading the C++ book intensively cost a lot of time, so I haven't exercised from it for a while now. I rather practise C++ by writing open-source software. Currently I'm using the combination C++/FLTK: http://launchpad.net/enjoy

I think the best combination is to learn C++ from a bought book and then learn the toolkit from online tutorials. After all, C++ toolkits aren't that hard...in most cases, a widget is a class, and the properties of the widget (eg. size, color, callback) are members of that class.

BTW: "Aan de slag met C++" is a good book, just a bit boring examples. profoX told me the book sucks because I knew nothing^^, but later he concluded that it was actually my understanding of the book that sucked.