c++ 读入一个字符

#include <iostream>

using namespace std;

int main()
{
   char c;
   cin>>c; //自动过滤掉不可见字符(如空格 回车 tab等),这些字符被当作间隔符号,输入不可见字符不识别
   cout <<c<<"  "<< c %2<< endl;
    return 0;
}

cin读入一个字符时,大多数字符是可以读入的,但是它会自动过滤掉不可见字符(如空格 回车 tab等)。openjudge系统检查提交的程序时,会多次运行程序,输入各种可能的情况进行检验,普通字符和不可见字符都可能被输入,所以cin>>输入不可见字符时就会有问题。openjudge的检验结果WA也不一定全错,可能得了几分,但不是满分,所以不是AC。

可以改成:

char c;

c=getchar();

#include <iostream>

using namespace std;

int main()
{
   char c;
   c=getchar();
   cout <<c<<"  "<< c %2<< endl;
    return 0;
}

用cin.get()更c++ style

#include <iostream>
using namespace std;
int main()
{
   char c;
   c=cin.get();
   cout <<c<<"  "<<int(c)<<"  "<< c %2<< endl;
   return 0;
}

猜你喜欢

转载自blog.csdn.net/juddi/article/details/83056213