这段代码是用 C++ 编写的,由于我不太明白的原因,它被编写了两次。我希望在输入一个随机字符后,它会显示一次字符,而字符串也会显示一次。但我没有把它作为输出。我错过了什么?
解决方案:添加 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 scrn 写c
,我会得到一个c
back + 两次短语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