Reducing my code

This is a discussion on Reducing my code within the C++ Programming forums, part of the General Programming Boards category; Hi I made a temperature conversion and the code is kinda many so i want to reduce it any idea ...

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    I would be less concerned with "reducing" the code as with making the code easier to read and understand.

    1. Indent your code properly.

    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?
    Last edited by laserlight; 01-08-2008 at 10:00 AM. Reason: Fixed inconsequential typo.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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];
    [Edit]
    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]
    Last edited by anon; 01-08-2008 at 04:06 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by King Mir View Post
    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?

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

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    In most cases, 1 or 2 decimal places is more than enough for scientific use
    yeah, right...
    What about errors addition?

    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    yeah, right...
    What about errors addition?

    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21