Help with calling functions, looping.

I'm trying to make a program that calls functions in a loop until a certain sentinel value is met. Certain criteria have to be met for this, and it's really stumping me. I know the "getTempType" function is redundant, and really could be lumped into the main loop and it'd be simpler, but it has to be a seperate function due to request.

The output for this is simply running every line in the loop in order, then exitting, so it seems as though my "tempType" variable is always evaluating to true in the loop. What exactly am I doing wrong? I simply want it to call the CtoF function if tempType is "1", FtoC function if "2" and to exit if "3".

Any help at all would be appreciated.

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

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//Prototype Functions
int getTempType(int tempType);
double convertToCelsius(double inputTemp);
double convertToFahrenheit();

int main()
{	
	//Variables
	double endResult = 0.0;
	int tempType = 0;
	double newTemp = 0.0;
	double inputTemp = 0.0;
	int tempTypeInput = 0;

	do {
		getTempType(tempType);
		if (tempType == 1)
		{
			cout << "Enter the temperature you would like to convert to Celsius: ";
			cin >> inputTemp;
			cout << setprecision(1) << fixed << "That would be " << convertToCelsius(inputTemp) << " degrees Celsius.";
			tempType = 0;
			system("pause");
		}
		if (tempType == 2)
		{
			cout << "Enter the temperature you would like to convert to Fahrenheit: ";
			cin >> inputTemp;
			cout << setprecision(1) << fixed << "That would be " << tempType << " degrees Fahrenheit.";
		}
	} while (tempType != 3);
		cout << "Thank you for using the temperature conversion program!";
			system("pause");
			return 0;
} 


int getTempType(int tempType)
{

	cout << "Is the number you are looking to convert in Celsius or Fahrenheit? Enter 1 for Celsius, 2 for Fahrenheit, or 3 to exit: ";
	cin >> tempType;
	return tempType;
}

double convertToCelsius(double inputTemp)
{
	double newTemp = 0.0;

	newTemp = (inputTemp - 32) * 5/9;
	return newTemp;
}


EDIT: I am aware the function for celsius to fahrenheit is missing, I was trying to debug it first then add in the last part, as the current code contains pretty much the base framework for what that function would do as well.

EDIT 2: Ooops, apparently this build is just looping the getTempType function over and over, the previous problem occured with another build.

Also, I tried searching for a similar problem and found no results, and my textbook, youtube, and google-fu seem to be failing me. Probably has something to do with the painkillers from my surgery, haha.
Last edited on
int getTempType(int tempType) copies your input value, rather than modifying the original variable.

Either change it to "getTempType(int& tempType)"
Note the &, which means that "tempType" is a reference to the original, not a copy.
Returning a value is not required in this case.

Or you can assign the returned value.
tempType = getTempTime(); (No real need for an argument in this case, just use a local variable inside the function.)
Last edited on
I'll be sure to try that tonight and see if that works, thank you very much! Anything else?

EDIT: Works like a champ, cleaning it up, then good to go, thanks!
Last edited on
Glad I could help, good luck learning the language!
Topic archived. No new replies allowed.