#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double FtC(double t);
double CtF(double t);
double showDegreesK(double k);
int main()
{
int ans;
double temp, answer,answer1;
char y;
char ansk;
cout << "Welcome to my temperature converter." << endl << endl;
do
{
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;
cout << "Would you also like to convert the temperature to Kelvin? y for yes, anything else for no." << endl;
cin >> ansk;
if (ans == 1)
{
cout << "Please input your desired temperature in F to be converted:" << endl;
cin >> temp;
answer = FtC(temp);
if (ansk == 'y')
{
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
answer1 = showDegreesK(temp);
cout << "The temperature in Kelvin is " <<answer1<< endl;
}
else
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
}
else
{
cout << "Please input your desired temperature in C to be converted: " << endl;
cin >> temp;
answer1 = CtF(temp);
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
cout << "The temperature in Kelvin is " << answer << 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 k)
{
int ans;
double temp1;
if (ans == 1)
k = (temp1 + 459.67)*(5.0 / 9.0);
else
k = (temp1)+273.15;
return k;
}
You would still need two variables ( ie temperature and an absolute flag) but it might be better to scrap the showDegrees function and incorporate the absolute decision in the two separate temperature conversion functions.
Also, it would be better to only have single cin lines in main for these two instead of 4 lines. There is no need for duplication.
What do you mean by absolute flag? Here is a slightly updated version, but I still can't pass the temp in C or F to my Kelvin function. (I have to use that name/function for a class). Also, my do while loop isn't working right. I can't get out of it when i want to.
#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double FtC(double t);
double CtF(double t);
double showDegreesK(double t);
char ansk;
int main()
{
int ans;
double temp, answer;
char y;
cout << "Welcome to my temperature converter." << endl << endl;
do
{
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;
cout << "Would you like to see the temperature in Kelvin?" << endl;
cin >> ansk;
if (ans == 1)
{
cout << "Please input your desired temperature in F to be converted:" << endl;
cin >> temp;
answer = FtC(temp);
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
if (ansk == 'y')
{
answer = showDegreesK(temp);
cout << "The temperature in Kelvin is " << answer << endl;
}
else
{
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
}
}
else
{
cout << "Please input your desired temperature in C to be converted: " << endl;
cin >> temp;
answer = CtF(temp);
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
if (ansk == 'y')
{
answer = showDegreesK(temp);
cout <<"The temperature in Kelvin is "<<answer<<endl;
}
else
{
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << 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)
{
char y;
char ansk;
double a;
if (ansk == 1)
a = t + 273.15;
else
a = (t+459.67)*(5.0/9.0);
return a;
}
Ok, thank you i fixed it i think. I am having trouble with one more thing though; I am trying to put something in the showDegreesK function that will 'catch' any wrong temperatures since Kelvin can't be negative. How would I go about doing it inside that function, and sending back a message saying something along the lines of "You can't have negative Kelvin" or whatever.
#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double FtC(double t);
double CtF(double t);
double showDegreesK(double t);
char ansk;
int main()
{
int ans;
double temp, answer;
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 >> ansk;
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);
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
if (ansk == 'y')
{
answer = showDegreesK(temp);
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);
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
if (ansk == 'y')
{
answer = showDegreesK(temp);
cout <<"The temperature in Kelvin is "<<answer<<endl;
}
else
{
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
}
}
else
{
break;
}
} 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)
{
char ansk;
double a;
if (ansk == 1)
a = t + 273.15;
else
a = (t+459.67)*(5.0/9.0);
return a;
}
Nevermind, not finished yet. Still not getting how to implement the showDegreesK function correctly. I shall try two variables and return, but is that necessary?
Yep, two variables are necessary whichever way you do it because the program (and user) has to decide which base temperature to use. It doesn't happen magically. :)
The test for negative temperatures could be something like
1 2 3 4 5
if(temp < 0)
cout << "oops!";
// ... some code to start again
else
// code to continue on ...
Ok, yea i am trying to use pass by reference but am getting stuck on how to get my two variables to my main. Do i have to create two new ones for my main, or just one, or just rename something? I am still fiddling with it for now.
#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double FtC(double t);
double CtF(double t);
void showDegreesK(double& t, double& s);
char ansk;
int main()
{
double temp, answer;
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 >> ansk;
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);
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
if (ansk == 'y')
{
showDegreesK(temp);
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);
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
if (ansk == 'y')
{
answer = showDegreesK(temp);
cout << "The temperature in Kelvin is " << answer << endl;
}
else
{
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
}
}
else
{
break;
}
} 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;
}
void showDegreesK(double& t, double& s)
{
char ansk;
double a;
double b;
if (ansk == 1)
a = t + 273.15;
else
b = (t + 459.67)*(5.0 / 9.0);
if (a||b<0)
cout << "That temperature is currently impossible" << endl;
void;
}
The correct terminology is absolute temperature not K (for Kelvin) which just applies to C (for Celsius) temperatures.
So you could have a third variable to show that the starting temp is degrees C or degrees F and then pass it along to showDegreesK (double t, double& s, char scale) where scale is 'C' or 'F'.
But you can save all the trouble by FtC and CtF having two variables (temperature and flag) and do the calculation there.
1 2 3 4 5 6 7 8 9
double FtC(double t, double ansk)
{
double a;
a = (5.0 / 9.0)*(t - 32.0);
if (ansk == 1)
a = a + 273.15; // or a += 273.15
return a;
}
BTW your line 115 won't work. should be if (a < 0 || b < 0) etc
I tried using 3 variables in my showDegreesK function because I think we are supposed to only have one variable in our FtC and CtF functions. I am getting odd numbers with e to a huge power.
#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double FtC(double t);
double CtF(double t);
void showDegreesK(double& temp, double& temp1, double& temp2);
char ansk;
int main()
{
double temp, answer,temp1,temp2;
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 >> ansk;
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);
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
if (ansk == 'y')
{
showDegreesK(temp,temp1,temp2);
cout << "The temperature in Kelvin is " << temp1 << 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);
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
if (ansk == 'y')
{
showDegreesK(temp,temp1,temp2);
cout << "The temperature in Kelvin is " << temp2 << endl;
}
else
{
cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
}
}
else
{
break;
}
} 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;
}
void showDegreesK(double& t, double& a, double& b)
{
char ansk;
if (ansk == 1)
a = t + 273.15;
else
b = (t + 459.67)*(5.0 / 9.0);
if (a<0||b<0)
cout << "That temperature is currently impossible" << endl;
}
Yea, that is what i had originally but i think I am supposed to calculate Kelvin from C and F in one function named ShowDegreesK. That is what I am having a hard time with really. The other problems are minor that I am slowly learning right now. How would I pass one argument to ShowDegreesK, and pass 2 back?
It is customary to have one thread for one problem Jon, not two as you've done here and in the other thread you started. There is a lot of duplication going on. Good luck with your quest. :)