Temperature conversion, infinite loop help

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  #include "stdafx.h"
#include<iostream>

using namespace std;
using namespace 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;
}
You ask the user to enter 1 or 2. Your while loop's condition is, keep going if answer not 0.
What do you mean? Here is an updated version, not sure if it is better though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace 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;
}
Topic archived. No new replies allowed.