2017-03-01 65 views
0

我需要关于如何在这个例子中使用isdigit()一些帮助:使用ISDIGIT C编程

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

我怎样才能实现这个代码不进入无限循环? 我试过不同的方式,但它似乎不想工作。我使用的编译器代码块和崇高的文本3

回答

0

如果已通过scanf("%d", &usr_option);读取号码,然后你不需要使用is_digit()你可以只使用ifswitch例如

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

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

'1' != 1

+1

你还是应该检查scanf'的'的返回值。如果它与'1'不同,'usr_option'包含随机数据。 – IInspectable

+0

感谢您的信息 – Josephy22