1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Remedial c++

  1. Oct 13, 2005 #1
    I know I am not a moron but this C++ is killing me, I have to write a program using functions to convert temp between c, f, k I am hung up

    // Mod4project2tempconvert.cpp : Defines the entry point for the console application.
    #include <iostream>
    using namespace std;

    double ftemp (double F, double C, double K, double temp, double scale)

    {
    while (scale != 0)
    {
    if (scale==C);
    F=temp*9/5+32;
    K=temp+273.15;

    if (scale==F);
    C=(temp-32)*5/9;
    K=(temp-32)*5/9+273.15;

    if (scale==K);
    F=(temp-273.15)*9/5+32;
    C=temp-273.15;
    }
    }


    int main()
    {
    double F, C, K, temp, scale;
    cout<<"Enter the scale you want to convert from C, F, or K- 0 to quit: ";
    cin >> scale;

    cout<<"\nEnter temperature";
    cin >> temp;
    cout<<"\nCongratulations you entered "
    <<scale<<"The following results are your converted temperatures "
    <<ftemp(C, F, K)<<endl;

    return 0;
    }

    Any help?
    Thanks
     
  2. jcsd
  3. Oct 13, 2005 #2
    // Mod4project2tempconvert.cpp : Defines the entry point for the console application.
    #include <iostream>
    using namespace std;

    double ftemp (double F, double C, double K, double temp, double scale)

    {
    while (scale != 0)
    {
    if (scale==C)
    {
    F=temp*9/5+32;
    K=temp+273.15;
    }

    if (scale==F)
    {
    C=(temp-32)*5/9;
    K=(temp-32)*5/9+273.15;
    }

    if (scale==K)
    {
    F=(temp-273.15)*9/5+32;
    C=temp-273.15;
    }
    }
    }


    int main()
    {
    double F, C, K, temp, scale;
    cout<<"Enter the scale you want to convert from C, F, or K- 0 to quit: ";
    cin >> scale;

    cout<<"\nEnter temperature";
    cin >> temp;
    cout<<"\nCongratulations you entered "
    <<scale<<"The following results are your converted temperatures "
    <<ftemp(F, C, K)<<endl;

    return 0;
    }

    I only get one complier error:ftemp does not require 3 arguments
    ........
     
  4. Oct 13, 2005 #3

    ranger

    User Avatar
    Gold Member

    Code (Text):
    Mark J. Merritt
    Temperature Conversion Calculator
    Wednesday, April 11, 2001
    */
    #include <iostream.h>
    #include <math.h>
    #include <iomanip.h>
    #include <conio.h>
    int main()
    {
    int choice;
    float celsius, fahrenheit, kelvin, temp;
    char response;
    do
    {
    // Clear screen
    clrscr();
    // Get temperature
    cout << "This is a temperature converter." << endl << endl
    << "Enter a temperature to convert: ";
    cin >> temp;
    // Display choices on which temperatures to convert
    cout << endl << endl << "1. Celsius to Fahrenheit" << endl
    << "2. Celsius to Kelvin" << endl << "3. Fahrenheit to Celsius"
    << endl << "4. Fahrenheit to Kelvin" << endl << "5. Kelvin to Celsius"
    << endl << "6. Kelvin to Fahrenheit" << endl;
    // Get temperature conversion choice
    cout << endl << "Please select a choice from above: ";
    cin >> choice;
    cout << endl << endl;
    // Calculate values based on the choice selected
    /*
    for the formaulas, please see
    http://library.thinkquest.org/20991/gather/formula/data/129.html
    */
    switch (choice)
    {
    case 1:
    fahrenheit=(9.0/5.0)*temp+32;
    cout << temp << " degrees Celsius is equal to " << fahrenheit
    << " degrees Fahrenheit.";
    break;
    case 2:
    kelvin=temp+273.16;
    cout << temp << " degrees Celsius is equal to " << kelvin
    << " degrees Kelvin.";
    break;
    case 3:
    celsius=(5.0/9.0)*(temp-32);
    cout << temp << " degrees Fahrenheit is equal to " << celsius
    << " degrees Celsius.";
    break;
    case 4:
    kelvin=((5.0/9.0)*(temp-32)+273.16);
    cout << temp << " degrees Fahrenheit is equal to " << kelvin
    << " degrees Kelvin.";
    break;
    case 5:
    celsius=273.16-temp;
    cout << temp << " degrees Kelvin is equal to " << celsius
    << " degrees Celsius.";
    case 6:
    fahrenheit=((temp-273.16)*(9.0/5.0))+32;
    cout << temp << " degrees Kelvin is equal to " << fahrenheit
    << " degrees Fahrenheit.";
    break;
    default:
    cout << "That was not a valid choice.";
    break;
    }
    // Ask to loop back or end program
    cout << endl << endl
    << "Would you like to convert another temperature (Y/N)? ";
    cin >> response;
    }
    // Program loops back if user chooses yes
    while (response=='y' || response=='Y');
    // Program ends if any other choice other than yes is entered
    cout << endl << endl << "This program has been terminated.";
    return 0;
    }
     
    The above is an example program. Hope it helps. I wrote one similar to this when I was learning, but it is in C.
     
    Last edited: Oct 13, 2005
  5. Oct 13, 2005 #4

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    What help do you want? What reason do you have to think that this is not perfectly good? You seem to have difficulty explaining what you want- even in the program you have 'cout<<"Enter the scale you want to convert from C, F, or K- 0 to quit: ";'
    If I were running the program and saw that message I would be think that I am supposed to enter the letters "C", "F", or "K"- which would then get me an error message because the program is expecting numerical input. What numerical input should I give if I want to convert from Centigrade?

    Also, I notice that, although you declare ftemp to return a "double" type, it doesn't actually return anything! You seem to think that the variables, C, K, F will change- they won't, you are calling by "value" here. The function gets copies of whatever numbers are in C, K, and F and works with them- the actual values in C, K, F don't change.

    What you need to do is call by "reference". Decalare
    null ftemp (double &F, double &C, double &K, double temp, double scale)
    so that the values are pointers to the values in memory. The "&" here tells the function that it will be getting pointer rather than the actual values but you can, in the function itself treat F, C, K as if they were the actual values, rather than having to use *F, *C, *K to access the values from the pointers.

    In the main program you will need ftemp(&F, &C, &k, temp, scale).
    The operator "&" gives a pointer to each variable rather than the variable itself.

    I'm still not sure what you want for "scale".
     
  6. Oct 13, 2005 #5

    ranger

    User Avatar
    Gold Member

    Code (Text):
    double ftemp (double F, double C, double K, double temp, double scale)
     
    Dont you have to put a semicolon (;) at the end of your statement?
     
  7. Oct 13, 2005 #6

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    I didn't notice this until after my remarks about "passing by value" and "passing by reference" (which still stand):
    Well, yes, you would! You defined your function by

    double ftemp (double F, double C, double K, double temp, double scale)

    which takes 5 parameters, and your call is

    ftemp(F, C, K) which has only three. What happened to "temp" and "scale"?
     
  8. Oct 24, 2005 #7

    dpm

    User Avatar

    I don't know whether you still need help, but you should look into enumerated types and switch statements. Your program can be written in a far more logical manner using these two language features.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Remedial c++
  1. C/C++ hw question! (Replies: 3)

  2. C\C++ help needed (Replies: 2)

  3. C++ . (Replies: 19)

  4. Records and C++ (Replies: 3)

Loading...