I had to.. edit.. this. Recursive calls to main() are bad, as were a few other things.
Code:
#include <iostream>
int menu();
float far(float x);
float cel(float x);
bool quitflag = 0;
int main()
{
while (quitflag != 1)
{
menu();
}
return(0);
}
int menu()
{
int ans;
char again;
std::cout << "1. Celsius to Fahrenheit";
std::cout << "\n2. Fahrenheit to Celsius";
std::cout << "\n\nSelection: ";
std::cin >> ans;
if (ans == 1)
{
int temp = 0;
std::cout << "\nTemperature: ";
std::cin >> temp;
std::cout << "\nFinal temp is: " << cel(temp);
}
else if (ans == 2)
{
int temp = 0;
std::cout << "\nTemperature: ";
std::cin >> temp;
std::cout << "\nFinal temp is: " << far(temp);
}
else
{
std::cout << "\nInvalid input, please try again.\n\n";
return(0);
}
std::cout << "\n\nCalculate another? ";
std::cin >> again;
std::cout << std::endl;
tolower(again);
if (again == 'n') // Only need to test an 'n' condition, the rest will auto-repeat. Switch this to 'y' and take out the 'quitflag' line if you want it to exit on anything except 'y'.
{
quitflag = 1;
return(0);
}
return(0);
}
float far(float x)
{
return((x-32)*(5.0/9.0));
}
float cel(float x)
{
return((x*1.8)+32);
}