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 */
}
fgets()andfscanf()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 thatfscanf()leaves the newline in the buffer, and if followed byfgets()you'll read the empty string and the whole thing will go out of kilter, unstopped by the lack of checking.