gpt4 book ai didi

c - 在 C 编程中使用 isdigit

转载 作者:行者123 更新时间:2023-11-30 16:49:32 24 4
gpt4 key购买 nike

我需要一些有关如何在此示例中使用 isdigit() 的帮助:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int usr_option;
do
{
printf("\t\t\t************************MENU***********************\n");
printf("\t\t\t*** 1. Enter Code\t\t\t\t***\n");
printf("\t\t\t*** 2. Encrypt code and verify if correct\t***\n");
printf("\t\t\t*** 3. Decrypt code\t\t\t\t***\n");
printf("\t\t\t*** 4. Display number of times code was enter\t***\n");
printf("\t\t\t***\t\t(i) Successfully\t\t***\n");
printf("\t\t\t***\t\t(i) Unsuccessfully\t\t***\n");
printf("\t\t\t*** 5. Exit Program\t\t\t\t***\n");
printf("\t\t\t***************************************************\n");
printf("\nPlease enter your option from the menu: ");
scanf("%d", &usr_option);
if (isdigit(usr_option))
{
//Inside here is my switch case e.g switch(usr_option){/*code*/}
}
else
{
printf("Need to enter a digit\n");
}
}//end do while
while(usr_option != 5);
return 0;
}

如何实现此代码以免进入无限循环?我尝试过不同的方法,但似乎不起作用。我使用编译器 CodeBlocks 和 Sublime Text 3。

最佳答案

如果您通过scanf("%d", &usr_option);读取了号码那么你不需要使用is_digit()您可以使用 ifswitch例如

switch(usr_option)
{
case 1: enter_code(); break;
case 2: encrypt(); break;

您只需要is_digit如果您要检查字符串中的字符是否为数字。

'1' != 1

关于c - 在 C 编程中使用 isdigit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42543740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com