Note that there are some explanatory texts on larger screens.

plurals
  1. POSpaces cant be used in string? c++
    primarykey
    data
    text
    <p>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.</p> <p>In a loop, I take in these parameters and create a new Person to add to an array.</p> <p>But here's my issue: If I put any spaces in the string, the loop races to the end.</p> <pre><code>Person *persons[10]; for (int i = 0; i &lt; sizeof persons;i++) { cout&lt;&lt;"Please type 1 for customer or 2 for Employee"&lt;&lt;endl; int q; cin&gt;&gt;q; string name; int salary; string complaint; if (q == 1) { cout&lt;&lt;"What is your name?"&lt;&lt;endl; cin&gt;&gt;name; cout&lt;&lt;"What is your complaint"&lt;&lt;endl; cin&gt;&gt;complaint; personPtr = new Customer(name,complaint); cout&lt;&lt;"Created customer"&lt;&lt;endl&lt;&lt;endl; persons[i] = personPtr; cout&lt;&lt; "added to array"&lt;&lt;endl&lt;&lt;endl; } else if(q==2) { cout&lt;&lt;"What is your name?"&lt;&lt;endl; cin&gt;&gt;name; cout&lt;&lt;"What is your salary"&lt;&lt;endl; cin&gt;&gt;salary; personPtr = new Employee(name,salary); persons[i] = personPtr; } else { cout&lt;&lt;"Sorry but i could not understand your input. Please try again"&lt;&lt;endl; i--; cin&gt;&gt;q; } } delete personPtr; system("PAUSE"); </code></pre> <p>Is there any special way to include strings?</p> <p>Here's the customer and employee classes for reference.</p> <pre><code>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(); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload