functions

hi guys in my class we are getting to other functions besides int main. how can i create an 1) input function and 2) conversion function of this code that i wrote. i tried to do something my self but it doesnt work. much thanks

//Daniel
//CMPCS 101
//Project 9
//November 2 2012
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

double getFar (double);//prototype for input F

// start of main
int main(void) {


cout << "Program by Daniel" << endl;
cout << "This program will conver temperature from Fahrenheit to Celsius" << endl;

double getFar (double Fare);



// intro text line
cout << endl; //blank
double F = -99.99; // Fahrenheit
double C = -99.99;//Celsius
char stop = 'q'; // I chose halt because it sounds better than stop

do {

F = getFar;

cout << "Fahrenheit: " << endl;


if ( F < 459.67 && F > -459.69 ) {
double FtoC = (( F - 32.0 ) * 5.0) / 9.0; //convert Fahrenheit to Celsius

cout << F << " Degrees Fahrenheit equals to: " << fixed << setprecision(2) << FtoC << " Degrees Celsius" << endl; // prints results
cout << endl; //blank
}
else {
cout << "Input is out of range!" << endl;
}
cout << "Would you like to do another calculation?" << endl;
cout << endl;
cout << "If no press Q and enter to quit. If yes then press any other key, then enter." ;
cin >> stop;
cout << endl;


} while(tolower(stop) != 'q');

return 0;
} // end of main

double getFar (double Fare) {

double fHeit = -99.99;

do {

cout << "Enter degrees in Fahrenheit: " << Fare; //prompt to enter Fahernheit
cin >> fHeit; //input Fahrenheit

cout << endl;

} while(fHeit < 459.67 && fHeit > -459.69);



return fHeit;


}
In user define functions there must be a returning value if return data-type is not void
In your program, the function
double getFar( )
declered for two times, this is invalid.
also in your program there is no need of parameter in getFar() function

here's the correct code:

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

double getFar ();//prototype for input F

// start of main
int main(void) {


cout << "Program by Daniel" << endl;
cout << "This program will conver temperature from Fahrenheit to Celsius" << endl;


// intro text line
cout << endl; //blank
double F = -99.99; // Fahrenheit
double C = -99.99;//Celsius
char stop = 'q'; // I chose halt because it sounds better than stop

do {

F = getFar();

cout << "Fahrenheit: " << endl;


if ( F < 459.67 && F > -459.69 ) {
double FtoC = (( F - 32.0 ) * 5.0) / 9.0; //convert Fahrenheit to Celsius

cout << F << " Degrees Fahrenheit equals to: " << fixed << setprecision(2) << FtoC << " Degrees Celsius" << endl; // prints results
cout << endl; //blank
}
else {
cout << "Input is out of range!" << endl;
}
cout << "Would you like to do another calculation?" << endl;
cout << endl;
cout << "If no press Q and enter to quit. If yes then press any other key, then enter." ;
cin >> stop;
cout << endl;


} while(tolower(stop) != 'q');

return 0;
} // end of main

double getFar () {

double fHeit = -99.99;

do {

cout << "Enter degrees in Fahrenheit: "; //prompt to enter Fahernheit
cin >> fHeit; //input Fahrenheit

cout << endl;

} while(fHeit > 459.67 && fHeit < -459.69);/*if you want to take in between -459 to 459 then condition in while must be false for correct value*/



return fHeit;


}

one more thing condition of taking input in between -459 to 459 is not correct see comment in above code; written with while in getFar() function
Last edited on
Topic archived. No new replies allowed.