简体   繁体   中英

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.

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM