0

For some reason, my program wont let me call main. Im trying to make the program repeat so i can keep adding stuff into main, but it will pretend like main isnt there and skip it.

Heres my code, ive tracked it line by line and even when i enter in the correct info it just refuses to read in the int main(); Any thoughts?

edit: im a moron. Thanks for the help!

#include <iostream>

using namespace std;

,,,

int main()
{
    // New OrderedList
    OrderedList OrderedList;//no constructor called - is head initialized to NULL?
    char repeat;

    int choice = 0, data;

    cout << "Choose from the following menu options,\n"
         << "1: Add an item\n"
         << "2: Search for an item\n"
         << "3: Delete an item\n"
         << "4: Display the list\n"
         << "5: Destroy the list\n";
    cin >> choice;

        if (choice <= 3)
    {
        cout << "\nPlease enter the item.";
        cin >> data; 
    }
    switch(choice)
    {
    case 1:
        OrderedList.Insert(data);
        break;
    case 2: 
        OrderedList.Search(data);
        break;
    case 3: 
        OrderedList.Delete(data);
        break;
    case 4:
        OrderedList.Print();
        break;
    case 5:
        //delete OrderedList; "no constructor called - is head initialized to NULL?"
        break;
    }

    cout << "Repeat Y/N?\n";
    cin >> repeat;

    if (repeat == 'y' || repeat == 'Y')
    int main();
    return 0;
}
vsoftco
  • 55,410
  • 12
  • 139
  • 252
Kdizzile
  • 29
  • 4
  • 6
    (1) You are not calling main, you are *declaring* is: `int main();` <= declaration. (2) You should never call `main()`. You should create another function. – Galik Nov 30 '14 at 00:56
  • Delete "int" keyword when calling main. – antonpp Nov 30 '14 at 00:57
  • This thread talks about calling `main()`: http://stackoverflow.com/questions/4518598/is-it-legal-to-recurse-into-main-in-c – Galik Nov 30 '14 at 01:08
  • Do not call `main`, for that is illegal. Instead, make your own `Main` and call that. Also, avoid needless recursion, for down that path the stack explodes. – Yakk - Adam Nevraumont Nov 30 '14 at 01:12

4 Answers4

4

You should never call main function inside your application, instead you can try using while loop.

int main(){    
   char repeat = 'Y';

   while( repeat == 'y' || repeat == 'Y' )    
   {
      //do some stuff

      cout << "Repeat? Y/N" << endl;
      cin >> repeat;    
   }

   return 0; 
}

Good luck!

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
rzaaeeff
  • 850
  • 1
  • 10
  • 18
3

You just shouldn't call main() recursively, in fact, you shouldn't call it at all. In fact, the C++ standard forbids calling using main in a "potentially evaluated expression", see §3.6.1.3 from the standard (as pointed out by @Captain Obvlious). main() is the C++ startup function of your program, invoked by the runtime library after all other (possible) initializations have been performed.

If you want to repeat what it's in main(), just use a while (or for) loop.

PS: the following seem to work:

Use a static variable as a stop condition, and call main() recursively, however I have to say I have never seen this practice, and I wouldn't recommend it at all. Just tested that this works:

#include <iostream>
using namespace std;

// calling main() 10 times, recursively
int main() {
    static int i = 10; 
    cout << "Hello, World!" << endl; 

    i--;
    if(i>0)
        main();
}

