Is return Main() a good idea?

Hello, I'm trying to loop back through the menu instead of it exiting. I did came to the conclusion of return back to main(). Never tried it before but it worked. Is there another way to do this?
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  #include <iostream>
#include <iterator>
using namespace std;
class Listthenodes
{
private:
    typedef struct nodes
    {
        int Numbers;
        nodes* next;

    }*Nodeptr;

Nodeptr head;
Nodeptr curr;
Nodeptr temp;

public:
    Listthenodes();
void Addnode(int addData);
void DeleteNode(int delnode);
void DisplayData ();

};
Listthenodes::Listthenodes()
{
    head = NULL;
    temp = NULL;
    curr = NULL;
}
void Listthenodes::Addnode(int addData)
{
    Nodeptr n = new nodes;
    n->next = NULL;
    n->Numbers = addData;

    if (head != NULL)
    {
        curr = head;
        while (curr->next != NULL){
            curr = curr->next;
        }
        curr->next = n;
    }

    else
        {

    head = n;
        }
}
void Listthenodes::DeleteNode(int delnode)
{
    Nodeptr d = NULL;
    temp = head;
    curr = head;
    while (curr != NULL && curr -> Numbers != delnode){
        temp = curr;
        curr = curr->next;
    }
    if(curr == NULL){

        cout << delnode << "Was not in the list" << endl;
    }
    else
    {
        d = curr;
        curr = curr->next;
        temp -> next = curr;
        delete d;
        cout << "The value" << delnode << " Was deleted" << endl;
    }
}
void Listthenodes::DisplayData()
{
    curr = head;
    while (curr != NULL){
        cout << curr->Numbers << endl;
        curr = curr->next;

    }

}



int main()
{
Listthenodes Justin;
int Bo;


int x;
    cout << "1 . Add to node" << endl;
    cout << "2 . Delete a node" << endl;
     cout << "3 . Display nodes" << endl;
      cout << "4 . Exit program" << endl;
  cin >> x;
    switch (x)
    {


    case 1:{
         cout << "Add an node" << endl;
        cin >> Bo;

        Justin.Addnode(Bo);

return main();

break;
    }


    case 2:
        {
break;
        }


    case 3:
        {
break;
        }


    case 4:
        {

break;
        }


    }
}
No, it is never okay to call main(). main is a special function that is only the entry point to the program. Use a while loop inside main instead.


rough example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    Listthenodes Justin;
    int Bo;
    while (true) {
        int x;
        cout << "1 . Add to node" << endl;
        cout << "2 . Delete a node" << endl;
        cout << "3 . Display nodes" << endl;
        cout << "4 . Exit program" << endl;
        cin >> x;
        switch (x)
        {
          case 1:
            cout << "Add an node" << endl;
            cin >> Bo;
            Justin.Addnode(Bo);
            continue;  // goes to top of while loop again

        }
    }
}
Last edited on
Is return Main() a good idea?

No - it's a bad idea.

The C++ Standard actually says that you may not call main() from your own code. But as C allows it is seems that compilers let you get away with it.

Is there another way to do this?

Yes.

Here you should use a while loop.

(But if you do come across an actual need for a recursive main, move the contents out of main into your own function and then call it recursively.)

Andy
Last edited on
@andywestken @Ganado

Thanks.
Topic archived. No new replies allowed.