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.