How Do You Handle Incorrect Temperature Scale Inputs in C++?

  • C/C++
  • Thread starter frogdogbb
  • Start date
  • Tags
    Temperature
In summary: It is not necessary to have the cout statement written three times, but it does make the output clearer for the user. As for the second question, if the user does not enter C, F, or K, the program will not run properly and may give an error message. It is important to include error handling in the code to account for unexpected user input. And yes, the main function should return an int according to the ANSI standard.
  • #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
 
Technology news on Phys.org
  • #2
maybe this is closer

// 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
Where are you inputting the values of F, C and K in main?
 
  • #4
Presumably because he wants the function to just do the calculation.

He posted this also in "homework help" where I suggested that he pass the F,K,C variables by "reference" rather than by "value".
 
  • #5
exactly what is the function suppose to do...
waht is the return "double"variable? and the compiler error you get should be easily solved...look at your function definition and look at your function call

as for your function itself, it will either enter a infinite loop because you haven't altered the scale value or while (scale != 0){...} or you won't use the function itself because scale==0.
 
  • #6
HallsofIvy said:
...I suggested that he pass the F,K,C variables by "reference" rather than by "value".

this is the answer your looking fordouble ftemp (double F, double C, double K, double temp, double scale)
you don't return a double;
your function prototype may be something like:
void ftemp (double* F, double* C, double* K, double temp, double scale)

and pass the variables like:
ftemp(&F, &C, &K, temp, scale)but you will still need to output to screen the values.
 
Last edited:
  • #7
Even if you pass the variables by reference how do you expect to do this type of comparison without having an intial value:

if (scale==C)
 
  • #8
dduardo said:
Even if you pass the variables by reference how do you expect to do this type of comparison without having an intial value:

if (scale==C)

i would have rewritten the code and give the correct answer but this is "homework help" and therefore i don't want to give the full answer.
 
  • #9
Well...

I had it figured out with a more consise program but I accidently earsed it. So I had to redo it it works and should be more than enough to get me a 100% even though it is not as pretty as I would like.

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

double kscale (double);
double fscale (double);
double cscale (double);
double kscale1 (double);
double fscale1 (double);
double cscale1 (double);

int main()
{
double k, f, c, temp;
char scale;
cout<<"Enter temperature: ";
cin>>temp;
cout<<"\nEnter scale C, F, or K: ";
cin>>scale;

if (scale=='k' || scale=='K')
{
k= temp;
f=kscale (temp);
c= kscale1 (temp);
cout<<"\n K= "<<k<<"\n C= "<<c<<"\n F= "<<f<<endl;
}

else if (scale=='f' || scale=='F')
{
f= temp;
k= fscale (temp);
c= fscale1 (temp);
cout<<"\n K= "<<k<<"\n C= "<<c<<"\n F= "<<f<<endl;
}
else if (scale=='c' || scale=='C')
{
c= temp;
k= cscale(temp);
f= cscale1(temp);
cout<<"\n K= "<<k<<"\n C= "<<c<<"\n F= "<<f<<endl;
}
}

double kscale (double temp)
{
double f;
f=((temp-273.15)*(9/5))+32;
return f;
}
double kscale1 (double temp)
{
double c;
c=temp-273.15;
return c;
}

double fscale (double temp)
{
double k;
k=((temp-32)*(5/9))+273.15;
return k;
}

double fscale1 (double temp)
{
double c;
c=(temp-32)*(5/9);
return c;
}

double cscale (double temp)
{
double k;
k=temp+273.15;
return k;
}
double cscale1 (double temp)
{
double f;
f=(temp*9/5)+32;
return f;
}
Thanks for the suggestions.
 
  • #10
this code looks ugly yet it may do as you wish.
does the code need to have
Code:
cout<<"\n K= "<<k<<"\n C= "<<c<<"\n F= "<<f<<endl;
written three times?

question:
Code:
cout<<"\nEnter scale C, F, or K: ";
cin>>scale;
what happens if a user doesn't enter C,F or K?

plus your main doesn't return a int which is required by ansii
 
Last edited:

FAQ: How Do You Handle Incorrect Temperature Scale Inputs in C++?

What is temperature conversion and why is it important?

Temperature conversion is the process of converting a temperature value from one unit of measurement to another. This is important because different countries and fields of science use different units of measurement for temperature, and it is necessary to convert between them in order to communicate and understand temperature values accurately.

How do I convert temperature with C++?

To convert temperature with C++, you can use the built-in functions in the library. For example, the cin function can be used to read in a temperature value, and the cout function can be used to display the converted temperature in the desired unit of measurement.

What are the most commonly used temperature units in C++?

The most commonly used temperature units in C++ are Celsius, Fahrenheit, and Kelvin. C++ also has built-in functions for converting between these units, such as celsiusToFahrenheit() and kelvinToCelsius().

How accurate are temperature conversions in C++?

The accuracy of temperature conversions in C++ depends on the precision of the data type used. For example, using the double data type will result in more accurate conversions compared to using the int data type. However, it is important to note that all conversions involve some degree of rounding and may not be 100% accurate.

Can I customize the conversion formula in C++?

Yes, you can customize the conversion formula in C++ by creating your own functions and using them in your code. This allows you to convert temperature values using any formula or unit of measurement that you prefer. However, it is important to ensure that your formula is accurate and tested before using it in your code.

Similar threads

Replies
5
Views
2K
Replies
6
Views
10K
Replies
17
Views
2K
Replies
3
Views
10K
Replies
2
Views
3K
Replies
1
Views
1K
Replies
1
Views
3K
Back
Top