how can i return to main?

Ok, The thing is I dont really know how to make a header file, so I gave it a shot and "tried" to make my own header for this big project am planing to make..=P

This is main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "temperature.h"

using namespace std;

int main()
{
    int aio_choices;
    cout << "Please chose an option." << endl;
    cout << "1. Temperature Converter." << endl;
    cout << "2. COMING SOON." << endl;
    cout << ":.:>> ";
    cin >> aio_choices;

    if (aio_choices == 1)
    {
        tempmenue();
    }
    return 0;


and this is my header file (temperature.h):
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef TEMPERATURE_H_INCLUDED
#define TEMPERATURE_H_INCLUDED

#include<iostream>
#include<windows.h>

using namespace std;

void tempmenue();
void dialogue();
int again();
int f2c();
int c2f();
int exit();

int exit()
{
    return 1;
}

int c2f()
{
    system ("CLS");
    int c;
    cout << "Enter the degree in Celsius.\n";
    cout << ":C:>> ";
    cin >> c;
    cout << ":F:=> " << c * 1.8 + 32 << endl;
    cin.get();
    again();
}

int f2c()
{
    system ("CLS");
    int f;
    cout << "Enter the degree in Fahrenheit.\n";
    cout << ":F:>> ";
    cin >> f;
    cout << ":C:=> " << f - 32 * 5 / 9 << endl;
    cin.get();
    again();
}

int again()
{
    int yn;
    cout << "Would you like to convert another degree?" << endl;
    cout << "1. Yes.\n";
    cout << "2. No.\n";
    cout << "3. Return to root menu.\n";
    cout << ":.:>> ";
    cin >> yn;

    if (yn == 1)
    {
        dialogue();
    }
    else if (yn == 2)
    {
        exit();
    }
    else if (yn == 3)
    {
       return 0;
    }
    else
    {
        cout << "You chose an invalid choice, Program will now terminate!!!";
        return 0;
    }
}

void dialogue()
{
    int choice;
    cout << "Please enter an invalid choice.\n" ;
    cout << "1. Fahrenheit to Celsius.\n" ;
    cout << "2. Celsius to Fahrenheit.\n" ;
    cout << "3. Return to root menu.\n" ;
    cout << "4. Exit.\n" ;
    cout << ":.:>> ";
    cin >> choice;

    if (choice == 1)
    {
        f2c();
    }
    else if (choice == 2)
    {
        c2f();
    }
    else if (choice == 3)
    {
        main();
    }
    else
    {
        exit();
    }
}

void tempmenue()
{
    system ("CLS");
    cout << "This Program allows you to convert between temperatures." << endl;
    dialogue();
}

#endif // TEMPERATURE_H_INCLUDED 


My problem is I cant return to my main.cpp from this part of the code:
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
void dialogue()
{
    int choice;
    cout << "Please enter an invalid choice.\n" ;
    cout << "1. Fahrenheit to Celsius.\n" ;
    cout << "2. Celsius to Fahrenheit.\n" ;
    cout << "3. Return to root menu.\n" ;
    cout << "4. Exit.\n" ;
    cout << ":.:>> ";
    cin >> choice;

    if (choice == 1)
    {
        f2c();
    }
    else if (choice == 2)
    {
        c2f();
    }
    else if (choice == 3)
    {
        main();
    }
    else
    {
        exit();
    }
}


Just as you guys know, am a really nooby guy in C++, so please go easy on me and dont give me harsh comments =P
And any recommendation is greatly appreciated..=)

EDIT: I use Code::Blocks 10.05
Last edited on
Try a different compiler, nothing's wrong with your code.







I think......
Dunno..
I believe my header file has got something wrong with it!!

But am not really sure..=P
You have exit, which returns a value, in a void which. I though void wasn't allowed to return anything. (I'm new too, so it's just a guess).
closed account (zb0S216C)
Why have you included Windows?

This is a pointless method:
1
2
3
4
int exit()
{
    return 1;
}


cf2( ); // Should return an int. Make it void if it doesn't return anything.
f2c( ); // Should return an int. Make it void if it doesn't return anything.
again( ); // Should return an int. Make it void if it doesn't return anything.


Why not integrate a loop instead of calling again( ) over and over?

Eventually, your code will return to main after the branching methods of tempmenue( ) return. Overall, no value is returned from tempmenue( ) because it's void.

1
2
3
4
else if (choice == 3)
{
    main();  // This call will mean that int main will be calling itself.
}


P.S: Your header is fine. Although, it's common to see methods and classes declared in a header and defined in a source file. aio_choices should be initialized before you use it, avoid using namespace std whenever possible; it causes namespace pollution.

No harshness intended.

Edit ---------------------------------8<-------------------------------------
I see methods that return values but the returning values are not processed whatsoever. Like I said, make methods void if they either return nothing or the return value is going to be ignored.

Instead of using multiple if conditions, use a switch condition( Cleaner by far ).
Last edited on
Thanks for your help, but could you explain me the declaring and defining classes and functions, because I dont really understand that part! Give me a small example if you dont mind..=)
closed account (zb0S216C)
This should help: http://www.learncpp.com/cpp-tutorial/19-header-files/
Topic archived. No new replies allowed.