1

I'm writing a program which allows user to enter alphanumeric strings and validate their input based on the options available.when I run my code below, it always print option is invalid even though I have entered an option within the range.Can anyone help me with this?Any help will be aprreciated.

#include<stdio.h>
#include<ctype.h>
#include<string.h>

int main (void){

char option;

do{
printf("\n\n----------------------------------------------------\n");
printf("Main Menu\n");
printf("----------------------------------------------------\n");
printf("1. Add Record\n");
printf("2. Delete record\n");
printf("3. List Record\n");
printf("4. Exit\n");
printf("----------------------------------------------------\n");

printf("Enter your option:");
scanf("%c",&option);

if(isdigit(option)){
switch(option){

    case 1:
    add();
    break;

    case 2:
    del();
    break;

    case 3:
    listrec();
    break;

    case 4:
    return 0;

    default:
    printf("The number that you have entered is invalid.Please enter a new option\n");
    break;

}

    else{   
    printf("The option that you have entered is invalid.Please enter a new option\n");  
    }

}while(option!=4);
return 0;
}
3
  • 5
    E.g case 1: --> case '1': Commented Jun 25, 2014 at 10:57
  • option is the ascii code. test : " switch (option - '0') " or " switch (option - '48') ". You should optain the expected result. You can also do : " scanf("%d",&option); " with "option" an int instead of a char. Commented Jun 25, 2014 at 10:58
  • @Strigidis: option - '0' will always work, while option - '48' is character set dependent. Commented Jun 25, 2014 at 11:01

2 Answers 2

2

Besides using 1 instead of '1' (you need to compare the character with the character, not with a number), there is another problem - you don't get the carriage return from the input, and this makes the input fail on the second pass. See below for a possible solution:

#include<stdio.h>
#include<ctype.h>
#include<string.h>

int main (void){

char option;

do {
  printf("\n\n----------------------------------------------------\n");
  printf("Main Menu\n");
  printf("----------------------------------------------------\n");
  printf("1. Add Record\n");
  printf("2. Delete record\n");
  printf("3. List Record\n");
  printf("4. Exit\n");
  printf("----------------------------------------------------\n");

  printf("Enter your option:");

  if(scanf("%c",&option)==1) {


    if(isdigit(option)){
      printf("you entered %c\n", option);
      switch(option){

      case '1':
        printf("adding\n");
        break;

      case '2':
        printf("deleting\n");
        break;

      case '3':
        printf("listing\n");
        break;

      case '4':
        return 0;

      default:
        printf("The number that you have entered is invalid.\nPlease enter a new option:\n");
        break;

      }

    }
    else {
      printf("you entered not a digit: %c\n", option);
      printf("The number that you have entered is invalid.\nPlease enter a new option:\n");
    }

    // empty the input buffer:
    while(getchar()!='\n');

  }

  }

} while(option!=4);
return 0;
}

A few things to note:

  1. There was a } missing in your code sample given - between the end of the switch and the else
  2. I check that scanf returns a value of 1 (valid conversion) - per Nisse's comment
  3. I empty the input buffer after the if by reading until I get an EOL (which stays in the buffer after the scanf). This ensures that the next character read is the next character after the carriage return, and not the carriage return itself.
  4. Miscellaneous clean-up...
Sign up to request clarification or add additional context in comments.

3 Comments

You should only test option if scanf() returns 1. Both scanf() and getchar() may return EOF. Without these checks, you may run into an infinite loop.
@NisseEngström - good point. I made "minimal changes" to get working code, did not do a thorough job. Before my morning coffee... will edit.
Thank you everyone for your help.Really appreciate it.
1

The integer value of a character object that contains a digit is equal to the internal representation of the corresponding character that denotes the digit. For example if the ASCII coding is used then for example '1' has internal representation equal to 49. So you had to write

case 49:

instead of

case 1:

Change case labels the following way that they would not depend on the internal representation of character digits.

    case '1':
    add();
    break;

    case '2':
    del();
    break;

    case '3':
    listrec();
    break;

    case '4':
    return 0;

    default:
    printf("The number that you have entered is invalid.Please enter a new option\n");
    break;

}

    else{   
    printf("The option that you have entered is invalid.Please enter a new option\n");  
    }

}while(option!='4');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.