Below is my coding , how do i check whether the user input is
interger or not ...... because if the user entered a integer i would like to ask
the user to re-enter ...... meaning this program will only accepts char
thanks in advance
#include <stdio.h>
#include <stdlib.h>
struct stackNode {
int data;
struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
void push( StackNodePtr *topPtr, int info );
char pop( StackNodePtr *topPtr );
int isEmpty( StackNodePtr topPtr );
void printStack( StackNodePtr currentPtr );
void instructions( void );
char Full();
int main()
{
StackNodePtr stackPtr = NULL;
int choice;
char value;
int timer;
timer = 0;
int timer2;
timer2 = 0;
timer = Full();
timer2 = timer;
instructions();
printf( "?" );
scanf( "%d", &choice );
while ( choice != 3 ) {
switch ( choice ) {
case 1:
timer2 = timer2 + 1;
if ( timer2 >= 11 )
{
printf("The stack is full\n");
printStack( stackPtr );
timer2 = timer2 - 1;
break;
}
else
{
printf( "Enter an character: " );
fflush(stdin);
scanf( "%c", &value );
push( &stackPtr, value );
printStack( stackPtr );
break;
}
}
case 2:
if ( !isEmpty( stackPtr ) ) {
printf( "The popped value is %c.\n", pop( &stackPtr ) );
}
printStack( stackPtr );
timer2 = timer2 - 1;
break;
default:
printf( "Invalid choice.\n\n" );
instructions();
break;
}
printf( "?" );
scanf( "%d", &choice );
}
printf( "End of run.\n" );
return 0;
}
void instructions( void )
{
printf( "Enter choice:\n"
"1 to push a value on the stack\n"
"2 to pop a value off the stack\n"
"3 to end program\n" );
}
void push( StackNodePtr *topPtr, int info )
{
StackNodePtr newPtr;
newPtr = ( StackNode * )malloc( sizeof( StackNode ) );
if ( newPtr != NULL ) {
newPtr->data = info;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
}
else {
printf( "%c not inserted. No memory available.\n", info );
}
}
char pop( StackNodePtr *topPtr )
{
StackNodePtr tempPtr;
char popValue;
tempPtr = *topPtr;
popValue = ( *topPtr )->data;
*topPtr = ( *topPtr )->nextPtr;
free( tempPtr );
return popValue;
}
void printStack( StackNodePtr currentPtr )
{
if ( currentPtr == NULL ) {
printf( "The stack is empty.\n\n" );
}
else {
printf( "The stack is:\n" );
while ( currentPtr != NULL ) {
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
}
printf( "NULL\n\n" );
}
}
int isEmpty( StackNodePtr topPtr )
{
return topPtr == NULL;
}
char Full()
{
int test;
test = 0;
return test;
}