user3676752
user3676752

Reputation: 19

c program.data validation. convert char to integer

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;
}

Upvotes: 1

Views: 263

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

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');

Upvotes: 1

Floris
Floris

Reputation: 46425

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...

Upvotes: 2

Related Questions