开发者

Spaces cant be used in string? c++

开发者 https://www.devze.com 2023-02-10 14:41 出处:网络
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.

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<<"So开发者_如何学Crry 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. 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
    }
    


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.


@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.
0

精彩评论

暂无评论...
验证码 换一张
取 消