using default values on <Enter> with no input

A

aurgathor

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 said:
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));
 
L

Larry Brasfield

aurgathor said:
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.
 
E

Evan

aurgathor said:
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)
 
R

rajkirangrandhi

aurgathor said:
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
 
A

aurgathor

Evan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top