Hi
I'm new to programming and am writing a booking system in C, in a C++ COMPILER, its nearly finished, but I amgoing round in circles getting the last couple of bugs out, and I can no longer see the wood for the trees, I spent 3 days re-structuring it, having only just understood how program flow is supposed to work,.(I had functions calling functions calling functions, and none of it was going back to main!, anyway I think ive sorted most of it, nearly)
Below is the code, this is probably easy, but the problem is(taking one function for example), in the oneseat function below, I can validate input between 1 and 20 with'if (row <= 0 || row >20)', but if I put a char in, rather than the program seeing that the input was not included between 1 & 20, it recursively loops, and I have to restart the program. (I originally tried an invalid choice function, set to the default on the switch in menu, but this was doing the same!)
Can anyone offer advice on validating this type of input??
Many Thanks in advance.
Dave
#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
int row,column,row_no_start,row_no_finish,count;
char position [21][11], seat, menu_input, goback;
/********************************/
/* */
/* DECLARED FUNCTIONS */
/* */
/********************************/
void display_status() ;
void oneseat();
void series_seats();
void onerow();
void menu();
/********************************/
/* */
/* MAIN BODY */
/* */
/********************************/
void main()
{
/*array default setting*/
{
for ( row=1; row<21; row++)
for ( column=1; column<11; column++)
position [row][column] ='.';
}
/*do switch while */
do
menu();
while (menu_input !='8');
}
/********************************/
/* */
/* MENU STATUS FUNCTION */
/* */
/********************************/
void menu()
{
//system ("cls"
printf("\n\t\tWELCOME TO THE EASYVIEW THEATRE BOOKING SYSTEM\n"
printf("\n\t\t----------------------------------------------\n"
printf("\n\t\t 1. Book an individual seat\n"
printf("\n\t\t 2. Book a series of seats\n"
printf("\n\t\t 3. Book a complete row of seats\n"
printf("\n\t\t 4. UnBook an individual seat\n"
printf("\n\t\t 5. UnBook a series of seats\n"
printf("\n\t\t 6. UnBook a complete row of seats\n"
printf("\n\t\t 7. Display the current seating plan\n"
printf("\n\t\t 8. Exit the system\n"
printf("\n\t\t Please enter a choice between 1 and 8 : "
scanf("%c", &menu_input);
if (menu_input<=3)
seat='x';
else
seat='.';
//printf("Press ENTER to return to menu"
//getch();
switch(menu_input)
{
case '1':{oneseat();break;}
case '2':{series_seats();break;}
case '3':{onerow();break;}
case '4':{oneseat();break;}
case '5':{series_seats();break;}
case '6':{onerow();break;}
case '7':{display_status();break;}
case '8':{printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t Good-bye \n\n\n\n\n\n\n\n\n\n\n\n\n\a"break;}
}
}
/********************************/
/* */
/* DISPLAY STATUS FUNCTION */
/* */
/********************************/
void display_status()
{
//system("cls"
printf("Key:\t\tX = Seat has been booked . = Seat is free"
printf("\n(Front of Cinema) C O L U M N S"
printf("\n\t 1 2 3 4 5"
printf(" 6 7 8 9 10\n\t"
count=1;
for ( row=1; row<21; row++)
{
printf("%2d", count++);
for ( column=1; column<11; column++)
printf ("%5c", position[row][column]);
printf("\n\t"
}
printf("Press ENTER to return to menu"
getch();
}
/********************************/
/* */
/* ONE SEAT FUNCTION */
/* */
/********************************/
void oneseat()
{
//system ("cls"
{
printf("\n\t\tPlease enter data carefully!"
printf("\n"
printf("\n\t\tEnter a Row Number between 1 and 20 : "
scanf("%d",&row);
if (row <= 0 || row >20)
oneseat();
printf("\n\t\tEnter a Column Number between 1 and 10 : "
scanf("%d",&column);
if (column <= 0 || column >10)
oneseat();
position[row][column]=seat;
}
{
display_status();
}
}
/********************************/
/* */
/* SERIES FUNCTION */
/* */
/********************************/
void series_seats ()
{
//system("cls"
{
printf("\n\t\tPlease enter data carefully!"
printf("\n"
printf("\n\t\tEnter a Row Number between 1 and 20 : "
scanf("%d",&row);
if (row <= 0 || row >20 )
series_seats();
printf("\n\t\tEnter a Column, where the booking series"
printf("\n\t\twill begin, between 1 and 10 : "
scanf("%d",&row_no_start);
if (row_no_start <= 0 || row_no_start >10 )
series_seats();
printf("\n\t\tEnter a final column, where the series"
printf("\n\t\twill end, between the number you have"
printf("\n\t\tjust entered and 10 : "
scanf("%d",&row_no_finish);
if (row_no_finish < row_no_start)
series_seats();
for (column=row_no_start;column<=row_no_finish;column++)
position[row][column]=seat;
}
{
display_status();
}
}
/********************************/
/* */
/* ONE ROW FUNCTION */
/* */
/********************************/
void onerow()
{
//system("cls"
{
printf("\n\t\tPlease enter data carefully!"
printf("\n"
printf("\n\t\tEnter a Row Number between 1 and 20,"
printf("\n\t\tto book all 10 seats along that row : "
scanf("%d",&row);
if (row <= 0 || row >20 )
onerow();
for(column=1;column<11;column++)
position[row][column]=seat;
}
{
display_status();
}
}
I'm new to programming and am writing a booking system in C, in a C++ COMPILER, its nearly finished, but I amgoing round in circles getting the last couple of bugs out, and I can no longer see the wood for the trees, I spent 3 days re-structuring it, having only just understood how program flow is supposed to work,.(I had functions calling functions calling functions, and none of it was going back to main!, anyway I think ive sorted most of it, nearly)
Below is the code, this is probably easy, but the problem is(taking one function for example), in the oneseat function below, I can validate input between 1 and 20 with'if (row <= 0 || row >20)', but if I put a char in, rather than the program seeing that the input was not included between 1 & 20, it recursively loops, and I have to restart the program. (I originally tried an invalid choice function, set to the default on the switch in menu, but this was doing the same!)
Can anyone offer advice on validating this type of input??
Many Thanks in advance.
Dave
#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
int row,column,row_no_start,row_no_finish,count;
char position [21][11], seat, menu_input, goback;
/********************************/
/* */
/* DECLARED FUNCTIONS */
/* */
/********************************/
void display_status() ;
void oneseat();
void series_seats();
void onerow();
void menu();
/********************************/
/* */
/* MAIN BODY */
/* */
/********************************/
void main()
{
/*array default setting*/
{
for ( row=1; row<21; row++)
for ( column=1; column<11; column++)
position [row][column] ='.';
}
/*do switch while */
do
menu();
while (menu_input !='8');
}
/********************************/
/* */
/* MENU STATUS FUNCTION */
/* */
/********************************/
void menu()
{
//system ("cls"
printf("\n\t\tWELCOME TO THE EASYVIEW THEATRE BOOKING SYSTEM\n"
printf("\n\t\t----------------------------------------------\n"
printf("\n\t\t 1. Book an individual seat\n"
printf("\n\t\t 2. Book a series of seats\n"
printf("\n\t\t 3. Book a complete row of seats\n"
printf("\n\t\t 4. UnBook an individual seat\n"
printf("\n\t\t 5. UnBook a series of seats\n"
printf("\n\t\t 6. UnBook a complete row of seats\n"
printf("\n\t\t 7. Display the current seating plan\n"
printf("\n\t\t 8. Exit the system\n"
printf("\n\t\t Please enter a choice between 1 and 8 : "
scanf("%c", &menu_input);
if (menu_input<=3)
seat='x';
else
seat='.';
//printf("Press ENTER to return to menu"
//getch();
switch(menu_input)
{
case '1':{oneseat();break;}
case '2':{series_seats();break;}
case '3':{onerow();break;}
case '4':{oneseat();break;}
case '5':{series_seats();break;}
case '6':{onerow();break;}
case '7':{display_status();break;}
case '8':{printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t Good-bye \n\n\n\n\n\n\n\n\n\n\n\n\n\a"break;}
}
}
/********************************/
/* */
/* DISPLAY STATUS FUNCTION */
/* */
/********************************/
void display_status()
{
//system("cls"
printf("Key:\t\tX = Seat has been booked . = Seat is free"
printf("\n(Front of Cinema) C O L U M N S"
printf("\n\t 1 2 3 4 5"
printf(" 6 7 8 9 10\n\t"
count=1;
for ( row=1; row<21; row++)
{
printf("%2d", count++);
for ( column=1; column<11; column++)
printf ("%5c", position[row][column]);
printf("\n\t"
}
printf("Press ENTER to return to menu"
getch();
}
/********************************/
/* */
/* ONE SEAT FUNCTION */
/* */
/********************************/
void oneseat()
{
//system ("cls"
{
printf("\n\t\tPlease enter data carefully!"
printf("\n"
printf("\n\t\tEnter a Row Number between 1 and 20 : "
scanf("%d",&row);
if (row <= 0 || row >20)
oneseat();
printf("\n\t\tEnter a Column Number between 1 and 10 : "
scanf("%d",&column);
if (column <= 0 || column >10)
oneseat();
position[row][column]=seat;
}
{
display_status();
}
}
/********************************/
/* */
/* SERIES FUNCTION */
/* */
/********************************/
void series_seats ()
{
//system("cls"
{
printf("\n\t\tPlease enter data carefully!"
printf("\n"
printf("\n\t\tEnter a Row Number between 1 and 20 : "
scanf("%d",&row);
if (row <= 0 || row >20 )
series_seats();
printf("\n\t\tEnter a Column, where the booking series"
printf("\n\t\twill begin, between 1 and 10 : "
scanf("%d",&row_no_start);
if (row_no_start <= 0 || row_no_start >10 )
series_seats();
printf("\n\t\tEnter a final column, where the series"
printf("\n\t\twill end, between the number you have"
printf("\n\t\tjust entered and 10 : "
scanf("%d",&row_no_finish);
if (row_no_finish < row_no_start)
series_seats();
for (column=row_no_start;column<=row_no_finish;column++)
position[row][column]=seat;
}
{
display_status();
}
}
/********************************/
/* */
/* ONE ROW FUNCTION */
/* */
/********************************/
void onerow()
{
//system("cls"
{
printf("\n\t\tPlease enter data carefully!"
printf("\n"
printf("\n\t\tEnter a Row Number between 1 and 20,"
printf("\n\t\tto book all 10 seats along that row : "
scanf("%d",&row);
if (row <= 0 || row >20 )
onerow();
for(column=1;column<11;column++)
position[row][column]=seat;
}
{
display_status();
}
}