当我在单词之间给出空格时,在运行时获得错误

时间:2013-09-16 04:52:17

标签: c++

我在运行时在数组中存储单词但是当我在单词之间给出空格时程序不要求第二次输入它直接给我一个输出而不需要第二次输入就是我的编码。

#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();
}

here is my output

1 个答案:

答案 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