Converting Temperature Units with C++

  • Context: Comp Sci 
  • Thread starter Thread starter frogdogbb
  • Start date Start date
  • Tags Tags
    C++ Temperature Units
Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program for converting temperatures between Celsius, Fahrenheit, and Kelvin. Participants are sharing code snippets, troubleshooting issues, and suggesting improvements related to function design and user input handling.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses frustration with the C++ program and seeks help with temperature conversion functions.
  • Another participant provides a revised version of the initial code, correcting syntax errors and suggesting changes to the function parameters.
  • A third participant shares a different example of a temperature conversion program, written in C, as a reference.
  • Some participants point out issues with the original program's input expectations and the need for proper return values from functions.
  • There are discussions about the importance of passing parameters by reference versus by value in C++ functions.
  • One participant suggests using enumerated types and switch statements for a more logical program structure.

Areas of Agreement / Disagreement

Participants express various opinions on the correctness of the original code and suggest different approaches to solving the problem. There is no consensus on the best method to implement the temperature conversion program, and multiple viewpoints remain on how to handle function parameters and user input.

Contextual Notes

Some participants note that the original program does not return values correctly and that the input prompts may lead to confusion regarding expected data types. There are also unresolved issues regarding the handling of function parameters and the overall program structure.

Who May Find This Useful

Individuals learning C++ programming, particularly those interested in function design and user input handling in console applications, may find this discussion beneficial.

frogdogbb
Messages
45
Reaction score
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;
count<<"Enter the scale you want to convert from C, F, or K- 0 to quit: ";
cin >> scale;

count<<"\nEnter temperature";
cin >> temp;
count<<"\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
// 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;
count<<"Enter the scale you want to convert from C, F, or K- 0 to quit: ";
cin >> scale;

count<<"\nEnter temperature";
cin >> temp;
count<<"\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
...
 
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:
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 'count<<"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".
 
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?
 
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"?
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K