Variable check

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AdINo
    New Member
    • Oct 2006
    • 11

    Variable check

    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;
    }
  • devikacs
    New Member
    • Jun 2007
    • 96

    #2
    Hi
    to check if the input is an integer, you could either check if the character entered is between '0' and '9' or check using isdigit function.

    Comment

    • kky2k
      New Member
      • May 2007
      • 34

      #3
      as far ur requirements do follow "devicacs" suggested..
      Coz' the only way to find the size of an integer in c is using sizeof("integer ")..
      That too compiler/O.S dependant..so it varies from machine to machine..

      Comment

      Working...