Jump to content

problem with Borland C++ builder 5.5


Recommended Posts

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 256

void 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.

Link to comment
Share on other sites

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++.

Link to comment
Share on other sites

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  256

void 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  256

int 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().

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy