0

I've developed a C program on Turbo C++; however, it glitches out once the program begins. I think it's most likely a runtime error, but I'm not entirely sure nor do I know how to resolve the issue.

The program does not close though, and despite providing glitched-out menu options, it refuses to respond to any of my inputs. It wouldn't let me interact or stop the program unless I tried screenshotting it, which forced Turbo C++ to minimize the screen and thus I was able to close the window. Holding Alt + X didn't even work.

The following is a link to a 4 second recording of what happens when the program is executed: text

A snippet of the code is also presented that's supposed to be executed when the user starts the program. :

/* Libraries used in the program */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <dos.h>

/*List of function prototypes used in program */
   void hName(void);
   int mainOpts(void);
   int menu(guestRec[], int, roomRec[], int);
   int guestOpts(guestRec[], int, roomRec[], int);
   void roomOpts(roomRec[], int);
   void getadBill(guestRec[], int);
   int reGuest(guestRec[], int);
   float addServ(void);
   void guestCheck(guestRec[], int, roomRec[], int);
   void checkIn(guestRec[], int, roomRec[], int);
   void editGuest(guestRec[], int);
   void checkOut(guestRec[], int, roomRec[], int);
   float addBill(guestRec, int);
   void payment(guestRec[], int, roomRec[], int);
   void exitOpt(void);
   int getRoomType(void);
   void pRoomType(int);
   int getRoomStat(void);
   void pRoomStat(int);
   int pRoomList(roomRec[], int, int, int);

main()
{
   int roomCount;
   int tguestId;
   int troomId;
   int roomNum;
   int n;
   int size;
   int s;
   int loc;
   guestRec guests[50];
   roomRec rooms[10];
   FILE *guestfile = fopen( "GUEST.TXT", "r" );
   FILE *roomfile = fopen( "ROOM.TXT", "r");
   FILE *servfile = fopen( "SERV.TXT", "r" );

   guestCount = 0;


   fscanf( guestfile, "%d", &tguestId );

   while ( tguestId != 0 )
   {
      guests[guestCount].guestId = tguestId;
      fscanf( guestfile, "%12[^,]", guests[guestCount].fname );
      fgets( guests[guestCount].lname, 13, guestfile );
      fgets( guests[guestCount].phoneNum, 13, guestfile );
      fgets( guests[guestCount].email, 25, guestfile );
      fscanf( guestfile, "%d", &guests[guestCount].gender);
      fscanf( guestfile, "%d", &guests[guestCount].roomID );
      fscanf( guestfile, "%f", &guests[guestCount].adBill );
      fscanf( guestfile, "%d", &guests[guestCount].checkIn.day );
      fscanf( guestfile, "%d", &guests[guestCount].checkIn.mon );
      fscanf( guestfile, "%d", &guests[guestCount].checkIn.yr );
      fscanf( guestfile, "%d", &guests[guestCount].checkOut.day );
      fscanf( guestfile, "%d", &guests[guestCount].checkOut.mon );
      fscanf( guestfile, "%d", &guests[guestCount].checkOut.yr );
      fscanf( guestfile, "%d", &guests[guestCount].numDays );
      fscanf( guestfile, "%d", &tguestId );
      guestCount++;

    }

    roomCount = 0;

    /* Records from the room file is read into the array */
    fscanf( roomfile, "%d", &troomId );

    while ( troomId != 0 )
    {
       rooms[roomCount].roomID = troomId;
       fgets( rooms[roomCount].roomName, 20, roomfile );
       fscanf( roomfile, "%d", &rooms[roomCount].roomSize );
       fscanf( roomfile, "%d", &rooms[roomCount].status );
       fscanf( roomfile, "%d", &rooms[roomCount].rType );
       fscanf( roomfile, "%d", &rooms[roomCount].maxGuests );
       fscanf( roomfile, "%f", &rooms[roomCount].rPrice );
       roomCount = roomCount + 1;
       fscanf( roomfile, "%d", &troomId );

     }

     guestCount = menu( guests, guestCount, rooms, roomCount );

     /* The guest file is closed and re-opened as a write file to accept */
     /* the information from the array, where changes were made */
     fclose( guestfile );
     guestfile = fopen( "GUEST.TXT", "w" );

     for ( loc = 0; loc < guestCount; loc++ )
     {
       fprintf( guestfile, "%d", guests[loc].guestId );
       fprintf( guestfile, "%s", guests[loc].fname );
       fprintf( guestfile, "%s", guests[loc].lname );
       fprintf( guestfile, "%s", guests[loc].phoneNum );
       fprintf( guestfile, "%s", guests[loc].email );
       fprintf( guestfile, " %2d", guests[loc].gender);
       fprintf( guestfile, " %2d", guests[loc].roomID );
       fprintf( guestfile, " %7.2f", guests[loc].adBill );
       fprintf( guestfile, " %2d", guests[loc].checkIn.day );
       fprintf( guestfile, " %2d", guests[loc].checkIn.mon );
       fprintf( guestfile, " %4d", guests[loc].checkIn.yr );
       fprintf( guestfile, " %2d", guests[loc].checkOut.day );
       fprintf( guestfile, " %2d", guests[loc].checkOut.mon );
       fprintf( guestfile, " %4d", guests[loc].checkOut.yr );
       fprintf( guestfile, " %2d\n", guests[loc].numDays );

     }

     fprintf( guestfile, "%d", 0 );

    /* The room file is closed and re-opened as a write file to accept */
    /* the information from the array, where changes were made during  */
    /* program execution */

    fclose( roomfile );
    roomfile = fopen( "ROOMT.TXT", "w" );

    for ( loc = 0; loc < roomCount; loc++ )
    {
      fprintf( roomfile, "%d", rooms[loc].roomID );
      fprintf( roomfile, "%s", rooms[loc].roomName );
      fprintf( roomfile, " %d", rooms[loc].roomSize );
      fprintf( roomfile, " %d", rooms[loc].status );
      fprintf( roomfile, " %d", rooms[loc].rType );
      fprintf( roomfile, " %d", rooms[loc].maxGuests );
      fprintf( roomfile, " %7.2f\n", rooms[loc].rPrice );

    }

    fprintf( roomfile, " %d", 0 );

    return 0;
}


int menu( guestRec guests[], int guestCount, roomRec rooms[], int roomCount )
{

   /* Calls the function to obtain menu choice from the user */
   /* and calls the appropriate function */

   int opt;

   do
   {
      opt = mainOpts();

      if ( opt == 1 )

     guestCount = guestOpts( guests, guestCount, rooms, roomCount );

      else if ( opt == 2 )

     roomOpts( rooms, roomCount );

      else if ( opt == 3 )

     getadBill( guests, guestCount );

      else if ( opt == 4 )

     printf ("\n\n     You have successfully exitted the system\n\n");

      else
      {
     printf ( "\n    Invalid choice" );
     system( "pause" );
      }

    }
    while ( opt != 4 );

    return guestCount;

  }


int mainOpts()
{
   /* This functions obtains the menu option selected from the user */
   /* and returns this choice */

   int menuOp;

   hName();
   printf( "1. GUEST OPTIONS\n\n" );
   printf( "2. ROOM OPTIONS\n\n" );
   printf( "3. ADDITIONAL SERVICES\n\n" );
   printf( "4. EXIT\n\n\n" );
   printf( "Please enter Choice # : " );
   scanf ( "%d", &menuOp );

   if (( menuOp >= 1 ) && ( menuOp <= 4 ))

      return menuOp;

   else

      return 0;

}

Honestly, I entered everything on my keyboard one-by-one, but the program continued glitching out. I tried esc as well, but the program did not respond to that input as well. I also tried using this snippet of code in main(), but the program would not compile at all:

   /* Check if the guest file opened successfully */
   if (guestfile == NULL)
   {
      printf("Error: Unable to open GUEST.TXT. Make sure the file exists.\n");
      return 1; /* Exit with an error code */
   }

   /* Check if the room file opened successfully */
   if (roomfile == NULL)
   {
      printf("Error: Unable to open ROOM.TXT. Make sure the file exists.\n");
      return 1; /* Exit with an error code */
   }
