相关疑难解决方法(0)

空格不能用在字符串中?C++

基本上我正在尝试多态性.我有2个对象,一个客户和一个员工.客户有姓名和投诉.员工有姓名和薪水.

在循环中,我接受这些参数并创建一个新的Person来添加到数组中.

但这是我的问题:如果我在字符串中放置任何空格,那么循环就会结束.

Person *persons[10];

for (int i = 0; i < sizeof persons;i++)
{
    cout<<"Please type 1 for customer or 2 for Employee"<<endl;
    int q;
    cin>>q;
    string name;
    int salary;
    string complaint;


    if (q == 1)
    {
        cout<<"What is your name?"<<endl;
        cin>>name;
        cout<<"What is your complaint"<<endl;
        cin>>complaint;

        personPtr = new Customer(name,complaint);
        cout<<"Created customer"<<endl<<endl;
        persons[i] = personPtr;
        cout<< "added to array"<<endl<<endl;
    }
    else if(q==2)
    {
        cout<<"What is your name?"<<endl;
        cin>>name;
        cout<<"What is your salary"<<endl;
        cin>>salary;
        personPtr = new Employee(name,salary);
        persons[i] …
Run Code Online (Sandbox Code Playgroud)

c++ string cin

3
推荐指数
1
解决办法
1756
查看次数

标签 统计

c++ ×1

cin ×1

string ×1