skip to Main Content

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


    1. In funtions declaration you mentioned function return type as double so at the end of function you have to return something as data type double or you need to make return type as a void.
    2. Also you need to use double as a data type for variables celcius,
      kilo, gallons in above functions as you are considering floating point
    3. Instead of calling your function inside main you are again declaring it
    4. Your functions declaration and definations both mismatched
    5. You are storing input choice into int data type and in if/else if condition you are using character comparision
    #include <iostream>
    using namespace std;
    void displayMenu();
    void fahrenheitToCilsuis();
    void milesToKilometers();
    void litersToGallons();
    
    int main() {
        int choice = 0;
        double data = 0;
        double result = 0;
        displayMenu();
        cin >> choice;
        if(choice == 1){
            fahrenheitToCilsuis();
        }
        else if(choice == 2){
            milesToKilometers();
        }
        else if(choice == 3){
            litersToGallons();
        }
        else if(choice == 4){
            cout << "Exiting..";
        }
        else
        {
            cout << "Wrong Input..";
        }
    
        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";
    }
    
    void fahrenheitToCilsuis(){
        double celcius;
        double fTemp;
        cout << "what is your fahrenheit:";
        cin >> fTemp;
        celcius = (fTemp - 32) * 5/9;
        cout << "Your celcius is: " << celcius;
    }
    
    void milesToKilometers(){
        double kilo;
        double miles;
        cout << "what is your miles:";
        cin >> miles;
        kilo = miles + 1.609;
        cout << "Your kilometers are: " << kilo;
    }
    void litersToGallons(){
        double gallons;
        double liters;
        cout << "what is your liters:";
        cin >> liters;
        gallons = liters / 3.785;
        cout << "Your gallons are: " << gallons;
    }
    
    Login or Signup to reply.
  1. 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 this

    if(choice == '1'){
        double fahrenheitToCilsuis(double fTemp);
    }
    

    You must call it like this

    if(choice == '1'){
        fahrenheitToCilsuis(double fTemp);
    }
    

    Fourth, it isn’t error but I think when you use if(choice == '1') you mean if(choice == 1)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search