However, if you compile with -pedantic or -pedantic-errors, you'll get a warning/error respectively, stating that you shouldn't/cannot call main();

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Well, actually you can call main in our code. Though, it is not recommended=) – antonpp Nov 30 '14 at 00:58
  • What you can do, of course, is create another function like 'int mainAux(int argc, char ** argv)', move all of your code into there, and then call that function from main() (or from mainAux()) as often as you like. – Jeremy Friesner Nov 30 '14 at 00:59
  • @Anton Thanks, actually was just editing the post, after testing a segfault by myself :). – vsoftco Nov 30 '14 at 00:59
  • 3
    For reference - §3.6.1.3 - The function main shall not be used within a program. – Captain Obvlious Nov 30 '14 at 01:03
  • @CaptainObvlious thanks, that's interesting. Even with `-Wall -Wextra` g++ flies through the code... – vsoftco Nov 30 '14 at 01:07
  • @Yakk: Unfortunately, I haven't written my own cpp compiler yet=) Try the code from this question if you don't trust me. http://stackoverflow.com/questions/2532912/call-main-itself-in-c – antonpp Nov 30 '14 at 01:12
  • @CaptainObvlious , with `-pedantic` you get an warning, and with `-pedantic-errors` an error. Thanks, every day I learn something new on SO :) – vsoftco Nov 30 '14 at 01:14
  • **-1** " the operating system calls it for you". I like this answer overall, but I feel obliged to downvote due to this dangerous disinformation. In C it could work as a beginner's point of view, but in C++, where user code can be executed before `main`, this is just dangerously misleading. – Cheers and hth. - Alf Nov 30 '14 at 01:14
  • @Cheersandhth.-Alf No problem. I agree code can run before `main()`, but `main()` itself is called by the OS, isn't it? I mean, the entry point to it, can you call it by yourself? – vsoftco Nov 30 '14 at 01:16
  • In addition to correcting the plain error, it would be nice (good for readers) if you would change the terminology a little. "Entry point" for `main` is a Microsoft tech writer's terminology. It's not at all the same as the linker's entry point option. – Cheers and hth. - Alf Nov 30 '14 at 01:16
  • @vsoftco: no, `main` is not the linking level (machine code level) entry point. it's just a C and C++ startup function, called by the runtime library after various initialization has been performed. – Cheers and hth. - Alf Nov 30 '14 at 01:17
  • @Cheersandhth.-Alf Changed, feel free to edit if you think something is not entirely correct. – vsoftco Nov 30 '14 at 01:20
  • **0** Removed downvote since answer's fixed. @vsoftco: well it's very difficult to get this 100% right. you now write "after all possible initializations", but the standard allows some initializations to be deferred to after the first statement of `main`. as far as i know that's in support of dynamic libraries (and the only such support in the entire standard). but it just complicates things. – Cheers and hth. - Alf Nov 30 '14 at 01:24
  • @Cheersandhth.-Alf damn... yes, I agree, and that's one of the reasons I hate and at the same time love C++, you have great power, but there are lots and lots of exceptions/subtle points, that I feel will take a lifetime to master. – vsoftco Nov 30 '14 at 01:26
2

If you change the line

int main();

(which is a function declaration) to

main();

which would be a function call, it is likely going to work. However, it would be better to wrap the contents of main in a loop.

EDIT: I just checked the standards, and interestingly, recursive calls to main are only illegal in C++. I find that surprising.

EDIT 2: Oh, the question is tagged C++. In that case, you are forbidden from calling main recursively! Demons will come out of your nose and whatnot.

Wintermute
  • 42,983
  • 5
  • 77
  • 80
  • AFAIK C allows you to call `main` regardless if it's recursively or not. Whether it is allowed in C or not is irrelevant as the question is tagged C++. – Captain Obvlious Nov 30 '14 at 01:06
  • @CaptainObvlious just checked the standard, you are right, however it's strange that g++ doesn't emit at least a warning, at the program compiles (and runs) just fine – vsoftco Nov 30 '14 at 01:11
  • How about while initializing a global variable like `int a = main();`. That's not recursive :) – Captain Obvlious Nov 30 '14 at 01:15
  • Recursion is not, in fact, necessary for the program to become malformed. The standard says "The function `main` shall not be used (3.2) within a program." in 3.6.1 (3). – Wintermute Nov 30 '14 at 01:17
  • @CaptainObvlious corrected my answer, you are right – vsoftco Nov 30 '14 at 01:34
1

Do not call main. Instead, use the following 1 liner

int main(int argc, char* argv[]) { 
   return (myMain(argc, char* argv[]);) 
}

to use your own main ... which can be called recursively.

int myMain(int argc, char* argv[]) 
{
   // what ever you want, including recursion, 
   // but do not call what you are using for "int main(int, char**)"
}

However, now you should be able to see that "main" has special meaning to all of your peers, so there is only confusion in using any variation on "main".

Example: in my file "dumy142.cc", I prefer to use a (somewhat) more meaningfull name, such as "int t142(int argc, char* argv[])":

int main(int argc, char* argv[]) {
   return(t142(arc, argv);
}
2785528
  • 5,438
  • 2
  • 18
  • 20