Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think it is worth starting at the beginning: although it works to have a fixed size buffer to read into, I think it is easier to use a <code>std::string</code> to capture each line. Further, you should <em>always</em> test after reading if the read operation was successful. That is, I would write an outer loop processing individual lines:</p> <pre><code>for (std::string line; std::getline(std::cin &gt;&gt; std::ws, line); ) { // process each individual line } </code></pre> <p>The use of the manipulator <code>std::ws</code> will make thing inside the loop a bit easier: it skips all whitespace, e.g., leading spaces on a line and empty lines. That is, if <code>std::getline()</code> managed to read a line, it is already known that this line isn't empty.</p> <p>Based on what you wrote about your functions, it seems they get different parameters. So I would create a string stream to process an individual line, read the first word as a command and depending on that split read further parameters:</p> <pre><code>std::istringstream sin(line); std::string command; if (sin &gt;&gt; command) { if (command == "add") { int number; std::string name; if (sin &gt;&gt; number &gt;&gt; name) { entity(number, name); } else { std::cout &lt;&lt; "ERROR: failed to read add command from '" &lt;&lt; line &lt;&lt; "'\n"; } else if (command == "???") { // ... } else { std::cout &lt;&lt; "ERROR: unrecognized command on line '" &lt;&lt; line &lt;&lt; "'\n"; } } </code></pre> <p>It would be possible to write a function which determines the parameters from a function pointer passed to it, reads those, and calls the function but this would require a few advanced techniques. Based on your question providing that answer would provide you with a solution which is probably a bit too complicated.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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