このコードは C++ で書かれており、よくわからない理由で 2 回書かれています。ランダムな文字を入力すると、文字が1回表示され、その下にある文字列も1回表示されると予想されます。しかし、私はこれを出力として取得しません。私は何が欠けていますか?
解決策: cin.ignore() ステートメントを追加すると、読み込まれたリターンも無視されます。私のコードを一度ループさせます。
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char letter;
letter = cin.get();
while (letter!= 'X')
{
cout << letter << endl;
cout << "this will be written twice for ununderstandable reasons";
letter = cin.get();
}
}
例: cmd scrnc
で書くと、c
back + フレーズの 2 倍になりますthis will be written twice for ununderstandable reasons
。だから私がアウトプットだと思ったのは
c
this will be written twice for ununderstandable reasons
実際には
c
this will be written twice for ununderstandable reasons
this will be written twice for ununderstandable reasons