Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you use <code>&gt;&gt;</code> to read input, it will leave unread characters in the stream (those, that couldn't be converted to integer, at least the return character you type to enter input), which the following <code>getline</code> will consume thinking it has already read an (empty) line.</p> <pre><code>#include &lt;limits&gt; //... cin &gt;&gt; pz-&gt;diameter; cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); cout &lt;&lt; "Enter the company name: "; // use getline because the company name can have spaces in it. getline(cin, pz-&gt;szCompany); </code></pre> <p>Your problem has nothing to do with the struct or pointers, just the normal behavior of input streams.</p> <p>You may also have to deal with incorrect input. For example, entering a non-number where a number is expected would put the stream in an error state, so that all subsequent attempts to read would fail unless you deal with it. Better take Neil's advice, but for getting input from the user, it might also make sense to use a function for formatted input that prompts you until you get valid input:</p> <pre><code>template &lt;class T&gt; T input(const std::string&amp; prompt, const std::string&amp; error_prompt) { std::cout &lt;&lt; prompt; T result; while (!(std::cin &gt;&gt; result)) { std::cin.clear(); std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); std::cout &lt;&lt; error_prompt; } std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); return result; } //... pz-&gt;weight = input&lt;float&gt;("Enter the weight: ", "Please enter a numeric value: "); </code></pre>
 

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