# Reducing my code

• 01-08-2008
limitmaster
Reducing my code
Hi I made a temperature conversion and the code is kinda many so i want to reduce it any idea how can i do it? tnx in advance. Here's the code..

Code:

```#include <iostream> #include <conio.h> #include <stdlib.h> using namespace std; main(){ float ctemp, ftemp, ktemp, rtemp, c, f, k, r; int ch; char con, num; do{ cout<<"1. Celsius\n" <<"2. Fahrenheit\n" <<"3. Kelvin\n"<<"4. Rankine\n"; cout<<"Choice: "; cin>> ch; system("cls"); switch(ch){     case 1: cout <<"Enter Temperature in Celsius: ";     cin>> ctemp;     cout<<"Convert to:?\n";     cout<<"a. Fahrenheit\n" <<"b. Kelvin\n" <<"c. Rankine\n";     cout<<"Choice: ";     cin>> con;     switch(con){         case 'a': f = (1.8*ctemp) + 32;         cout << "Temperature in Fahrenheit = " << f << endl; break;         case 'b': k = ctemp + 273.15;         cout << "Temperature in Kelvin = " << k << endl; break;         case 'c': r = (ctemp + 273.15) * 1.8;         cout << "Temperature in Rankine = " << r << endl; break;         default: cout<<"Wrong choice exiting...\n"; exit(1);break;     }     break;     case 2:     cout <<"Enter Temperature in Fahrenheit: ";     cin>> ftemp;     cout<<"Convert to:?\n";     cout<<"a. Celsius\n" <<"b. Kelvin\n" <<"c. Rankine\n";     cout<<"Choice: ";     cin>> con;         switch(con){         case 'a': c = (ftemp - 32) * 0.56;         cout << "Temperature in Celsius = " << c << endl;         break;         case 'b': k = 273 + ((ftemp - 32.0) * (5.0/9.0));         cout << "Temperature in Kelvin = " << k << endl;         break;         case 'c': r = ftemp + 460;         cout << "Temperature in Rankine = " << r << endl;         break;         default: cout<<"Wrong choice exiting...\n"; exit(1);         break; }      break;      case 3:     cout <<"Enter Temperature in Kelvin: ";     cin>> ktemp;     cout<<"Convert to:?\n";     cout<<"a. Celsius\n" <<"b. Fahrenheit\n" <<"c. Rankine\n";     cout<<"Choice: ";     cin>> con;         switch(con){         case 'a': c = ktemp - 273.15;         cout << "Temperature in Celsius = " << c << endl;         break;         case 'b': f = ((ktemp - 273.15) * (1.8)) + 32;         cout << "Temperature in Fahrenheit = " << f << endl;         break;         case 'c': r = ktemp * 1.8;         cout << "Temperature in Rankine = " << r << endl;         break;         default: cout<<"Wrong choice exiting...\n"; exit(1);         break; } break;     case 4:     cout <<"Enter Temperature in Rankine: ";     cin>> rtemp;     cout<<"Convert to:?\n";     cout<<"a. Celsius\n" <<"b. Fahrenheit\n" <<"c. Kelvin\n";     cout<<"Choice: ";     cin>> con;         switch(con){         case 'a': c = (rtemp / 1.8) - 273.15;         cout << "Temperature in Celsius = " << c << endl;         break;         case 'b': f = rtemp - 460;         cout << "Temperature in Fahrenheit = " << f << endl;         break;         case 'c': r = ktemp * 1.8;         cout << "Temperature in Kelvin = " << r << endl;         break;         default: cout<<"Wrong choice exiting...\n"; exit(1);         break;     }    } cout<<"Do you want to continue (y/n): "; cin>> num; }while(num=='y'); getch(); }```
and btw what's the formula in converting rankine to kelvin i have already searched in the internet and found nothing.. lol i don't even know if my formulas area correct.
• 01-08-2008
laserlight
I would be less concerned with "reducing" the code as with making the code easier to read and understand.

