I have this bit of code that is supposed to first display the following menu and ask the user to enter the choice
Welcome to the Conversion Program
===============================
1. Fahrenheit to Celsius
2. Miles to kilometers
3. Liters to Gallons
4. Exit from the program
I have set up this code to use functions, when the correct number is selected the code will execute the ruction corresponding to the number given to it. like this
int choice;
void displayMenu();
cin >> choice;
if(choice == '1'){
double fahrenheitToCilsuis(double fTemp);
}
else if(choice == '2'){
double milesToKilometers(double miles);
}
else if(choice == '3'){
double litersToGallons(double liters);
}
else if(choice == '4'){
cout << "Exiting..";
}
when I try to run the code I get 3 errors at the end of where each function is defined at the bottom. I don’t understand why any help?
here’s the full code, I’ve put in comments where the errors show up.
#include <iostream>
using namespace std;
void displayMenu();
double fahrenheitToCilsuis();
double milesToKilometers();
double litersToGallons();
int main() {
int choice;
void displayMenu();
cin >> choice;
if(choice == '1'){
double fahrenheitToCilsuis(double fTemp);
}
else if(choice == '2'){
double milesToKilometers(double miles);
}
else if(choice == '3'){
double litersToGallons(double liters);
}
else if(choice == '4'){
cout << "Exiting..";
}
return 0;
}
void displayMenu(){
cout << "Welcome to the Conversion Programn===============================n";
cout << "1. Fahrenheit to Celsiusn2. Miles to kilometersn3. Liters to Gallonsn4. Exit from the programn";
}
double fahrenheitToCilsuis(double fTemp){
int celcius;
cout << "what is your fahrenheit";
cin >> fTemp;
celcius = (fTemp - 32) * 5/9;
cout << "Your celcius is " << celcius;
}//Non-void function does not return a value
double milesToKilometers(double miles){
int kilo;
cout << "what is your miles";
cin >> miles;
kilo = miles + 1.609;
cout << "Your kilometers are " << kilo;
}//Non-void function does not return a value
double litersToGallons(double liters){
int gallons;
cout << "what is your liters";
cin >> liters;
gallons = liters / 3.785;
cout << "Your gallons are " << gallons;
}//Non-void function does not return a value
2
Answers
kilo, gallons in above functions as you are considering floating point
First, you should change return type of your function to void because they doesn’t return anything. Second, I see all of your function don’t need parameter, you should declare
fTemp
,miles
in function’s body. Third, you can’t call function like thisYou must call it like this
Fourth, it isn’t error but I think when you use
if(choice == '1')
you meanif(choice == 1)