Hi there, I am writing a program that will convert F to C or C to F with the option of converting either to K. I am having trouble passing the F or C to my function for Kelvin; I am getting a 0 every time. I feel I am doing something wrong with my variables, but I am not sure. Could anyone push me in the right direction?
#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double answer;
double FtC(double t);
double CtF(double t);
double showDegreesK (double t);
int main()
{
double answer = 0;
char answerkelvin;
int ans;
double temp;
cout << "Welcome to my temperature converter." << endl << endl;
do
{
cout << "Would you like to see the temperature in Kelvin? y for yes, n for no" << endl;
cin >> answerkelvin;
cout << "Which conversion type would you like to do:" << endl;
cout << "1. F to C \n2. C to F \n0. End Program" << endl;
cin >> ans;
if (ans == 1)
{
cout << "Please input your desired temperature in F to be converted:" << endl;
cin >> temp;
answer = FtC(temp);
if (answerkelvin == 'y')
{
showDegreesK(answer);
if (answer<0)
{
cout << "That temperature is impossible" << endl;
}
else
{
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
cout << "The temperature in Kelvin is " << answer << endl;
}
}
else
{
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
}
}
elseif (ans == 2)
{
cout << "Please input your desired temperature in C to be converted: " << endl;
cin >> temp;
answer = CtF(temp);
if (answerkelvin == 'y')
{
showDegreesK(answer);
if (answer < 0)
{
cout << "That temperature is impossible"<<endl;
}
else
{
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
cout << "The temperature in Kelvin is " << answer << endl;
}
}
else
{
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
}
}
else
{
cout << "You entered an invalid key." << endl;
}
} while (ans != 0);
cout << "Thank you for using my temperature converting program!" << endl;
system("Pause");
return 0;
}
double FtC(double t)
{
double a;
a = (5.0 / 9.0)*(t - 32.0);
return a;
}
double CtF(double t)
{
double a;
a = ((9.0 / 5.0*t)) + 32;
return a;
}
double showDegreesK(double t)
{
double f, c;
f = answer + 273.15;
c = (answer + 459.67)*(5.0 / 9.0);
return t;
}
Remove the global on line 9. I think this is complicating the flow of your program and making it more difficult for you to reason about it.
Don't use any globals at all and give it another try, then come back if you're still having problems.
I know globals are considered 'bad'; I used them because I was having trouble getting it to compile otherwise. I guess my question is, how could I get around using globals? And my original question is how can I get my showDegreesK function to know whether it is receiving a degree in C or in F?
oh ok. I am trying to have it to it only sends a temp in C to the function, where am I going wrong with my variables? I think I am calling my functions correctly.
#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double FtC(double t);
double CtF(double t);
double showDegreesK (double t);
int main()
{
double answer = 0, temp;
char answerkelvin;
int ans;
cout << "Welcome to my temperature converter." << endl << endl;
do
{
cout << "Would you like to see the temperature in Kelvin? y for yes, n for no" << endl;
cin >> answerkelvin;
cout << "Which conversion type would you like to do:" << endl;
cout << "1. F to C \n2. C to F \n0. End Program" << endl;
cin >> ans;
if (ans == 1)
{
cout << "Please input your desired temperature in F to be converted:" << endl;
cin >> temp;
answer = FtC(temp);
if (answerkelvin == 'y')
{
showDegreesK(temp);
if (temp<0)
{
cout << "That temperature is impossible" << endl;
}
else
{
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
cout << "The temperature in Kelvin is " << answer << endl;
}
}
else
{
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
}
}
elseif (ans == 2)
{
cout << "Please input your desired temperature in C to be converted: " << endl;
cin >> temp;
answer = CtF(temp);
if (answerkelvin == 'y')
{
showDegreesK(temp);
if (answer < 0)
{
cout << "That temperature is impossible"<<endl;
}
else
{
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
answer = FtC(temp);
showDegreesK(temp);
cout << "The temperature in Kelvin is " << answer << endl;
}
}
else
{
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
}
}
else
{
cout << "You entered an invalid key." << endl;
}
} while (ans != 0);
cout << "Thank you for using my temperature converting program!" << endl;
system("Pause");
return 0;
}
double FtC(double t)
{
double a;
a = (5.0 / 9.0)*(t - 32.0);
return a;
}
double CtF(double t)
{
double a;
a = ((9.0 / 5.0*t)) + 32;
return a;
}
double showDegreesK(double t)
{
double answer = answer + 273.15;
return answer;
}
Line 140: answer doesn't have any particular value before you assign it. You need to add absolute zero to the variable that actually contains the temperature value.
Lines 51, 87, 97, and 98: Your functions are pure now, so if you don't assign their return value to anything, calling them doesn't do anything.
1 2 3 4 5 6 7
c = 20;
answer = CtF(c);
//answer = 68; c still equals 20
answer = FtC(c);
//answer = -6.666...; c still equals 20
showDegreesK(c);
//no change in program state
Also, rename showDegreesK(). It's a bad name because it doesn't actually show anything.
I am supposed to have the function named that. Alright, I fixed all that. Now, my error catch for having a negative kelvin isn't working. Should it not be answer<0? Here is my updated code:
#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double FtC(double t);
double CtF(double t);
double showDegreesK (double t);
int main()
{
double answer = 0, temp;
char answerkelvin;
int ans;
cout << "Welcome to my temperature converter." << endl << endl;
do
{
cout << "Would you like to see the temperature in Kelvin? y for yes, any other key for no" << endl;
cin >> answerkelvin;
cout << "Which conversion type would you like to do:" << endl;
cout << "1. F to C \n2. C to F \n0. End Program" << endl;
cin >> ans;
if (ans == 1)
{
cout << "Please input your desired temperature in F to be converted:" << endl;
cin >> temp;
answer = FtC(temp);
if (answerkelvin == 'y')
{
answer = showDegreesK(temp);
if (answer<0)
{
cout << "That temperature is impossible" << endl;
}
else
{
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
cout << "The temperature in Kelvin is " << answer << endl;
}
}
else
{
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
}
}
elseif (ans == 2)
{
cout << "Please input your desired temperature in C to be converted: " << endl;
cin >> temp;
answer = CtF(temp);
if (answerkelvin == 'y')
{
answer = showDegreesK(temp);
if (answer < 0)
{
cout << "That temperature is impossible"<<endl;
}
else
{
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
answer = FtC(temp);
answer = showDegreesK(temp);
cout << "The temperature in Kelvin is " << answer << endl;
}
}
else
{
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
}
}
elseif (ans == 0)
{
cout << "Thank you for using my temperature converting program!" << endl;
}
else
cout << "You entered an invalid key" << endl;
} while (ans != 0);
system("Pause");
return 0;
}
double FtC(double t)
{
double a;
a = (5.0 / 9.0)*(t - 32.0);
return a;
}
double CtF(double t)
{
double a;
a = ((9.0 / 5.0*t)) + 32;
return a;
}
double showDegreesK(double t)
{
double answer = answer + 273.15;
return answer;
}
ok, so i think i am ALMOST there. How would you go about putting in the error catch for negative kelvin, without outputting anything else but an error message? I am having trouble with the order of calling functions/cout to the screen.
#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double FtC(double t);
double CtF(double t);
double showDegreesK (double t);
int main()
{
double answer = 0, temp,answer1;
char answerkelvin;
int ans;
cout << "Welcome to my temperature converter." << endl << endl;
do
{
cout << "Would you like to see the temperature in Kelvin? y for yes, any other key for no" << endl;
cin >> answerkelvin;
cout << "Which conversion type would you like to do:" << endl;
cout << "1. F to C \n2. C to F \n0. End Program" << endl;
cin >> ans;
if (ans == 1)
{
cout << "Please input your desired temperature in F to be converted:" << endl;
cin >> temp;
if (answerkelvin = 'y')
{
answer = FtC(temp);
cout << "The temperature " << temp << " in F is " << answer << endl;
answer = showDegreesK(answer);
cout << "The temperature in Kelvin is " << answer<<endl;
}
else
{
cout << "The temperature " << temp << "in F is " << answer << endl;
}
}
elseif (ans == 2)
{
cout << "Please input your desired temperature in C to be converted: " << endl;
cin >> temp;
if (answerkelvin = 'y')
{
answer = CtF(temp);
cout << "The temperature " << temp << " in C is " << answer <<"in F"<<endl;
answer = FtC(temp);
answer = showDegreesK(answer);
cout << "The temperature in Kelvin is " << answer<<endl;
}
else
{
cout << "The temperature " << temp << "in C is " << answer <<"in F"<<endl;
}
}
elseif (ans == 0)
{
cout << "Thank you for using my temperature converting program!" << endl;
}
else
cout << "You entered an invalid key" << endl;
} while (ans != 0);
system("Pause");
return 0;
}
double FtC(double t)
{
double a;
a = (5.0 / 9.0)*(t - 32.0);
return a;
}
double CtF(double t)
{
double a;
a = ((9.0 / 5.0*t)) + 32;
return a;
}
double showDegreesK(double t)
{
double a = t + 273.15;
if (a < 0)
cout << "That temperature is impossible" << endl;
return a;
}