Hattori Posted March 9, 2005 Report Share Posted March 9, 2005 Hi, I'm trying in to run a program in c++ using Borland C++ builder 5.5. When I try to compile the program, there is an error that says that I can't use main() as a function. I trying to use the main() function to go back to main. for example, #include<iostream.h>#define SIZE 256void main(){ char n, quit; cin.getline(n, SIZE, '\n'); if(strcmp(n, "Kyosuke") == 0) { cout<<"It matches!"<<"\n"; } else { main(); } while(quit != 'q') { cout<<"Press 'q' to exit the program. "; cin>>quit; }}This main() function works all the time until I'm using Borland. I really want use this function because it's better this way. Is there a way to fix this problem. Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted March 9, 2005 Report Share Posted March 9, 2005 Hello again, Hattori-san! I don't know Borland C++, so I will just ask you if it is allowed to recursively call the main() function? I know that recursive calls in C/C++ are usually allowed, but I am not sure about main().Actually I have just done a Google search, and from this article and others it appears as if recursive calls to main() are allowed in C, but not in C++. Quote Link to comment Share on other sites More sharing options...
Hattori Posted March 9, 2005 Author Report Share Posted March 9, 2005 チキショー! No wonder that C++ wouldn't allow it. That's lame, lol. Oh well, rules are rules and must follow accordingly. アリガトウゴザイマシュー。 Quote Link to comment Share on other sites More sharing options...
scuzzman Posted March 9, 2005 Report Share Posted March 9, 2005 Hi, I'm trying in to run a program in c++ using Borland C++ builder 5.5. When I try to compile the program, there is an error that says that I can't use main() as a function. I trying to use the main() function to go back to main. for example, #include#define SIZE 256void main(){ char n, quit; cin.getline(n, SIZE, '\n'); if(strcmp(n, "Kyosuke") == 0) { cout } else { main(); } while(quit != 'q') { cout cin>>quit; }}This main() function works all the time until I'm using Borland. I really want use this function because it's better this way. Is there a way to fix this problem.While you can recursively call main(), it's widely disapproved of, and not ANSI-compliant.That said, I might reccomend this approach:#include <iostream>#define SIZE 256int main() // main() should usually return an int for OS cross-compatibility{ char n[SIZE], quit; quit = "0"; // Defines quit so program will execute properly while ( quit != 'q') { cin.getline( n, SIZE, '\n' ); if( strcmp( n, "Kyosuke" ) == 0 ) { cout << "It matches!" << endl; } else { cout << "No Match" << endl; // You can do whatever you want here... quit = "1"; // Assures quit != 'q' so execution continues } cout<<"Press 'q' to exit the program. "; cin>>quit; if (quit = 'q') { return 0; // force execution to stop } }return 0; //program exit}Encapsulating it all in a while() loop keeps you from having to recursively call main(). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.