2. Use functions. The temperature conversion formulae are a good starting point.

3. Use descriptive names. With the given context I can have a good guess what ctemp, ftemp, ktemp and rtemp mean, but what on earth is the significance of c, f, k, r, con and num?
• 01-08-2008
anon
1. Don't attempt to reduce code length by cramming more stuff onto one line. Give it more air. This concerns the inner switches.

2. You could do it with only two switches (instead of nesting a switch inside each case of the outer switch) and reduce redundancies if you first converted the input to a common unit - such as Kelvins - in the first switch and then from that intermediate result to the selected units in the second switch.
(Or you might try tables of function pointers to get rid of switches entirely :) )

3. An array of unit names might also be helpful because this way the messages become the same instead of having a number of unique messages:
Code:

`cout << "Enter temperature in " << unit_name[selection];`

I noticed that you don't allow conversions to the same units. That might be harder to achieve because you'll need to skip one unit and take into account that the user input does not map directly to the four available units.

It might be wise to make a special function for presenting the user with options, which also lets you skip one option and maps the user's choice to the number associated with each temperature unit. (Basically add 1 to user input if the selected index is greater or equal to that of the skipped item.)

For that the third suggestion may be unavoidable.
[/Edit]
• 01-08-2008
King Mir
In addition to what others have said:

1) use doubles not floats. Not really a code length problem, but it will make the answers more accurate.

2) Separate user interface from the algorithms. You should use a separate function for each convention.
• 01-09-2008
matsp
Quote:

Originally Posted by King Mir
In addition to what others have said:

1) use doubles not floats. Not really a code length problem, but it will make the answers more accurate.

I think that it won't matter for this purpose - float has about 6-8 digits that are correct, and unless you are trying to determine the difference between a Helium atom moving once a second and a Helium atom moving twice a second [that being EXTREMELY close to 0 Kelvin], then the difference in precision will not be sufficient to warrant using doubles for this purpose. In most cases, 1 or 2 decimal places is more than enough for scientific use, and if you just want to know the temperature to know what clothes to wear, about +/- 2 degrees C will be sufficient.

Of course, once temperatures reach "inner parts of an hydrogen bomb explosion" you may find that the last few places above the decimal point are no longer precise, but do you REALLY care if it is 31241134 or 31241138 degrees C?
Quote:

2) Separate user interface from the algorithms. You should use a separate function for each convention.
This I certainly agree with.

--
Mats
• 01-09-2008
vart
Quote:

In most cases, 1 or 2 decimal places is more than enough for scientific use
yeah, right...

If you have A with precision of 0.01 and B with presision of 0.01
A+B will have precision of 0.02

A few number of additional operations and the result has a precision of 0.1. You really think it is enough for Scientific calculations?

Look at the Calculator program in the Scientific Mode. ;)
• 01-09-2008
matsp
Quote:

Originally Posted by vart
yeah, right...

If you have A with precision of 0.01 and B with presision of 0.01
A+B will have precision of 0.02

A few number of additional operations and the result has a precision of 0.1. You really think it is enough for Scientific calculations?

Look at the Calculator program in the Scientific Mode. ;)

Yes, but with a float number, you get 6-8 digits for the whole number, so unless you have temperatures in the range of 10000 degrees, _AND_ still want to get two decimal digits, then you do need double.

I said "scientific use", by which I mean to "measure and use temperatures" in science. For example, if we want to melt gold, it melts at 1064 degrees C - do you need to know more than two decimal places for this?

Obviously, if you are doing calculations where the value is being used for further calculations, etc, you will need some extra precision to preserve the precision of the final value. But for all uses of temperatures that I can think of, a float can be used.

--
Mats
• 01-09-2008
limitmaster
first of all sorry for the delay in replying because im at school. im really sorry...
About ur comments and advice guys tnx a lot it helped. About functions i've read it lately and im planning on using it so gonna go now and do coding..^^.. Thanks to all again...