请帮助我解决为什么C ++忽略for(带有字符串)的第一个CIN?

时间:2019-03-14 17:59:26

标签: c++ c++11

我正在尝试学习C ++字符串,但遇到了一些麻烦。 当我运行该程序时,它会问我要添加多少人,而当我尝试添加他们时,它会忽略:

cout<<"Enter your name: ";
getline (cin, name[n]);

并直接转到getline (cin, lastname[n]);

#include<iostream>
#include<string>
using namespace std;

int main(){
    string name[50], lastname[50], city[50];
    int tot=0, n=0;
    cin>>tot;
    for (int n=0; n<tot; n++){
        cout<<"Enter your name: ";
        getline (cin, name[n]);
        cout<<"Enter your lasname: ";
        getline (cin, lastname[n]);
        cout<<"Enter your city: ";
        getline (cin, city[n]);
    }
    for (int n=0; n<tot; n++){
        cout<<name[n]<<" "<<lastname[n]<<" from "<<city[n]<<endl;
    }
    cout<<endl;
    system("pause");
    return 0;
}

非常感谢:)

0 个答案:

没有答案