Converting Temperature Units with C++

In summary: I know that the course you are taking is probably not covering these things- but you probably should be looking into them if you are having trouble writing code as you have done here. I have written a couple of programs that are similar to yours- but this is the first one I have written using an enumerated type and switch.It's worth noting that if you do the same task using enumerated types and switch, you don't need any of those "if" statements- "if (scale==C);" is meaningless. It has no effect on the program at all. What you want is "if (scale==C) { ... do something ... }" (The semicolon I told you about is wrong. It's
  • #1
frogdogbb
45
0
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
 
Physics news on Phys.org
  • #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
...
 
  • #3
Code:
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
[URL]http://library.thinkquest.org/20991/gather/formula/data/129.html[/URL]
*/
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 by a moderator:
  • #4
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".
 
  • #5
Code:
double ftemp (double F, double C, double K, double temp, double scale)

I only get one complier error:ftemp does not require 3 arguments

Dont you have to put a semicolon (;) at the end of your statement?
 
  • #6
I didn't notice this until after my remarks about "passing by value" and "passing by reference" (which still stand):
frogdogbb said:
I only get one complier error:ftemp does not require 3 arguments
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"?
 
  • #7
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.
 

1. How do I convert from Celsius to Fahrenheit in C++?

To convert from Celsius to Fahrenheit, you can use the formula: F = (C * 9/5) + 32, where F is the temperature in Fahrenheit and C is the temperature in Celsius. You can also use the built-in function std::round to round the result to the nearest whole number.

2. What is the formula for converting from Fahrenheit to Celsius in C++?

The formula for converting from Fahrenheit to Celsius is: C = (F - 32) * 5/9, where C is the temperature in Celsius and F is the temperature in Fahrenheit. Again, you can use the std::round function to round the result.

3. How can I convert from Kelvin to Celsius in C++?

To convert from Kelvin to Celsius, you can use the formula: C = K - 273.15, where C is the temperature in Celsius and K is the temperature in Kelvin. This formula is based on the fact that 0 Kelvin is equal to -273.15 degrees Celsius.

4. Can I convert from Celsius to Kelvin in C++?

Yes, you can convert from Celsius to Kelvin by adding 273.15 to the temperature in Celsius. The formula is: K = C + 273.15, where K is the temperature in Kelvin and C is the temperature in Celsius.

5. Is there a built-in function for temperature conversion in C++?

No, there is no built-in function for temperature conversion in C++. However, you can easily create your own functions or use the formulas mentioned above to convert between different temperature units.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
752
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
878
Back
Top