I am writing a program that will have the user input a temperature in either C or F, then have it converted to the other with the option to have it also be converted to Kelvin. My problem is that when i try to use my kelvin function, it created an infinite loop and I am not sure where I am going wrong. Also, on a side note when i try to enter "0" to exit the program/do-while-loop, it doesn't. Any suggestions would be apprecieated.
#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double FtC(double t);
double CtF(double t);
double TtK(double t);
int main()
{
int ans,ans1;
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;
if (ans == 1)
{
cout << "Please input your desired temperature in F to be converted:" << endl;
cin >> temp;
answer = FtC(temp);
cout << "Would you like to also see the temperature converted to Kelvin? y for yes, anything else for no" << endl;
cin >> ans1;
if (ans1 == y)
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
answer = TtK(temp);
cout << "This is also " << answer<<" Kelvin";
}
else
{
cout << "Please input your desired temperature in C to be converted:" << endl;
cin >> temp;
answer = CtF(temp);
cout << "Would you like to also see the temperature converted to Kelvin? y for yes, anything else for no" << endl;
cin >> ans1;
if (ans1 ==y)
cout << "\nA temperature of " << temp << "in C is " << answer << "in F" << endl;
answer = TtK(temp);
cout << "This is also " << answer << " Kelvin" << 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 TtK(double t)
{
double k;
int ans;
double temp;
if (ans == 1)
k = temp + 273;
else
k = ((5.0 / 9.0)*(t - 32.0)) + 273;
return k;
}
#include "stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
double FtC(double t);
double CtF(double t);
double TtK(double k);
int main()
{
int ans;
double temp, answer;
char y;
char ansk;
cout << "Welcome to my temperature converter." << endl << endl;
do
{
cout << "Would you also like to convert the temperature to Kelvin? y for yes, anything else 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);
if (ansk == y)
{
cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
answer = TtK(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;
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 TtK(double k)
{
int ans;
double temp1;
if (ans == 1)
k = (temp1 + 459.67)*(5.0 / 9.0);
else
k = (temp1)+273.15;
return k;
}