我在运行时在数组中存储单词但是当我在单词之间给出空格时程序不要求第二次输入它直接给我一个输出而不需要第二次输入就是我的编码。
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
char a[50];
char b[50];
cout<<"please tell us what is your language\t";
cin>>a;
cout<<"please tell us what is your language\t";
cin>>b;
cout<<a<<b;
getch();
}
答案 0 :(得分:3)
#include<iostream>
//#include<conio.h> // better don't use this, it's not portable
#include <string>
//using namespace std; // moving this inside the function
int main(){
using namespace std; // a bit more appropriate here
string a;
string b;
cout<<"please tell us what is your language\t";
getline(cin, a); // `a` will automatically grow to fit the input
cout<<"please tell us what is your language\t";
getline(cin, b);
cout<<a<<b;
//getch(); // not portable, from conio.h
// alternative to getch:
cin.ignore();
}
std::getline
的引用(底部有一个示例)和std::string
。