473,385 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

using default values on <Enter> with no input

I have a code that reads in 2 shorts, and checks if they are
in a given range. I'd like to modify it to use a predefined
default value if nothing, but as newline is entered. What
would be the best way to do that?

TIA

current code:

do {
cout << " Rows [" << MIN_ROWS << ".." << MAX_ROWS << "](" << DEF_ROWS <<
"): ";
cin >> rows;
cout << "Colums [" << MIN_COLS << ".." << MAX_COLS << "](" << DEF_COLS
<< "): ";
cin >> cols;
if ((rows < MIN_ROWS) || (rows > MAX_ROWS) || (cols < MIN_COLS) || (cols
MAX_COLS)) {

cout << "Values for rows and colums must be in the indicated range
\n";
cout << "Please re-enter, or ^C to abort: \n\n";
}
} while ((rows < MIN_ROWS) || (rows > MAX_ROWS) || (cols < MIN_COLS) ||
(cols > MAX_COLS));

Jul 23 '05 #1
4 3831
"aurgathor" <sp********@you.com> wrote in message
news:1111090791.af4f9aa082b3c592d1e6365b7558e11f@t eranews...
I have a code that reads in 2 shorts, and checks if they are
in a given range. I'd like to modify it to use a predefined
default value if nothing, but as newline is entered. What
would be the best way to do that?

With rare exceptions, the best way to handle any
input directly created by humans is to collect it
in some grouping that is likely to match what they
expect (newline bounded, textbox content, etc.),
then parse and convert that into internal values,
using code that implements your defaults. I am
fond of just giving the variables their default
values explicitly before the input processing, then
assigning only the ones that were specified.

I leave the coding of this approach to you.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #2

aurgathor wrote:
I have a code that reads in 2 shorts, and checks if they are
in a given range. I'd like to modify it to use a predefined
default value if nothing, but as newline is entered. What
would be the best way to do that?


If I understand your problem correctly, it's that you can't figure out
how to get cin to work if there isn't any actual input. (Like in your
code, if you hit enter at one of the prompts, it will just sit there
and continue to wait for more input.

If so, I think you want to look at getline. That's the only function I
can think of that will treat newlines in the way that you want. Other
input methods, such as cin, look for "real" input and ignore
whitespace. (You could probably feed a parameter to get and maybe a
couple other functions to, but I don't know off the top of my head. I'd
use getline.)

Put the input into a string object and either parse it manually or you
can make a stringstream from it and use that as you would cin. If you
want example code, let me know.

This has the benefit of giving you more control over input too. (Just
be aware that if you have a getline following a cin, the cin will leave
the newline in the input stream and so getline will return an empty
string, so you'll want to use either two getlines in a row or else a
cin.ignore)

Jul 23 '05 #3
aurgathor wrote:
I have a code that reads in 2 shorts, and checks if they are
in a given range. I'd like to modify it to use a predefined
default value if nothing, but as newline is entered. What
would be the best way to do that?

TIA


Here is what I use to do a similar thing:
template <class T>
void read(T &val, const std::string &prompt, const T &def_val)
// Displays a prompt for input, and assigns a default value if
nothing is entered.
{
std::string inputLine;

std::cerr << prompt << "[" << def_val << "] ";

getline(std::cin,inputLine);
if(inputLine.length() > 0)
{
std::istringstream input(inputLine.c_str());
if(! (input >> val)) val = def_val;
}
else val = def_val;
}

HTH
Rajkiran

Jul 23 '05 #4

"Evan" <ev****@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
If I understand your problem correctly, it's that you can't figure out
how to get cin to work if there isn't any actual input. (Like in your
code, if you hit enter at one of the prompts, it will just sit there
and continue to wait for more input.
Yes indeed. That I could figure out in very little time. ;-)
If so, I think you want to look at getline. That's the only function I
can think of that will treat newlines in the way that you want. Other
input methods, such as cin, look for "real" input and ignore
whitespace.


Thanks for the tip, I'll try that.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Denis Hierstein | last post by:
I need a function, witch make a break in a for-loop and wait for the <enter>-key ... when I use Pascal I just use the Read; or the ReadLn;-function, then the loop stop as long as the user push the...
0
by: aurgathor | last post by:
I have a code that reads in 2 shorts, and checks if they are in a given range. I'd like to modify it to use a predefined default value if nothing, but a newline is entered. What would be the...
7
by: jerrygarciuh | last post by:
Hello, I have been playing with various Googled solutions for capturing the <Enter> key to suppress form submission. My first question is whether anyone has a script that works in all common...
7
by: Susan Bricker | last post by:
I know that I saw some information concerning the <shift>+<enter> combination use to bypass launching an Access mdb application and enter the Access design workspace. Would someone please direct...
6
by: tor | last post by:
Hello How can I use an other key then TAB to move from one textBox to another?? Torfinn
2
by: JP | last post by:
Hi, It's a login screen, so users enter their ID/Password and hit <ENTER> instead of clicking on Login button. But when they hit <ENTER> an Image Button gets clicked. I want to make my Login...
4
by: Paul W | last post by:
On my simple login-screen I have a 'username' and 'password' field and an imagebutton for the 'OK'. This used to work nicely so that when the user hit <Enter> from the password field, it invoked...
4
by: peshrad | last post by:
Hi ! I'm working with Win 2K and Visual Studio 2003. I have a problem because pressing <ENTER> in a text input control causes a postback of my web form. Here comes some example code (already...
4
by: almurph | last post by:
Hi everyone, I'm a newbie to javascript. I have written some code to detect when a user presses the down-arrow, up-arrow and enter button. Essentially the user can arrow down or up through a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.