Running integer when it is after the code

So my question is this: I want to run the main(); again, but the problem stays that whenever I try to run it again, the code MUST be after the main();.

I don't want to show the whole code, since it is kind of TOO long, so I'm practically showing a new code, which explains what I'm saying.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int test()
{
     cout << "The user came here, but, if I want to get the user back to the main, I can't do so";
     main();
     return 0;
}

int main()
{
     cout << "Alright, so this is the main.";
     cout << "Now I want to get the user to another function";
     test();
     return 0;


So, that's my point. You can't get the user back to the main if it is after the function the user currently is. That's opening me a lot of problems and blocking me do some different things.

Thank you for any answer.
Last edited on
what are you actually trying to achieve?

main() (normally) is both the start and end point of a program.
After the test function completes with the return statement, the program goes back to where it was called in main. Main is a special function and it shouldn't be called explicitly.

This works....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int test()
{
     std::cout << "\nThe user came to test function.";
     return 0;
}

int main()
{
     std::cout << "Alright, so this is the main.";
     std::cout << "\nNow I want to get the user to another function";
     test();
     std::cout << "\nNow I've returned to main.";
     return 0;
Alright, so this is the main.
Now I want to get the user to another function
The user came to test function.
Now I've returned to main.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int test(int b)
{
     std::cout << "\nThe user came to test function.";
     return b+1;
}

int main()
{
  int a = 0;
  while ( a < 10 ) {
     std::cout << "Alright, so this is the main.";
     std::cout << "\nNow I want to get the user to another function";
     a = test( a );
     std::cout << "\nNow I've returned to main.";
  }
  return 0;
}
That's the way I want to return it. I'll show part of my 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
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
int again()
{
    c > 0 && c < 6;
    cout << "What do you want to do? Catch pokemon, buy pokeballs, sell pokemons or check your stats?\n\n"; 
    cout << "Press 1 (and than ENTER) for catching a pokemon. \nPress 2 (and than ENTER) to buy pokeballs. \nPress 3 (and than ENTER) to sell pokemons. \nPress 4 (and than ENTER) to check your stats.";
    cout << "\nYour option: ";
    cin >> n;
    main();
    if(n = 1)
    {
         if(x > 0 && x < 6)
         {
              if(c = 1)
                            {
                                 cout << "Suceed! You caught a pokemon!";
                                 y = y+1;
                                 again();
                            }
              else if(c = 2)
                            {
                                 cout << "Failure! You didn't caugh any pokemon and lost a pokeball instead!";
                                 x = x-1;
                                 again();
                            }
              else if(c = 3)
                            {
                                 cout << "Failure! You didn't caugh any pokemon!";
                                 x = x-1;
                                 again();
                            }
              else if(c = 4)
                            {
                                 cout << "Failure! You didn't caugh any pokemon!";
                                 x = x-1;
                                 again();
                            }
              else if(c = 5)
                            {
                                 cout << "Suceed! You caught a pokemon!";
                                 y = y+1;
                                 again();
                            }
         }
         else 
         {
              again();
              cout << "You don't have any pokeball."; 
         }
    }
    else if(n = 2)
    {
         if(x < 6)
         {
              if(z > 199)
              {
                   x = 5;
                   z = z-200;
                   again();
              }
              else 
              { 
                   again();
                   cout << "You need at least 200$ to buy pokeballs!"; 
              }
         }
         else 
         { 
              cout << "You got 5 pokeballs already";
              again(); 
         }
    }
    else if(n = 3)
    {
         if(y > 0)
         {
              cout << "You gave " << y << " pokemons and got " << y*200 << "$ in return";
              z = y*200;
              y = 0;
              again();
         }
         else 
         { 
              cout << "You don't have any pokemon in your bag.";
              again(); 
         }
    }
    else if(n = 4)
    {
         cout << "You got " << x << " pokeballs, " << y << " pokemons and " << z << "$ in your bag.\n";
         again();
    }
    else 
    {
         again();
    }
    system("pause");
    return 0;
}


That's the code I got. There's a problem when I'm doing this, whatever happens, it always keeps me to n = 1. That's why I want to separate it (n=1 brings to n1 and n=2 brings to n2 and so on) and when it is done with the function there, it returns back to the int again()

Hope this is able to be undertstood.
Read about "iteration" and "loops".
Also - if you're comparing to see if two things are equal, you need to use the equality operator ==, not the assignment operator =.

@wildblue

Even if I use the ==, still nothing will change. For some reasons, when I put the 2 number, it will tell me "Suceed! You caught a pokemon". Also, is there any way to random numbers from 1 to 5? My way of c = c > 0 && c < 6 is not working. The result is always success.

Thanks for any answer given.
Why are you trying to call main on line 8 after they input a value for n?

And I'm not sure where these variables are defined - c,n,x,y ?

Random - see rand and srand in the <cstdlib>
http://www.cplusplus.com/reference/cstdlib/rand/

If your compiler supports C++11 standard there is new <random>
http://www.cplusplus.com/reference/random/
Last edited on
Topic archived. No new replies allowed.