Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot validate char, *Begginer, Please help*

Status
Not open for further replies.

techskool

Technical User
Jun 20, 2002
151
GB
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 (&quot;cls&quot;);
printf(&quot;\n\t\tWELCOME TO THE EASYVIEW THEATRE BOOKING SYSTEM\n&quot;);
printf(&quot;\n\t\t----------------------------------------------\n&quot;);
printf(&quot;\n\t\t 1. Book an individual seat\n&quot;);
printf(&quot;\n\t\t 2. Book a series of seats\n&quot;);
printf(&quot;\n\t\t 3. Book a complete row of seats\n&quot;);
printf(&quot;\n\t\t 4. UnBook an individual seat\n&quot;);
printf(&quot;\n\t\t 5. UnBook a series of seats\n&quot;);
printf(&quot;\n\t\t 6. UnBook a complete row of seats\n&quot;);
printf(&quot;\n\t\t 7. Display the current seating plan\n&quot;);
printf(&quot;\n\t\t 8. Exit the system\n&quot;);
printf(&quot;\n\t\t Please enter a choice between 1 and 8 : &quot;);
scanf(&quot;%c&quot;, &menu_input);

if (menu_input<=3)
seat='x';
else
seat='.';

//printf(&quot;Press ENTER to return to menu&quot;);
//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(&quot;\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&quot;);break;}


}

}


/********************************/
/* */
/* DISPLAY STATUS FUNCTION */
/* */
/********************************/

void display_status()
{
//system(&quot;cls&quot;);
printf(&quot;Key:\t\tX = Seat has been booked . = Seat is free&quot;);
printf(&quot;\n(Front of Cinema) C O L U M N S&quot;);
printf(&quot;\n\t 1 2 3 4 5&quot;);
printf(&quot; 6 7 8 9 10\n\t&quot;);
count=1;
for ( row=1; row<21; row++)
{
printf(&quot;%2d&quot;, count++);
for ( column=1; column<11; column++)
printf (&quot;%5c&quot;, position[row][column]);
printf(&quot;\n\t&quot;);
}
printf(&quot;Press ENTER to return to menu&quot;);
getch();
}

/********************************/
/* */
/* ONE SEAT FUNCTION */
/* */
/********************************/

void oneseat()
{
//system (&quot;cls&quot;);
{
printf(&quot;\n\t\tPlease enter data carefully!&quot;);
printf(&quot;\n&quot;);
printf(&quot;\n\t\tEnter a Row Number between 1 and 20 : &quot;);
scanf(&quot;%d&quot;,&row);
if (row <= 0 || row >20)
oneseat();
printf(&quot;\n\t\tEnter a Column Number between 1 and 10 : &quot;);
scanf(&quot;%d&quot;,&column);
if (column <= 0 || column >10)
oneseat();
position[row][column]=seat;
}
{
display_status();
}
}

/********************************/
/* */
/* SERIES FUNCTION */
/* */
/********************************/

void series_seats ()
{
//system(&quot;cls&quot;);
{
printf(&quot;\n\t\tPlease enter data carefully!&quot;);
printf(&quot;\n&quot;);
printf(&quot;\n\t\tEnter a Row Number between 1 and 20 : &quot;);
scanf(&quot;%d&quot;,&row);
if (row <= 0 || row >20 )
series_seats();
printf(&quot;\n\t\tEnter a Column, where the booking series&quot;);
printf(&quot;\n\t\twill begin, between 1 and 10 : &quot;);
scanf(&quot;%d&quot;,&row_no_start);
if (row_no_start <= 0 || row_no_start >10 )
series_seats();
printf(&quot;\n\t\tEnter a final column, where the series&quot;);
printf(&quot;\n\t\twill end, between the number you have&quot;);
printf(&quot;\n\t\tjust entered and 10 : &quot;);
scanf(&quot;%d&quot;,&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(&quot;cls&quot;);
{
printf(&quot;\n\t\tPlease enter data carefully!&quot;);
printf(&quot;\n&quot;);
printf(&quot;\n\t\tEnter a Row Number between 1 and 20,&quot;);
printf(&quot;\n\t\tto book all 10 seats along that row : &quot;);
scanf(&quot;%d&quot;,&row);
if (row <= 0 || row >20 )
onerow();
for(column=1;column<11;column++)
position[row][column]=seat;
}
{
display_status();
}
}
 
Here is your program with some minor changes,this should help a little bit.

#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 = '8', 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 */

while (menu_input !='8');
menu();
}
/********************************/
/* */
/* MENU STATUS FUNCTION */
/* */
/********************************/

void menu()
{

//system (&quot;cls&quot;);
printf(&quot;\n\t\tWELCOME TO THE EASYVIEW THEATRE BOOKING SYSTEM\n&quot;);
printf(&quot;\n\t\t----------------------------------------------\n&quot;);
printf(&quot;\n\t\t 1. Book an individual seat\n&quot;);
printf(&quot;\n\t\t 2. Book a series of seats\n&quot;);
printf(&quot;\n\t\t 3. Book a complete row of seats\n&quot;);
printf(&quot;\n\t\t 4. UnBook an individual seat\n&quot;);
printf(&quot;\n\t\t 5. UnBook a series of seats\n&quot;);
printf(&quot;\n\t\t 6. UnBook a complete row of seats\n&quot;);
printf(&quot;\n\t\t 7. Display the current seating plan\n&quot;);
printf(&quot;\n\t\t 8. Exit the system\n&quot;);
printf(&quot;\n\t\t Please enter a choice between 1 and 8 : &quot;);
scanf(&quot;%c&quot;, &menu_input);

if (menu_input<=3)
seat='x';
else
seat='.';

//printf(&quot;Press ENTER to return to menu&quot;);
//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(&quot;\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&quot;);break;}


}

}


