2

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

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

这是我的输出

4

1 回答 1

4
#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.

于 2013-09-16T05:06:08.873 回答