Nic*_*lus 1 c++ input console-input
我知道这是一个奇怪的问题,但是有没有办法从控制台读取以前的输入?
就像是:
The fox is brown // line 1
The duck is yellow // line 2
Here where the control is right now_ // but I want to read line 2
Run Code Online (Sandbox Code Playgroud)
PS:我正在使用窗户
如果通过阅读先前的输入,您的意思是在 C++ 程序中,那么答案是肯定的。标准输入是一个流,它维护一个读缓冲区。
快速而肮脏地展开流并读取同一行两次
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter a line of text and you will see it echo twice\n";
string mystring;
getline(cin, mystring);
cout << mystring;
cout << "\n";
// reverse the input stream by the lengtht of the read string (+1 for the newline)
for (int i = 0; i <= mystring.length(); i++)
{
cin.unget();
}
string mystring2;
getline(cin, mystring2);
cout << mystring2;
cout << '\n';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2252 次 |
| 最近记录: |