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.
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.
#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;
}
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();
}
elseif(c = 2)
{
cout << "Failure! You didn't caugh any pokemon and lost a pokeball instead!";
x = x-1;
again();
}
elseif(c = 3)
{
cout << "Failure! You didn't caugh any pokemon!";
x = x-1;
again();
}
elseif(c = 4)
{
cout << "Failure! You didn't caugh any pokemon!";
x = x-1;
again();
}
elseif(c = 5)
{
cout << "Suceed! You caught a pokemon!";
y = y+1;
again();
}
}
else
{
again();
cout << "You don't have any pokeball.";
}
}
elseif(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();
}
}
elseif(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();
}
}
elseif(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()
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.