[英]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.