-7

I am trying to make the simplest of all games, but with a loop function.

I'm given the error "a function-definition is not allowed here before '{', as well as a whole list of [Error] expected '}' at end of input.

Am I not allowed to nest int main() within another? Is that my issue at all? How do I accomplish this code without the nesting?

My knowledge and experience extend no more than a few chapters in two books.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    char again = 'y';
    while (again == 'y')


    int main()
        {

            srand(static_cast<unsigned int>(time (0)));
            int secret = rand() % 100 +1;
            int tries = 0;
            int guess;
            cout << "\tGuess the random number\n\n";

            do 
                {
                    cout << "Enter a guess: ";
                    cin >> guess;
                    ++ tries;
                    if (guess > secret)
                        {
                            cout << "Too High!\n\n:";
                        }
                    else if (guess < secret)
                        {
                            cout << "Too Low!\n\n";
                        }
                    else 
                        {
                            cout << "\nThat's It! You go it in " << tries << " guesses!\n";
                        }
} while (guess != secret);
}

cout << "\n\tWould you like to play again? (y/n): ";
char again;
cin >> again;


}

2 Answers2

3

Your issue is nesting a main function inside of the existing main function. (This is not allowed.) In general, (With the exception of some stuff you can do with structs/lambdas which approximate that kind of functionality) you shouldn't nest functions inside of each-other. If you merely remove the function declaration, then your code should work fine:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    char again = 'y';
    while (again == 'y')

        {

            srand(static_cast<unsigned int>(time (0)));
            int secret = rand() % 100 +1;
            int tries = 0;
            int guess;
            cout << "\tGuess the random number\n\n";

            do
                {
                    cout << "Enter a guess: ";
                    cin >> guess;
                    ++ tries;
                    if (guess > secret)
                        {
                            cout << "Too High!\n\n:";
                        }
                    else if (guess < secret)
                        {
                            cout << "Too Low!\n\n";
                        }
                    else
                        {
                            cout << "\nThat's It! You go it in " << tries << " guesses!\n";
                        }
                } while (guess != secret);

            cout << "\n\tWould you like to play again? (y/n): ";
            cin >> again;
        }


}
Lilith Daemon
  • 1,473
  • 1
  • 19
  • 37
2

As far as I know, you cannot nest int main() inside of int main() for a number of reasons. In general, you can't define one function inside of another (defining "locally"). However, even if you can, your desire to do so should be a massive red flag that your design is off.

NOTE: As Nathan Oliver pointed out in the comments, you can forward-declare a function "locally" (within another function), but the actual implementation must be outside. Even doing this, you should usually second-guess your design at this point.

You need to be asking yourself what you're trying to do.

  • Are you wanting to group code together under a function to call repeatedly? Then create a separate function (with a different name) outside of main().

  • Are you wanting to repeat code? If so, you need a loop or a recursive function structure.

  • Are you wanting to split up some behavior into several functions, but hide all but one function involved? If so, look into classes (not necessarily objects - you can also have static classes.)

Those are just three of many possibilities, and deciding exactly what you want to do and how you want to do it is your responsibility alone to decide, as a programmer.

Brief recap: as far as C++ (and C) are concerned, if you're trying to declare any function inside any other function, you are doing something seriously wrong in design.


On a more technical note, you should also understand precisely what int main() is. It is a special function that is the entry point for your program. No C or C++ program can run without an int main().

You have to have a single instance of this function in main.cpp in order for your program to work. If you have more than one int main(), the computer cannot likely will not be able to find the entry point.

Besides that, having more than one function called main() anywhere in your code is a great way to confuse yourself and others.

In short, you should only have one int main() per program, no exceptions.

CodeMouse92
  • 6,840
  • 14
  • 73
  • 130