Getting input without a linebreak - c++

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?
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++;
}

Related

Default value for input in C++ [duplicate]

My app reads user input using std::cin stream.
In one place I would like to provide default input and let the user to accept it as it is (by pressing enter) or modify it before continuing (by removing old characters with backspace and adding new text).
I'm aware that characters can be placed directly into cin.rdbuf, but that's not exactly what I want to achieve.
I would like to put characters into console window in the place where console's cursor is when waiting for user input and do no read them before user will accept them. User should be also able to remove them and write their own text.
Can something like this be achieve using cin or do I have to simulate this by reading single characters and repainting content of the console?
No, something like that cannot be done with std::cin. Its read buffer is read directly from standard input. Standard input is a "cooked" character stream. All the editing is handled entirely by your operating system's terminal console, and pressing Enter results in your application's std::cin reading the entered text.
The traditional way this is done is to simply indicate the default input value in the prompt itself, and use the default value in the event of empty input, something like:
std::string buffer;
std::cout << "What color is the sky [blue]? ";
std::getline(std::cin, buffer);
if (buffer.size() == 0)
buffer="blue";

Providing default value of cin input

My app reads user input using std::cin stream.
In one place I would like to provide default input and let the user to accept it as it is (by pressing enter) or modify it before continuing (by removing old characters with backspace and adding new text).
I'm aware that characters can be placed directly into cin.rdbuf, but that's not exactly what I want to achieve.
I would like to put characters into console window in the place where console's cursor is when waiting for user input and do no read them before user will accept them. User should be also able to remove them and write their own text.
Can something like this be achieve using cin or do I have to simulate this by reading single characters and repainting content of the console?
No, something like that cannot be done with std::cin. Its read buffer is read directly from standard input. Standard input is a "cooked" character stream. All the editing is handled entirely by your operating system's terminal console, and pressing Enter results in your application's std::cin reading the entered text.
The traditional way this is done is to simply indicate the default input value in the prompt itself, and use the default value in the event of empty input, something like:
std::string buffer;
std::cout << "What color is the sky [blue]? ";
std::getline(std::cin, buffer);
if (buffer.size() == 0)
buffer="blue";

How to change newline button to space to finish reading from input stream?

I need to read a row of symbols from input stream without going to new line after reading each symbol. More precisely, I need to change hitting the "Enter" button to "Space" after reading each new symbol. Is there a way to do it without reading all row to a string and parsing string after that? Any options are acceptable, like scanf, cin.
If you can use conio.h then you can do that by reading input in a character array with getch() function. Or if you are in visual studio you can use _getch() function for the same result.
conio.h defines function named getch() and getche() which reads a character then terminates without any enter key. Both these functions have specific meanings while they do the same task. I don't use those any more so I don't remember much. It's upto you if you wanna use them or not...
Entering input seperated by Space or Enter are equivalent.
But, you do need to press Enter at the end of your space seperated input, because that is the default for stdin in C/C++.
If your program is something like:
#include <stdio.h>
int main()
{
int arr[5];
int i;
for (i = 0; i < 5; ++i)
{
scanf("%d",&arr[i]);
}
}
and you input:
1
2
3
4
5
This would be similar to:
1 2 3 4 5
But you do need to hit the Enter key at the end for the program to accept your input for five variables as a whole.
If you are talking about never pressing an Enter key,
It is impossible.
Until you are reading input from a file, of course.

C++ - Skipping code after first run-through?

I have a do-while loop, shown below
do
{
dimensions = NULL;
printf("=======================================\nDo you want to multiply in 2 dimensions, or 3?\n\n");
scanf("%c", &dimensions);
... //do stuff
printf("\nEnter r to repeat, return to terminate\n");
scanf("%c", &key);
scanf("%c", &key);
}
while(key == 'r');
On the first run, it executes fine. The problem however is when it runs through the code again after the user enters 'r' and hits return. It'll take you to the first printf("==== etc., but won't allow the user to do anything, it'll go straight back to the second printf("\nEnter...
I stepped through the code to see what was going on, and on a second run through the program just skips the scanf( and all following code for absolutely no reason. Initially I thought it was because 'dimensions' wasn't being set to a value that doesn't run the following methods - but I have, and even if that were the case, the program would run the methods instead of skipping them without user input.
Am I missing something? Is scanf( not enough to stop the program once it's already been used?
Your problem is that when your program gets input from the console with scanf, it reads the data from the keyboard into the input buffer, then values are taken out of the buffer and placed into the locations you provide to scanf. The issue is that when scanf reads a character, it also reads the \n into the buffer, then upon being called again, it reads the second character that was placed into the buffer (without asking you for more input - because why would it? It already HAS things in the buffer).
So there are two solutions: one - use fflush on stdin like so: fflush(stdin). Second - write a while loop that clears out characters one by one from the input buffer: while (getchar() != '\n' );
EDIT: For more reading, see How to clear input buffer in C?
Think it through: "the user enters 'r' and hits return", then the program reads the 'r' and repeats. What's left in the input buffer? There were two keys pressed, and the code only read the first one.
That's also the reason that the code needs two calls to scanf. The first clears the extra character out of the input buffer and the second reads the new character.
What is happening now
To make the buffer flush you need to enter
r<enter>
Hitting <enter> flushes the buffer. So the input buffer now contains two characters.
r\n
So the first scanf will read r
The second scanf will read \n
So by the time you reach the while key has a value of \n. The test fails and the loop is not repeated. So you should remove the second scanf() reading into key.
So you remove the second scanf. Now what happens?
User types
r<enter>
This leaves the input buffer with:
r\n
The scanf() read r into key. The loop repeats correctly. But when we get back to the scanf(). The input buffer still has \n character in the buffer. So the scanf() immediately reads this value and the loop exists as it should have.
how you should fix it.
Ask for Y/N answer and validate that the input is correct.
std::string line;
std::getline(std::cin, line);
while (line != "Y" && line != "N")
{
std::cout << "Hey cluts enter a correct value. Y/N\n";
std::getline(std::cin, line);
}

Possible to discard return character when using std::cin >>?

When using the >> operator in c++ to capture user input, is it possible to prevent the console from printing the newline that is generated when the user presses the return key?
You cannot prevent newline character, because when you use cin, you are communicating with system core, which is not under control by users. console will return, when you enter \n or EOF or other exception situation.
So the better way is to use getchar() to capture the '\n', and do not leave it in buffer.
It is possible to prevent this newline behavior by inputting two EOFs instead of Carriage Return from the keyboard. After entering your string at the console prompt, hit
CTRL-D, CTRL-D
Note, this is a platform specific answer. This works on my Mac, but on Windows OS the EOF sequence may be CTRL-Z, RETURN. I would appreciate an answer edit <-- HERE.
Alternately, you can ditch the >> operator and use something like std::getline and specify an exact string termination delimiter. For example:
std::string myString;
std::getline(std::cin, myString, ';');
std::cout << myString;
This will read from standard input to myString, and put the string terminating NULL character where it finds the first semicolon ';'. Then you'll only have to hit CTRL-D (input EOF) once.
You can enter the values or input by pressing space every time. But at the end you must press enter key.
Let's say: you want to enter "5,4,3,2,1"
You can do: 5 [enter] 4 [enter] 3[enter] 2[enter] 1[enter]
Also: 5[space]4[space]3[space]2[space]1[enter]
But if you want to print the output near input, you can simply print the input first and than you can print the what you want.
Example:
Input: 3 Output: input+1
So you will do:
cout<<input;
cout<<" "<<input+1<<endl;
Good luck :)