Spaces cant be used in string? c++

半世苍凉 提交于 2019-12-10 20:46:08

问题


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.

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&)
    

    only ever reads input up to the next whitespace character. (That's just the way it was specified when Jerry Schwartz invented IO streams 25 years ago.) If you need to read whole lines, then

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

    is what you need to use:

    std::string name;
    std::getline(std::cin, name);
    
  2. 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
    }
    



回答2:


First i think sizeof needs parenthesis around the object. (See comment below)

Second cin ignores whitespaces when making an input. So "Jose " becomes "Jose". That might be the problem you're having.




回答3:


@sbi already answered your main question. But there are a couple other things you may want to pay attention to.

  1. You are holding onto pointers after they are deleted, which can cause problems later.
  2. You should use _getch() or cin.get() instead of system("PAUSE"). Using system calls to keep your command window open is not ideal.


来源:https://stackoverflow.com/questions/4992229/spaces-cant-be-used-in-string-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!