DSwain- The New Day

Tuesday, April 11, 2006

Learning C++: Function Jumping Method Using Only iostream

Over the past week or so, I've been teaching myself the programming language C++. For the most part, this is a language which almost everyone seems to know about to some degree (though, I have heard that Java has more projects on Sourceforge than they do C projects, but that's another story) and it's been a language which I've wanted to learn for some time. Sadly, though, C/C++ aren't exactly too nice for a first language, in my perspective. After running through with bash scripting, a little bit of Java, and a few others, I pretty much get the idea now. I decided to take the liberty of forcing myself to learn some C++.

One problem I've had with C++ though is getting used to Object Oriented Programming. I haven't even really done much of any, but I already have an issue of it. When you're building functions among a C++ program, it's not exactly easy to have those loops end and return to the original function. I decided to apply a little method I've also used a little bit in Bash to help me out on this one.

In this instance, I was working on a program which actually did something as opposed to just simply amuse and teach me. The program is pretty simple in theory; Write an application which can move a number starting in a base SI unit (grams, say) and have it calculate what it is as different SI units (kilograms, hektagram, etc.) Initially, I made it work simply by writing the entire code within main() but obviously, this was terribly sloppy looking and made the code just pile up real fast. My solution in the end was to break up each calculation into different functions and use main() just to make the calls to the functions. Once again though, my problem was it never making the jump back into main() like one would assume the code would do. So, my idea looked something like this.

(I'm not going to post all the code because it's almost 160 lines of it. I will when I get my webserver back online someday...)
void openapp () {
int unit;
system("clear");
cout << "Conversion Program\n\n";
cout << "Units of conversion\n\n";
unit=0;
while (unit<=5) {
cout << unit << ". " << si_unit[unit] << "\n";
++unit;
}
cout << "\nEnter a number (0-5) respectivly\n";
cout << "Press 6 to list the units again\n";
cout << "Press 7 to exit\n";
}

This was the function which essentially listed your options when you first open the program. I did this because as each function ends, it calls this function and lists your choices nice and clearly.
void kilo () {
float a,b;
cout << "Enter your base unit\n";
cout << "Press 0 to exit\n";
cin >> a;
while (a!=0) {
b=a*1000;
cout << "Base: " << a << "\n";
cout << "Kilo: " << b << "\n";
cout << "\nEnter your base unit\n";
cout << "Press 0 to exit\n";
cin >> a;
}
}

This is an example of one of the calculation functions. They all look pretty much the same with the exception of the actual formula changing. As you can see, I have the variable a handling whether the function should end or not. It continues until you enter zero simply because you shouldn't have an instance when you're calculating zero conversions. Bare in mind, though, that you can deal with very small amounts of material though because all the variables are floats so the decimals will not be truncated.
string si_unit[]={"kilo","hekto","deka","deci","centi","milli"};
This is just the string list of your conversion options.
int main () {
int unit;
system("clear");
openapp();
cin >> unit;
while (unit!=7) {
if (unit==6) {
openapp();
cin >> unit;
}
else if (unit==0) {
kilo();
openapp();
cin >> unit;

}
...

This is an excerpt of the main() function. It just gives you more options (numerically) as to what you want to convert to. Hitting six lists out what your possible choices are, and 7 exits the application entirely. As you can see, each function has to take input of the variable unit via the command cin >> unit;. This seems a bit tedious in general, but it does work.

Overall, this whole learning experience has been quite a rapidly paced one, and an interesting one to say the least. I'm glad I seemed to figure it out on my own, though. Kind of a nice accomplishment, though I'm sure many of you have even better methods of condensing and slicing down my code to be even tighter. There didn't seem to be a lot written on the issue either as far as I could tell. Maybe it was more obvious than I thought, but it seemed like everyone used different type of header files to do certain code jumps like this, so I decided this method was the best way to go.

By the way, for those of you who want to know, this actually did help chop my code down quite a bit. With this editing, not only does the code look more clearly written, but it also displays a little more clearly in a terminal window, keeps the same functionability as before, and reduced the amount of code from over 180 lines to just under 160. It was well worth the effort.

0 Comments:

Post a Comment

<< Home