开发者

Getting input without a linebreak

I'm writing a program which will be doing manipulation of matrices. I want the user to be able to enter data into a matrix by typing it in one row at a time. So it will first ask for the value in row: 1, column: 1. The user will type in the appropriate value, and then press enter, after which he will type in the value for row: 1, column: 2.

This is the trick: I want the console to not enter a new line when the user presses enter. Instead, I want it to simply insert a tab character. Is this possible?

开发者_StackOverflow社区

Thanks so much.


Yes, it's possible. You'll need to use a console/terminal library, though. Ncurses for *nix, wincon (part of the Windows API; you can just #include windows.h to use it)... There are a lot of choices out there.

The actual algorithm will simply be checking the characters that are sent as key events/using the getkey() equivalents of the various libraries, outputting the inputted characters to the console if the key pressed is not ENTER but would still cause a character to be echoed to the screen (i.e. function keys, caps lock, shift, etc. wouldn't cause any echoing to the console or terminal window) and then outputting \t if the key pressed is indeed ENTER.


Set the cursor position back up to the previous line. In Windows, you can use SetConsoleCursorPosition().


It's not exactly what you wanted, but you could get the same effect by using getline to obtain the row input all on one line, and then use std::stringstream to parse out the values.

 std::string row;
 getline(cin,row);
 std::stringstream ss(row);
 int j=0,i=currentrow;  //put this in a loop over your rows
 int input; //or float, double, whatever
 while(ss >> input)
 {
      mat[i][j] = input;
      j++;
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