12
  • 2
    TurboC++ came with a pretty good debugger. Have you tried using it? Also, you need to reduce your code to a minimal reproducible example that demonstrates the issue, as well as a clear problem statement (a link off-site to a video isn't one). The process of removing code in order to create that minimal reproducible example often helps you figure out what's causing the issue. Commented Apr 19 at 0:47
  • 1
    One more note is that DOSbox is actually not a good platform for running 16 bit software reliably, but it instead targets 16 bit games, and is widely hailed for that. So, you might get a 2nd opinion from at least one other platform ( like DOSbox-X or what have you ) before you fully blame your code. Finally, I wish to add that I'm in full agreement with the comments from Ken White, especially `minimal reproducible example'. We like to run code that people post and try to duplicate the issue. I still have Turbo C and Borland C++ 3.0 -- wouldn't mind compiling your code and checking here Commented Apr 19 at 4:08
  • 1
    One more thing on the topic of alternate tools, Borland C++ 5.x is available for free on various places on the web. This is a full 32-bit version . . . which is pretty much a TurboC that can run in today's MS Windows. Essentially, if your TurboC code works in this later version but not 16 bit TurboC, then this fact may also point to a problem other than your code. You might also find it easier to spend most of your development time in current Windows and this newer version of Borland C++, and then only do a final compile and deploy in 16-bit TurboC for your finished product. ymmv. Commented Apr 19 at 4:21
  • 3
    Related, if you are a beginner and you are mixing fgets() and fscanf() from the same stream there is a high chance that the code won't do what you expect. Moreover, you never check that any inputs have succeeded. Note that fscanf() leaves the newline in the buffer, and if followed by fgets() you'll read the empty string and the whole thing will go out of kilter, unstopped by the lack of checking. Commented Apr 19 at 6:42
  • 2
    ... one consequence of your uninitialized variables is that when you fail to gain input to any, who knows what damage is done when you use that variable later. You've tried entering from the keyboard too, and an important consideration about taking user input is that your code must be robust enough to handle any unexpected input thrown at it, whether accidental or malicious. Commented Apr 19 at 6:58

1 Answer 1

-1

The issue wasn't with the code itself, but the compiler being used. I don't have the best explanation, but according to the advice that I received from @gregspears:

"Borland C++ 5.x is available for free on various places on the web. This is a full 32-bit version . . . which is pretty much a TurboC that can run in today's MS Windows. Essentially, if your TurboC code works in this later version but not 16 bit TurboC, then this fact may also point to a problem other than your code. You might also find it easier to spend most of your development time in current Windows and this newer version of Borland C++, and then only do a final compile and deploy in 16-bit TurboC for your finished product. ymmv."

Sign up to request clarification or add additional context in comments.

4 Comments

There certainly are issues with the code. This line fscanf( guestfile, "%12[^,]", guests[guestCount].fname ); will begin by reading the previous newline, and will leave a comma in the buffer, so that the next string read with fgets( guests[guestCount].lname, 13, guestfile ); will begin with a comma, and end with a newline. etc.
And looking at the next loop, fgets( rooms[roomCount].roomName, 20, roomfile ); will read just a newline, that was left in the buffer by the previous fscanf(), after which fscanf( roomfile, "%d", &rooms[roomCount].roomSize ); will be trying to read an integer from the unread string.
Not to mention the dearth of error checking.
Even BorlandC++ for Windows is extremely old. It's at least 24 years old. It was mutating into Borland C++ Builder wich had had a dialect of C++. What's wrong with having mingw, mingw-clang, msys2, or WinSDK which ALL are available for free too? Currently only some quite specialized compilers are non-free, like Portland C, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.