Hi!
Just wondering, how to turn a normal "Enter" response to a default entry?
i.e.
Thanks.Code:float number1;
cin Input number: ;
if (number1 = (blank entry or "Enter"){
number1 = 0.02;
}
Printable View
Hi!
Just wondering, how to turn a normal "Enter" response to a default entry?
i.e.
Thanks.Code:float number1;
cin Input number: ;
if (number1 = (blank entry or "Enter"){
number1 = 0.02;
}
Use std::getline to read a line of input instead of reading the integer directly. If the line is empty, use a default value, else parse the integer from the line.
>Just wondering, how to turn a normal "Enter" response to a default entry?
First read the input into a string first. At that point, there are several different ways to proceed. Here is one method:
Code:std::string snum;
double num;
std::cout << "Enter a number (or press <ENTER> for default):";
std::getline(cin, snum);
std::istringstream converter(snum);
converter >> num;
if (!converter)
{
if (converter.eof())
{
std::cout << "No value was entered. A default value will used." << endl;
num = .02;
}
}
std::cout << num << std::endl;