From: Jochen Rundholz on
Hi,

I want to ask for several values and enter should go for the default. I
tried the following:

int a = 0;
cout << "Enter a value, hit enter for default [0]: ";
cin >> a;

It doesn't work... any idea what I can do?

Regards,
Jochen
From: Karl Heinz Buchegger on
Jochen Rundholz wrote:
>
> Hi,
>
> I want to ask for several values and enter should go for the default. I
> tried the following:
>
> int a = 0;
> cout << "Enter a value, hit enter for default [0]: ";
> cin >> a;
>
> It doesn't work... any idea what I can do?

Yep. Don't read as int, read the user input as string.
If the user enters an empty string, you know that you have
to apply the default value. If the user did not enter an empty
string, convert that string to a number.

Hint: Doing user input like you did above is usually good enough
for toy programs. For industrial programming you almost always do
user input in the way of collecting user input in a string variable.
This way, nothing from the I/O system interferes with the input and you
get your hands on whatever the user entered. Once you have that input
in a string variable, you continue with validity checks and converting
the input to the target format (eg. an int, double, date, etc). As a second
bonus, error handling often gets much easier if you don't have to constantly
clear errors flags in the I/O subsystem and need to get rid of unprocessed
input.

--
Karl Heinz Buchegger
kbuchegg(a)gascad.at
From: Jochen Rundholz on
Karl Heinz Buchegger wrote:

> Jochen Rundholz wrote:
>>
>> Hi,
>>
>> I want to ask for several values and enter should go for the default. I
>> tried the following:
>>
>> int a = 0;
>> cout << "Enter a value, hit enter for default [0]: ";
>> cin >> a;
>>
>> It doesn't work... any idea what I can do?
>
> Yep. Don't read as int, read the user input as string.
> If the user enters an empty string, you know that you have
> to apply the default value. If the user did not enter an empty
> string, convert that string to a number.
>

Okay, I'll do it that way. This way I won't have a backspace problem either.
Google told me two different versions to convert an integer to int:

int a;
string s="5";
a = atoi(s) // doesnt work
a = atoi(s.c_str()); //this works

Is the first call just something old and outdated? Why doesn't it work? I
thought the s.c_str() converts a string to a char pointer and furthermore
I've read in some documentation that atoi() needs a string as an argument.
I am puzzeled...

Regards,
Jochen

From: Karl Heinz Buchegger on
Jochen Rundholz wrote:
>
> Okay, I'll do it that way. This way I won't have a backspace problem either.
> Google told me two different versions to convert an integer to int:
>
> int a;
> string s="5";
> a = atoi(s) // doesnt work
> a = atoi(s.c_str()); //this works
>
> Is the first call just something old and outdated? Why doesn't it work?

because aoti expects it parameter to be a pointer to characters. s is not
a pointer to characters, thus the compiler barfs.

> I
> thought the s.c_str() converts a string to a char pointer and furthermore

Yes. it does (somewhat). c_str() returns a pointer to characters. Exactly what
atoi expects.

> I've read in some documentation that atoi() needs a string as an argument.
> I am puzzeled...

I am with you. The problem is that with strings there are 2 possible interpretations.
* The old C-style string. Basically a sequence of characters, terminated with a '\0'
* The C++ std::string class.

From the context it is usually clear what is ment. If not, look up the documentation.
If a call expects a 'const char*' or something like that, then it wants you to pass
a pointer to an old C-style string (you know: character sequence, terminated with '\0').

Oh, before I forget: Don't use atoi(). It has a serious failure. There is no way to
differntiate between a conversion that didn't work and a conversion that correctly yields
the resut 0. atoi( "abc" ) *will* return 0!

Use strtol() instead (or use a string stream).

--
Karl Heinz Buchegger
kbuchegg(a)gascad.at
From: osmium on
"Jochen Rundholz" writes:

> Okay, I'll do it that way. This way I won't have a backspace problem
> either.
> Google told me two different versions to convert an integer to int:
>
> int a;
> string s="5";
> a = atoi(s) // doesnt work
> a = atoi(s.c_str()); //this works
>
> Is the first call just something old and outdated? Why doesn't it work? I
> thought the s.c_str() converts a string to a char pointer and furthermore
> I've read in some documentation that atoi() needs a string as an argument.
> I am puzzeled...

Sadly, the word "string" has not one, but two, profoundly different,
meanings in the C/C++ community. atoi is natively a C function, but s in
your program is a C++ string. There should be a list of booby traps
somewhere but I haven't seen the list.