字符串不能使用空格? c ++ [英] Spaces cant be used in string? c++

查看:281
本文介绍了字符串不能使用空格? c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我正在试验多态性。我有2个对象,一个客户和一个雇员。客户有名称和投诉。

Basically I'm experimenting with polymorphism. I have 2 objects, a customer and an employee. a customer has a name and a complaint. An employee has a name and a salary.

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

In a loop, I take in these parameters and create a new Person to add to an array.

但是这里是我的问题:如果我在字符串中放入任何空格,循环到最后。

But here's my issue: If I put any spaces in the string, the loop races to the end.

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] = personPtr;
    }
    else
    {
        cout<<"Sorry but i could not understand your input. Please try again"<<endl;
        i--;
        cin>>q;
    }
}
delete personPtr;
system("PAUSE");

是否有任何特殊方式来包含字符串?

Is there any special way to include strings?

以下是客户和员工类别供参考。

Here's the customer and employee classes for reference.

class Person
{
public:
Person(const string n)
{name = n;}; // initialise the name
virtual void printName();
protected:
string name;
};


class Customer:public Person
{
public:
string complaint;

Customer(string name, string cm)
    :Person(name)
{       
    complaint=cm;
}
virtual void printName();
};

class Employee:public Person
{
public:
int salary;

Employee(string name,int sl)
    :Person(name)
{       
    salary = sl;
}
virtual void printName();
};


推荐答案


  1. 输入运算符

  1. The input operator

std::istream& operator>>(std::istream& is, std::string&)



读取输入,直到下一个空格字符 。 (这只是当Jerry Schwartz在25年前发明IO流时指定的方式。)如果您需要读取整个行,那么

std::istream& getline(std::istream&, std::string&, char='\n')

是你需要使用:

std::string name;
std::getline(std::cin, name);


  • 输入可能失败 。例如,读取 int 可能会失败,因为在输入缓冲区中只有非数字数字。如果流操作失败,则有将在流中设置的状态位。在故障之后,流不会执行任何进一步的操作。 >> 的操作数将保持不变。

    因此,您需要 检查输入操作是否成功 ,然后再使用数据。最简单的方法是在输入操作之后检查流:

  • Input might fail. For example, reading an int might fail because there are only non-numeric digits in the input buffer. If stream operations fail, there are state bits that will get set in the stream. After a failure, a stream will not perform any further operations. Operands of >> will then be left untouched.
    So you need to check whether your input operations succeeded before using the data. The simplest way to do this is to check the stream after input operations:

    if(!std::cin) {
      // input failed
    }
    


  • 这篇关于字符串不能使用空格? c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