Temperature Conversion Program

This is a discussion on Temperature Conversion Program within the C++ Programming forums, part of the General Programming Boards category; I'm a beginner student learning about functions and do not understand them fully. My assignment was to write a program ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Temperature Conversion Program

    I'm a beginner student learning about functions and do not understand them fully. My assignment was to write a program that deals with temperature conversion. My program had to use 6 functions (1. Program overview 2. Enter temp 3. Enter scale 4. Convert F to C 5. Convert C to F 6. Display results) It also needs to display an error message if an inappropriate input is entered for both scale (which I have) and temperature (which I do not know where to place). Appropriate values for temps are >= -459.67 F or -273.15 C. I do not know where to place this scale either. I also have multiple errors in my program. If anyone could give me any guidance in helping me to fix my program, I would greatly appreciate it!!

    Code:
    #include <iostream>
    using namespace std;
    
    void programOverview ();
    char getScale ();
    int getDegree ();
    float convertFtoC (float);
    float convertCtoF (float);
    int getResults ();
    
    int main ()
    {
      cout.precision (2);
      programOverview ();
      getDegree ();
      getScale();
      {
      if (scale == "F")
        {
          convertFtoC();
        }
      else if (scale == "C")
        {
          convertCtoF();
        }
      else
        cout << "ERROR: Invalid temperature scale" << endl;
      }
      getResults();
    
      return 0;
    }
    
    //This function displays a brief overview explaining the program to the user
    void programOverview ()
    {
      cout << "This program will convert a temperature reading provided in" << endl;
      cout << "either Fahrenheit or Celsius to the other measurement scale." << endl;
    }
    
    //This function requires the user to enter the temperature scale to be used
    char getScale ()
    {
      char scale;
      cout << "Enter the letter of the temperature scale that will be used:" << endl;
      cout << "F = Fahrenheit; C = Celsius)" << endl;
      cin >> scale >> endl;
      return scale;
    }
    
    //This function requires the user to enter the temperature reading in degrees
    int getDegree ()
    {
      int degree;
      cout << "Enter your temperature reading in degrees:" << endl;
      cin >> degree;
      return degree;
    }
    
    //This function converts a Fahrenheit temperature to Celsius
    float convertFtoC (float Ftemp)
    {
      float Ctemp;
      Ctemp = (Ftemp - 32) / 1.8;
      return Ctemp;
    }
    
    //This function converts a Celsius temperature to Fahrenheit
    float convertCtoF (float Ctemp)
    {
      float Ftemp;
      Ftemp = 1.8 * Ctemp + 32;
      return Ftemp;
    }
    
    //This function displays the results
    {
      int getResults ()
      cout << "Your temperature reading converts as follows:" << endl;
      cout << "Fahrenheit:" << return Ftemp << endl;
      cout << "Celsius:" << return Ctemp << endl;
    }

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. getDegree() returns an integer but you aren't storing it anywhere. Not an error but it's against the rules.
    2. Same is the case for getScale().
    3. convertFtoC (float) is returning and accepting parameters, while you are not providing it.
    4. Same is the case with convertCtoF (float);
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Code:
    tempConversion.cpp:18:7: error: ‘scale’ was not declared in this scope
    You call getScale() but don't store the result in a variable called "scale" that you use on this line:
    Code:
      if (scale == "F")
    to fix it put this in:
    Code:
     char scale = getScale();
    I see that this is a misunderstanding of variable scope which is understandable since you're just now learning about functions. What you have to understand is that that "scale" variable that you create inside the getScale() function only exists while getScale() executes, and furthermore it is only recognised inside getScale(). To get that char value outside, you make that char the return value so you can assign it to a new "scale" variable inside main().

    Quote Originally Posted by Mr.777
    1. getDegree() returns an integer but you aren't storing it anywhere. Not an error but it's against the rules.
    Not really. There are many cases where it is appropriate to ignore a return value. Usually this is because you called the function for the side effect it produces. The return value gives you extra information/functionality but you choose not to use it. E.g.:

    Code:
    std::cout << "Hello";
    std::cout.flush();
    std:stream::flush() actually returns an ostream&, but it's often not used:
    ostream::flush - C++ Reference
    Last edited by Mozza314; 04-01-2011 at 12:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Conversion program
    By rupesh.rk in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2009, 03:47 AM
  2. Minute to hour/minute conversion program
    By Remius in forum C Programming
    Replies: 7
    Last Post: 12-29-2007, 08:39 AM
  3. Temperature Conversion code problems
    By eroth88 in forum C Programming
    Replies: 6
    Last Post: 10-22-2006, 02:24 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM

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