• Register
1 vote
14 views

Problem:

I am trying to make a program that will convert the temperature from Celcius to Fahrenheit and vice-versa.

The sample code is attached below:

#include <iostream>

using namespace std;

void dialouge();
int c2f();
int f2c();

int c2f()
{
    int c;
    int choice;
    cout << "Please enter the degree in Celsius: \n";
    cout << ":.: ";
    cin >> c;
    cout << ":F: " << c * 1.8 + 32 << endl;
    cin.get();

    cout << "Convert another degree?\n";
    cout << "1. Yes.\n";
    cout << "2. No.\n";
    cout << ":.: ";
    cin >> choice;

    if (choice == 1)
    {
        dialouge();
    }

    else if (choice == 2)
    {
        return 0;
    }
}

int f2c()
{
    int f;
    int choice;
    cout << "Please enter the degree in Fahrenheit: \n";
    cout << ":.: ";
    cin >> f;
    cout << ":C: " << f - 32 * 5 / 9 << endl;
    cin.get();

    cout << "Convert another degree?\n";
    cout << "1. Yes.\n";
    cout << "2. No.\n";
    cout << ":.: ";
    cin >> choice;

    if (choice == 1)
    {
        dialouge();
    }

    else if (choice == 2)
    {
        return 0;
    }
}

void dialouge()
{
    int choice;
    cout << "Please choose your desired option:-\n";
    cout << "1. Celsius to Fahrenhite.\n";
    cout << "2. Fahrenhite to Celsius.\n";
    cout << ":.: ";
    cin >> choice;

    if (choice == 1)
    {
        c2f();
    }

    else if (choice == 2)
    {
        f2c();
    }
}

int main()
{
    cout << "This program allows you to convert from Celsius to Fahrenheit and vice versa.\n";
    dialouge();

}

 

But the problem is: When I compile it, CodeBlocks keeps giving me the following warning:
int f2c(): control reaches end of non-void function
int c2f(): control reaches end of non-void function

what does it mean? and if there is anything wrong that I haven't noticed, please point it out.

12 7 7
15,250 points

1 Answer

1 vote

Solution:

The compiler throws back the error because the functions f2c and c2f do not return values on all control paths.

I hope this will help you:

int func(int choice) // This function will return an integer value
{
    if(choice == 1)
    {
        return 1;  // If choice is not 1, then this code is never executed and the function will not return an int
    }
}

int func2(int choice)
{
    if(choice == 1)
    {
        return 1;  // If choice is 1, then 1 is returned
    }
    return 0;  //If choice is not 1, then 0 is returned, so every possible control path returns something
}

Thanks

13 9 6
94,240 points

Related questions

0 votes
1 answer 22 views
22 views
Problem: I have recently started learning C language as currently lockdown is going on so I am learning C language with the help of available online tutorials. I have managed to write the binary search algorithm. If I try to compile my binary search algorithm then I am facing very ... +1, high);     else if(val == sorted[mid])         return mid; } Please guide me in fixing my above C code.
asked Aug 1 Raphael Pacheco 4.9k points
1 vote
1 answer 118 views
118 views
Problem : I am learning C. If I try to compile my below code then I often face an error message as shown below &ldquo;Warning: control may reach end of non-void function [-wreturn-type]&rdquo;. How can I fix my below code to avoid above error message? Below is my code which is ... (board[r][c - 1] == 0)         {             return true;         }     }     else     {         return false;     } }
asked Apr 27 stewart 4k points
0 votes
1 answer 38 views
38 views
I am new to python programming language. I wrote a where I tried to pass a list of functions : def function(f. *args) for f in args print f I can not pass my list of function using this code It gives me error File "main.py", line 1 def function(f. *args) ^ ... : File "main.py", line 1 def function(*args) ^ SyntaxError: invalid syntax How can I solve this and how can I pass a list of function?
asked Mar 12 LizzyM 6.1k points
1 vote
1 answer 18 views
18 views
Problem: I am exploring the c++ programming language nowadays. It&rsquo;s really interesting to learn new things every day. I was trying to adopt the methods in c++ but struggling to return values from a void method. I actually want to know that if it is ... a return statement in a void method. Thanks for your co-operation in this learning process. I really appreciate your effort. Thanks again.
asked Mar 25 Gavin 15.3k points
2 votes
1 answer 69 views
69 views
Problem: Hello guys, I am taking an Udemy online course in C++ recently. I joined a few days back and learning the language step by step. Today, I found a problem in my c++ code that&rsquo;s stopping my code running. My program is giving back an error &ldquo;Invalid use ... main.cpp: In function int main()': main.cpp:13:31: error: invalid use of void expression int n = function2(function1()); ^
asked Apr 19 Gavin 15.3k points