/********************************/
/* */
/* DISPLAY STATUS FUNCTION */
/* */
/********************************/

void display_status()
{
//system(&quot;cls&quot;);
printf(&quot;Key:\t\tX = Seat has been booked . = Seat is free&quot;);
printf(&quot;\n(Front of Cinema) C O L U M N S&quot;);
printf(&quot;\n\t 1 2 3 4 5&quot;);
printf(&quot; 6 7 8 9 10\n\t&quot;);
count=1;
for ( row=1; row<21; row++)
{
printf(&quot;%2d&quot;, count++);
for ( column=1; column<11; column++)
printf (&quot;%5c&quot;, position[row][column]);
printf(&quot;\n\t&quot;);
}
printf(&quot;Press ENTER to return to menu&quot;);
getch();
}

/********************************/
/* */
/* ONE SEAT FUNCTION */
/* */
/********************************/

void oneseat()
{
//system (&quot;cls&quot;);

printf(&quot;\n\t\tPlease enter data carefully!&quot;);
printf(&quot;\n&quot;);
printf(&quot;\n\t\tEnter a Row Number between 1 and 20 : &quot;);
scanf(&quot;%d&quot;,&row);
if (row > 0 && row <=20)
{

printf(&quot;\n\t\tEnter a Column Number between 1 and 10 : &quot;);
scanf(&quot;%d&quot;,&column);
if (column > 0 && column <=10)
{
position[row][column]=seat;
display_status();
}
}
}

/********************************/
/* */
/* SERIES FUNCTION */
/* */
/********************************/

void series_seats ()
{
//system(&quot;cls&quot;);

printf(&quot;\n\t\tPlease enter data carefully!&quot;);
printf(&quot;\n&quot;);
printf(&quot;\n\t\tEnter a Row Number between 1 and 20 : &quot;);
scanf(&quot;%d&quot;,&row);
if (row > 0 && row <=20)
{
printf(&quot;\n\t\tEnter a Column, where the booking series&quot;);
printf(&quot;\n\t\twill begin, between 1 and 10 : &quot;);
scanf(&quot;%d&quot;,&row_no_start);
if (row_no_start > 0 && row_no_start <=10 )
{
printf(&quot;\n\t\tEnter a final column, where the series&quot;);
printf(&quot;\n\t\twill end, between the number you have&quot;);
printf(&quot;\n\t\tjust entered and 10 : &quot;);
scanf(&quot;%d&quot;,&row_no_finish);
if (row_no_finish < row_no_start)
{
for (column=row_no_start;column<=row_no_finish;column++)
position[row][column]=seat;

display_status();
}
}
}
}

/********************************/
/* */
/* ONE ROW FUNCTION */
/* */
/********************************/

void onerow()
{
//system(&quot;cls&quot;);

printf(&quot;\n\t\tPlease enter data carefully!&quot;);
printf(&quot;\n&quot;);
printf(&quot;\n\t\tEnter a Row Number between 1 and 20,&quot;);
printf(&quot;\n\t\tto book all 10 seats along that row : &quot;);
scanf(&quot;%d&quot;,&row);
if (row >= 0 && row <=20 )
{
for(column=1;column<11;column++)
position[row][column]=seat;

display_status();
}
}
 
Many thanks 'Leibnitz', thats very useful. I'm still trying to run functions to functions, but I can see now I havent been using enough nested 'if' statements.

Thankyou very much.

Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top