c++ 讀入一個字元
阿新 • • 發佈:2018-11-08
#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;
}