检查输入是否为数字或字符

时间:2019-11-07 17:13:20

标签: c++

我对C ++和编程还是很陌生。您可以说是初学者的初学者。 我需要解决一个练习,用户可以输入2个字符(0-9之间的数字/任意字母)。 用户执行此操作后,程序需要检查输入内容之一是数字还是字母。 如果是字母,则应结束程序。 如果两者都是数字,则字符应转换为整数并应加在一起。

对我来说,真正的问题是我不知道将这种“检查数字或字母”(如果可能的话没有isdigit)与类型转换结合起来的样子。

真的会喜欢一个小的“教程”

谢谢!

1 个答案:

答案 0 :(得分:0)

听起来像是您的大学作业。顺便尝试一下 附言-还有其他解决方案,例如使用isdigit isalpha

void check(const char input_char) 
{ 
     // CHECK ALPHABET
    if ((input_char >= 65 && input_char <= 90) 
        || (input_char >= 97 && input_char <= 122)) 
    { cout<<"This is an alphabet.";/* Do whatever you want */  

    // CHECK DIGITS 
    else if (input_char >= 48 && input_char <= 57) 
        { cout<<"This is a digit." /* Do whatever you want */  

    // OFC ! IT'S SPECIAL CHAR
    else
        { cout<<"This is a special char." /* Do whatever you want */  
} 

Read this for ASCII