c 程序.数据验证。将字符转换为整数

标签 c validation input

我正在编写一个程序,允许用户输入字母数字字符串并根据可用选项验证他们的输入。当我运行下面的代码时,它总是打印选项无效,即使我输入了范围内的选项。谁能帮我解决这个问题?我们将不胜感激。

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

最佳答案

除了使用 1 而不是 '1' (你需要将字符与字符进行比较,而不是与数字进行比较),还有另一个问题 - 你不' t 从输入中获取回车,这使得输入在第二次通过时失败。请参阅下面的可能解决方案:

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

一些注意事项:

  1. 您的代码示例中缺少一个 - 在 switch 的末尾和 else
  2. 之间
  3. 我检查 scanf 返回值 1(有效转换)- 根据 Nisse 的评论
  4. 我在 if 之后清空输入缓冲区,直到我得到一个 EOL(它在 scanf 之后保留在缓冲区中)。这确保读取的下一个字符是回车后的下一个字符,而不是回车本身。
  5. 杂项清理...

关于c 程序.数据验证。将字符转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24406720/

相关文章:

c# - 理解这个 RegEx 语句

c - 从两个文件读取输入并将其写入 C 中的另一个文件

c - 如何获取输入直到输入 "only"换行符或空格?基本上如何运行循环直到您不输入任何内容作为输入?

c - 代码9的输出如何?

c - 来自不相关 block 的指针的比较(>,> =,<,<=)

validation - JSF 2 在单击时禁用命令按钮但如果验证失败则重新启用?

Javascript 表单验证(返回问题)

r - Shiny /R : Hide error message when no value in selectizeInput

c - 一个内核模块,用于透明地绕过来自 NIC 和 TCP 应用程序的数据包。有可能完成吗?

c++ - 为什么优化没有发生?