Thread: Newbie Question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Karsten
    Join Date
    Sep 2004
    Posts
    8

    Talking

    Hi Folks ,

    yes i have finisched my "work" on this converter ,but not as i previously wanted ,i switched over to the "swith/case" thingy instead of the if function ,made it easier in the end and of course i did get me some ideas how to manage it from some other example programs ,which is not cheating as i guess
    so here the final version :

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    void Start ();
    void GetResults();
    
    const float FRACTION = 1.80;       // Named constant definitions:
    
    
       char x;
       float fahrenheit;   // fahrenheit temperature
       float celsius;      // celsius temperature
    
       double cel,far,result;   // some variables
    
    void Start()
       {
    
           cout << " You can choose between those :\n";   // select Temp
           cout << " 1. Celsius \n";
           cout << " 2. Fahrenheit \n";
           cout << " 3. or any other Key to Exit \n";
           cin >> x;
           cout << "\n\n";
    
    
            switch (x)
            {
            case '1' : cel;  // getting the choice
            break;
            case '2' : far;
            break;
            default : exit(0);
            break;
            }
            GetResults();
    }
    void GetResults()
    {
              if (x=='1')                                        // Chooce to convert in Fahrenheit
              {
              cout << " You choose Option ( 1 ) \n";             // calculating Celsius in Fahrenheit
              cout << " Please enter a temperature in degrees Celsius: ";endl;
              cin >> cel;
              result = (cel * FRACTION ) + 32.0;
              cout << " " << cel << " degrees Celsuis are " << result << " degrees Fahrenheit\n";endl;
              cout << "\n";
               }
              if (x=='2')                                        // Choose to convert in Celsius
              {
              cout << " You choose Option ( 2 )\n";              // calculating Fahrenheit in Celsius
              cout << " Please enter a temperature in degrees Fahrenheit: ";endl;
              cin >> far;
              result = (far - 32.0 ) / FRACTION;
              cout << " " << far << " degrees Fahrenheit are " << result << " degrees Celsius\n";endl;
              cout << "\n";
              Start();
              }
               else if (x!= '1')
              {
               Start();
              }
               else if (x!='2')
              {
               Start();
              }
               else if (x!= '3')
              {
               Start();
              }
    
    }
    
    
    
    
          // Main program:
     int main()
    {
           cout.precision(4);
           cout << "*************************************\n";
           cout << "***    Temperature - Converter    ***\n";
           cout << "***         Designed by -         ***\n";
           cout << "***  +---- Karsten Mueller ----+  ***\n";
           cout << "***       in September 2004       ***\n";
           cout << "***            Version            ***\n";
           cout << "***             V 1.3             ***\n";
           cout << "*************************************\n";
           cout << endl;
           cout << endl;
         Start();
         return 0;
    }
    thanks again for all your help and ideas ,i sure will continue and learn more while iam doing as well .

    Hoschi

  2. #2
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    CornedBee and Hunter2,

    Thanks for that, it clears things up a lot.


    Hoschi,

    Glad you got your progam running. Just noticed a couple of things, if you don't mind me commenting. I'm still a beginner too but thought I'd pipe up anyway (if I'm wrong one of the more experienced folk can smack my nose for sticking it in where it doesn't belong yet ):

    - your switch/case doesn't seem to be doing anything; that is, it still looks like the if statements are controling the program flow; I don't think you need both... how about creating one function for going from cel to far and another one for far to cel and then calling them from the switch/case?

    - if you still want to go with ifs how about an "if, elseif, else" set instead of "if, if, elseif,elseif,etc."? Might streamline your code a bit.

    -
    Code:
    cout << " 3. or any other Key to Exit \n";
    this actually doesn't seem to exit the program, just takes it back to the start function

    - you've declared a couple of float variables (farhenheit and celsius) but I don't think you've used them in the program anywhere, though maybe I've missed them


    Ok, I'll shut up for now. Thanks for posting your code. Hope this helps a bit (and isn't too anoying ).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM