![](/img/trans.png)
[英]C/C++ : Can I keep the cursor in the current line after pressing ENTER?
[英]How can i Keep “Enter” in the current line when receiving information from the user in the console by c++?
int Plus(){
int a = 0, b = 0;
cin >> a;
cout << "\b + ";
cin >> b;
cout<<" = "<<a + b;
return a+b;}
我希望将结果像这样在一行上打印在控制台上:1 + 2 = 3
不要这样使用return! 我会为您建议一个简单的解决方案。 三个变量。 只需添加一个c
变量即可保存结果。 和:
cout << a <<"+" <<b << "=" << c <<endl;
//我看到您已将函数命名为“ Plus”,因此我相信您仅以这种方式使用它。 希望有帮助!
好吧,如果您使用cin,则不能在同一行中保留“ Enter”。
更好的方法是使用
#include<conio.h>
和_getchar()
这个问题已经在这里得到回答:
不过,如果您想继续使用cin,请尝试以下操作:
cin>>a>>b;
cout<<a<<" + "<<b<<" = "<<a+b;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.