I'm having trouble accessing variables throughout multiple functions. For example, I need to use userinput throughout many other functions, but do not have access to it with my current code. I am pretty sure that I need to change the parameters of my functions, so that I can pass the value of those functions by reference, but I'm having trouble doing that.
Here's what my code looks like right now:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double fahrtocels();
double celstofahr();
double celstokelv();
double kelvtocels();
int main()
{
do { int userinput=0;
cout << " 1) Fahr to Cels" << endl;
cout << " 2) Cels to Fahr" << endl;
cout << " 3) Cels to Kelv" << endl;
cout << " 4) Kelv to Cels" << endl;
cin >> userinput;
if(userinput=1)
fahrtocels();
if(userinput=2)
celstofahr();
if(userinput=3)
celstokelv();
if(userinput=4)
kelvtocels(); } while(userinput=0);
}
double fahrtocels()
{
int input;
int tempinput;
double finaloutput;
cout <<" Please Enter the Temp in Fahrenheit"<<endl;
cin >> input;
userinput= 1;
tempinput = input - 32;
finaloutput = tempinput/1.8;
cout<<input<< " degrees fahrenheit is equivalent to " << finaloutput<< " celsius"<< endl;
userinput = 0;
}
double celstofahr()
{
int inputcels;
int tempinputcels;
double finaloutputcels;
cout <<" Please Enter the Temp in Celsius"<<endl;
cin >> inputcels;
tempinputcels = inputcels * 1.8;
finaloutputcels = tempinputcels + 32;
cout<<inputcels<< " degrees celsius is equivalent to " << finaloutputcels<< " fahrenheit"<< endl;
}
double celstokelv()
{
int inputcelskelv;
double finaloutputcelskelv;
cout <<" Please Enter the Temp in Celsius for Kelvin Conversion"<<endl;
cin >> inputcelskelv;
finaloutputcelskelv = inputcelskelv + 273.15;
cout<<inputcelskelv<< " degrees celsius is equivalent to " << finaloutputcelskelv<< " kelvin"<< endl;
}
double kelvtocels()
{
int inputkelvcels;
double finaloutputkelvcels;
cout <<" Please Enter the Temp in Kelvin for Celsius conversion"<<endl;
cin >> inputkelvcels;
finaloutputkelvcels = inputkelvcels - 273.15;
cout<<inputkelvcels<< " degrees kelvin is equivalent to " << finaloutputkelvcels<< " celsius"<< endl;
}