【发布时间】:2016-12-20 18:18:18
【问题描述】:
我的代码有问题,在我编译并运行代码后出现,它会输出两次并且输出错误。
我的任务是重复输入数字/字母/大写并相应地输出结果。至于while循环,我相信我必须把while(1)。
#include <stdio.h>
int main()
{
char c;
int num;
printf("\nEnter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z'))
printf("%c\nIt is an alphabet.", c);
if ((c >= 'A' && c <= 'Z'))
printf("%c\n It is a capital alphabet.", c);
if (c <= '1' ||c >= '1')
printf("\nIt is a numeric");
else
printf("error");
return 0;
}
【问题讨论】:
-
应该有一个
else if链...但它不存在... -
if (c <= '1' ||c >= '1')是错误的。 – 您是否尝试调试代码? -
请确保您确切知道您正在学习什么语言。您的代码类似于 C。C 和 C++ 是非常不同的语言。
-
你真的不应该写这样的比较来确定一个角色的类别。您有
std::islower、std::isupper和std::isdigit用于此目的。例如,如果您的系统是 EBCDIC,那么您对小写的比较将失败。让标准函数确定角色类别——它知道您可能不知道的所有这些细节。
标签: c++ c while-loop scanf