Reputation: 29
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;
}
Upvotes: 0
Views: 963
Reputation: 5576
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);
}
Upvotes: 1
Reputation: 880
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!
Upvotes: 4
Reputation: 56577
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()
;
Upvotes: 3
Reputation: 44063
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.
Upvotes: 2