+ 2
Dynamic entries by user as user input
Hi We can take dynamic length input using cin into while loop... Once user provide 0 or negative input, one can break loop. This imposes user to remember break condition and need to provide extra user input to break the same... Another option is to take input seperated by space and store it into string variable... Post this , we can split the same and get all user entries into array or vector. here limitation is that user has to provide values seperated by space. Now , what can be done when user input must be separated by new line and number of input is not fixed along with user adamant not to provide exit condition of do while loop.. Any thoughts would be of great help...!
3 odpowiedzi
+ 1
It would be interesting to know what you are making.
But lets say its a shopping list and each line the user inputs is an item..
C:\Users\Ketan> groceries.exe
Please enter your groceries:
Milk
Eggs
Bread
█
How do I, as a user, expect to stop inputting things after I'm done?
I think would prefer a blank line, so pressing Enter twice == stop.
I mean you *need* an "exit" command or similar. Maybe you could capture the ESC key or something but I'm not sure if that is cross-platform.
The way I see it there are only 2 ways of doing things:
1) Read until EOF, blank line, exit word, ESC key, etc
1b) Ask "do you want to input more? [y/n]" after every line
2) make the user input the number of lines beforehand
By the way we see those same solutions in C++ itself: strings are \0-terminated so that is like an array with an "exit word". std::vectors have the length stored in a header so that's like an array where we input the length first.
What "traditional" apps are you thinking about? What are those doing?
+ 1
Your only other option is to read until EOF (end of file), that is, until cin is closed and there is no more input. The user can close cin by pressing Ctrl+Z followed by Enter in Windows' command prompt, and Ctrl+D in the Linux terminals.
std::string line;
while (std::getline(std::cin, line)) {
...
}
// EOF reached
That's a fine solution if your program usually doesn't run interactively with a user who types things. For example a program that usually processes files:
C:\Users\Ketan> myprogram.exe < myfile.txt
(here cin closes when the file is read)
If your program is used by a human then having an exit condition like a blank line or something is probably a better choice. (Having to memorize a keyboard shortcut to close cin is just a really bad exit condition. Closing cin also means you can never enter anything ever again.)
Alternatively you could also make the user input how many lines he wants to input, like so:
3
Foo
Bar
Baz
0
Schindlabua thanks for this solution.. actually file read was something i missed and i myself also got convinced with this option... It doesn't ask for any extra input.users just have to enter values into text file rather than on console... But unfortunately , user manager is not opting to go for this option... Manager is insisting on the input on console like the traditional way other applications are working.... On top of it, no additional input like number of input or ctrl + z + enter is accepted...
I know I am stretching this but it is what end user's expectation is... Does it like no other mean to provide the functionality users are expecting for ? Any thoughts ?