Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: Get char after space character instead or return carriage
    primarykey
    data
    text
    <p>Okay this is similar to my last question but what I ended up doing was way too complex for something as simple as this. I simply need to get a single character or number (I will know which of these I am receiving) from the console after I press space, instead of pressing enter. I'm sure there must be a way to have the terminal read input after a space instead of a '\n'. I need to read inputs from the console in which the succeeding data types will vary depending on what the first input is, and I think reading the entire line, parsing it into strings, then parsing some of those into ints is a bit unnecessary.</p> <p>So Is this actually not possible in C++ or have I just not found it yet?</p> <p>EDIT:</p> <p>For anyone who has had this problem I'm posting my solution, because I feel like an idiot now.</p> <pre><code>#include &lt;iostream&gt; using namespace std; int main() { int command = 0, x = 0, y = 0, z = 0; char c; do { cin &gt;&gt; command; switch(command) { case 1: cin &gt;&gt; c &gt;&gt; x; cout &lt;&lt; c &lt;&lt; " " &lt;&lt; x &lt;&lt; endl; break; case 2: cin &gt;&gt; x &gt;&gt; y &gt;&gt; z; cout &lt;&lt; x &lt;&lt; " " &lt;&lt; y &lt;&lt; " " &lt;&lt; z &lt;&lt; endl; break; } } while (command); //Exits when command = 0; return 0; } </code></pre> <p>The following cin's inside the switch statement will read from the same buffer as the first cin, so there is no need to read what the command is beforehand anyway. As you can see this works fine for different types and amounts of inputs after the first cin, so there's no need to use any other solution.</p> <p>Just posting this for anyone else who may have the same problem, and not a great understanding of the way cin works.</p>
    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.
    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