- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在his answer ,特别是在 linked Ideone example ,@Nawaz 展示了如何更改 cout
的缓冲区对象以写入其他内容。这让我想到利用它来准备来自 cin
的输入,方法是填充它的 streambuf
:
#include <iostream>
#include <sstream>
using namespace std;
int main(){
streambuf *coutbuf = cout.rdbuf(cin.rdbuf());
cout << "this goes to the input stream" << endl;
string s;
cin >> s;
cout.rdbuf(coutbuf);
cout << "after cour.rdbuf : " << s;
return 0;
}
但这并不像预期的那样有效,或者换句话说,它失败了。 :| cin
仍然需要用户输入,而不是从提供的 streambuf
中读取。有什么办法可以做到这一点吗?
最佳答案
#include <iostream>
#include <sstream>
int main()
{
std::stringstream s("32 7.4");
std::cin.rdbuf(s.rdbuf());
int i;
double d;
if (std::cin >> i >> d)
std::cout << i << ' ' << d << '\n';
}
关于c++ - 是否可以从 cin 输入 "prepare"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6013638/
我使用 cin.peek() 方法获得了这段代码。我注意到奇怪的行为,当程序输入看起来像 qwertyu$[Enter] 时,一切正常,但是当它看起来像 qwerty[Enter]$ 时,它仅在我键入
#include #include using namespace std; int main(){ ofstream fout("student.dat",ios::out); char name[
我是 C++ 编程新手,遇到了障碍。这是我的代码: #include using namespace std; int main(){ int sum = 0, a; cout >
我有一个简单的程序,当输入完全符合预期时,它名义上可以工作。 #include using namespace std; int main () { int a, b; char a
int main () { int num1, num2; int cnt = 1; if (cin >> num1){ while (cin >> num2){
我有这个程序,但 cin 随机跳过..我的意思是有时它会,有时它不会。有什么解决办法吗? int main(){ /** get course name, number of
我是一名正在准备期末考试的 C++ 初学者。我用两种方式写了一个程序。第一个代码使用 cin.getline() 并且不能正常工作。第二个代码使用 cin.get() 和 cin >> 并正确执行所有
据我所知,在 cin 之后使用 getline() 时,我们需要先刷新缓冲区中的换行符,然后再调用 getline(),我们通过调用 cin.ignore() 来完成此操作。 std::string
我正在尝试以下代码: int main() { char str1[20]; int a; cout > a; cout > 忽略空格(即 ' ', '\t', '\n
我刚开始阅读 C++ 11 入门书,目前在 if 语句部分。我以前有使用 Java 的经验,但 C++ 输入/输出确实让我感到困惑。为了方便起见,我将多个练习放在一个文件中。但是,似乎自从我调用了 w
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: c++ cin input not working? 我一直在尝试使用以下 C++ 代码在整数后输入字符串。
需要 I/O 重定向,我不能使用 fstream。我用 I/O通过使用 ">line)通过 I/O 读取文件重定向。然后我需要 cin>>x ,但这次是在运行时进行用户输入,但会被跳过。 我试过了 c
我正在尝试使用 getline() 但在输入第一个记录光标后不要等待第二个国家/地区名称,它会跳过并跳转到首都名称。我的错误在哪里。如果我输入 国家:印度 首都:德里 Capita:57382 它可以
我有以下循环。它应该读取数字直到 EndOfFile,或者用户输入 -999 int arr[100]; int index; for (index = 0; index > arr[index];
以下两个循环和每个循环什么时候停止有什么区别? #include #include #include using namespace std; int main() { int x,y;
在这个程序的每次输入中,我希望用户能够输入他们想要的任意数量的“单词”(意思是用空格/制表符分隔的文本)......但是不允许用户输入单词超出当前 cin。例如:如果我键入 John Smith Do
int main() { int oddeven = 1; int modulotwo; // Simple loop since no reason to check 0
我写了一个 switch 语句,并创建了一个默认值,它会简单地说用户选择了一个错误的选项并重复输入。我想确保如果出现问题,它会首先清除缓冲区,所以我使用了 cin.sync(),但输入 'a' 仍然会
我是一个新手程序员,正在练习下面的这个程序。无论出于何种原因,在我的程序结束时,cin.ignore() 被编译器完全跳过并直接移动到 cin.get(),但是之前已经有一个按键,所以编译器完全跳过它
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我是一名优秀的程序员,十分优秀!