1

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.

So Is this actually not possible in C++ or have I just not found it yet?

EDIT:

For anyone who has had this problem I'm posting my solution, because I feel like an idiot now.

#include <iostream> using namespace std; int main() { int command = 0, x = 0, y = 0, z = 0; char c; do { cin >> command; switch(command) { case 1: cin >> c >> x; cout << c << " " << x << endl; break; case 2: cin >> x >> y >> z; cout << x << " " << y << " " << z << endl; break; } } while (command); //Exits when command = 0; return 0; } 

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.

Just posting this for anyone else who may have the same problem, and not a great understanding of the way cin works.

2
  • Not sure if i have understood your needs but what about std::cin >> anInt >> aChar >> aFloat ? Input of the three variables does occur on the same line, each separated by a space. Commented Feb 28, 2011 at 23:31
  • Yeah that was the problem essentially, but the types of the values I had to get varied based on whatever the first input was, and I didn't realize that I could read those inputs from the same line without having to register the first character after I was done typing it. Essentially I found out that I could read different inputs from the first line that had already been typed, after I hit enter. Commented Mar 4, 2011 at 4:34

1 Answer 1

1

Just issue multiple calls to getchar() (or getch()) in a loop and check the input every iteration.

Something like this (untested but should work):

int loop = 1 int spacehit = 0 char last_c = 0; while(loop) { char c = getchar(); switch(c) { case ' ': spacehit = 1; printf("hit '%c' before 'space'!\n", last_c); break; case 'x': printf("hit 'x' after 'space'!\n"); spacehit = 0; break; case 27: // escape loop = 0; break; default: // do something, e.g. append the key in c to a string break; } last_c = c; } 

Edit: Added some code to print the char before hitting space in case that's what you were looking for.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.