What's wrong with my code?

its a simple Celsius to Fahrenhiet (and vice versa) converter. 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 havent noticed, please point it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#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();

}


Thanks =)
closed account (3hM2Nwbp)
That's because the functions f2c and c2f do not return values on all control paths.
What does it mean exactly, I don't have a good grasp on the language yet!
closed account (3hM2Nwbp)
1
2
3
4
5
6
7
8
9
    if (choice == 1)
    {
        dialouge();
    }

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


If choice is 1, then an integer is not returned. To resolve this, you can either change your methods' returns to void or do something similar to this in order to make the warning go away.

1
2
3
4
5
6
7
    /// I think this is what you want to do.
    if (choice == 1) /// If the user enters 1, then make another calculation
    {
        dialouge();
    }
    /// If not, return
    return 0;


-Edit- You seem to be using recursion in this program, if you're unfamiliar with the term, Wikipedia has a nice article on it. http://en.wikipedia.org/wiki/Recursion_%28computer_science%29
Last edited on
Didn't really get it, could you explain to me in other words?
closed account (3hM2Nwbp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

// Warning: Not all control paths return a value
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
    }
}

/// No warnings - all control paths return a value
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
}


Does that help?
Ohh, ok...this is a whole better explanation!
Got it man, Thanks =)
Topic archived. No new replies allowed.