[英]C++ not showing cout in Xcode console but runs perfectly in Terminal
Basically i am running a simple program in Xcode Version 8.3 (8E162)
基本上我在Xcode Version 8.3 (8E162)
运行一个简单程序
#include <iostream>
using namespace std;
int main() {
int a;
cout << "What is your age: ";
cin >> a;
cout << "My age is " << a << endl;
return 0;
}
I have seen different questions about cout
need to be flushed and all std::cout won't print and Xcode debugger not showing C++ cout output . 我已经看到需要清除有关cout
其他问题,并且所有std :: cout都不会打印,并且Xcode调试器不会显示C ++ cout输出 。 The Xcode debugger does not print cout
until i put \\n or endl
. 在我输入\\n or endl
之前,Xcode调试器不会打印cout
。 But, it works perfectly fine on terminal. 但是,它在终端上工作得很好。
What if i had to use What is your age:
and the user input age in the single line rather than the next line putting \\n and endl
? 如果我不得不使用What is your age:
并且用户在单行而不是下一行中输入\\n and endl
输入年龄?
This is what the Xcode debugger shows after build and run 这是Xcode调试器在构建和运行后显示的内容
This is when user inputs and it displays the result 这是当用户输入并显示结果时
This is on the terminal and this is what exactly what i need the output on the Xcode debugger. 这在终端上,这正是我需要在Xcode调试器上输出的内容。
By doing some research, there seems to be bug on cin and cout
stream on Xcode Version 8.3 Build 8E162
released on Mar 27, 2017
. 通过做一些研究Mar 27, 2017
年Mar 27, 2017
发布的Xcode Version 8.3 Build 8E162
似乎在cin and cout
流上存在错误。 Degrading to Xcode Version 8.2.1
works like a charm. 降级到Xcode Version 8.2.1
就像一个魅力。
You already solved your problem yourself: std::cout
uses buffered output and should always be flushed. 您已经自己解决了问题: std::cout
使用缓冲输出,应始终刷新。 You can achieve this by either using std::cout << "What is your age? << std::flush
, by using std::cout.flush()
or by adding a line break like std::endl
which flushes implicitly. 您可以通过使用std::cout << "What is your age? << std::flush
,通过使用std::cout.flush()
或添加类似std::endl
换行符来隐式std::cout << "What is your age? << std::flush
,可以实现此目的。
A complete solution could look like this: 完整的解决方案如下所示:
#include <iostream>
using namespace std;
int main() {
int a;
cout << "What is your age: " << flush;
cin >> a;
cout << "My age is " << a << endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.