user3081016
user3081016

Reputation: 25

User input without newline?

Basically I need user input for an integer, I tried these methods:

#include <iostream>
int main() {
 int PID;
 scanf("%i",&PID); //attempt 0
 cin >> PID; //attempt 1
}

any ideas?

Upvotes: 1

Views: 899

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595557

Most command-line input methods require the user to press ENTER to signal the end of the input, even if the app does not use the line break.

If you do not want the user to end the input with ENTER then you will likely have to resort to reading the input 1 character at a time and handle data conversions manually. The problem is, without a line break, how do you know when the user has finished typing the input? When the user has typed X number of characters/digits? What if the user types fewer then your max? Start a timer and stop reading when the user stops typing for X seconds? See the problem?

Line breaks are not something to be ignored. You should re-design your input logic to accept them, not avoid them. If that is really not an option, then maybe you need to re-think why you are creating a command-line app in the first place instead of a GUI app where the user can press a button when ready.

Upvotes: 3

Related Questions