getline function

V

vknid

Hello, I have a question. Its probably a very newbish question so please be
nice hehe. =D

I have been reading through C++ Programming Fundamentals, and have come a
crossed an example program that shows how to use the 'getline' function.

It said:
"The getline function allows you to specify how many bytes you will get from
the users input. Each character the user's types takes up one byte. So if,
in the getline function, you specify four bytes, and the user types in the
word 'computer', you will only retrieve 'comp', the first four letters."

Then gave the example:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char text[10];
cout << "Please enter a word.\n";
cin.getline(text,10);
cout << text << endl;
return 0;
}

Ok so I did that and gave it a test. I'm also going to assume that "char
text[10];" is where I specify how many bytes the user can input (which would
be 10 bytes, or 10 characters, right?). So I run the program and type in
"Dictionary". The result was that the 'Y' was cut off.

That went wrong?

Thanks.
..vK
 
J

John Harrison

Hello, I have a question. Its probably a very newbish question so please
be
nice hehe. =D

I have been reading through C++ Programming Fundamentals, and have come a
crossed an example program that shows how to use the 'getline' function.

It said:
"The getline function allows you to specify how many bytes you will get
from
the users input. Each character the user's types takes up one byte. So
if,
in the getline function, you specify four bytes, and the user types in
the
word 'computer', you will only retrieve 'comp', the first four letters."

Then gave the example:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char text[10];
cout << "Please enter a word.\n";
cin.getline(text,10);
cout << text << endl;
return 0;
}

Ok so I did that and gave it a test. I'm also going to assume that "char
text[10];" is where I specify how many bytes the user can input (which
would
be 10 bytes, or 10 characters, right?). So I run the program and type in
"Dictionary". The result was that the 'Y' was cut off.

That went wrong?

Thanks.
.vK

Nothing went wrong, the description in the book is wrong and confusing.

The size that is specified in getline is the size of the array. It is
there to make sure that getline does not attempt to write beyond the end
of the array (getline has no other way of knowing how big the array is).

Now what the book forgot to mention is that if you use a char array to
represent a string then the end of the string is indicated by a NUL char
(i.e. '\0'). So if you have a 10 character array then there is only room
for a 9 character string because one position is taken up with the NUL
char.

Of course what you should really be doing is using C++ strings, your book
should be teaching you this instead of being stuck with old fashioned char
arrays.

#include <iostream>
#include <string>

int main()
{
std::string text;
std::cout << "Please enter a word.\n";
std::getline(std::cin, text);
std::cout << text << '\n';
return 0;
}

Now there are no limits, the user can enter as many characters as they
like.

john
 
J

jmh

John said:
Hello, I have a question. Its probably a very newbish question so
please be
nice hehe. =D

I have been reading through C++ Programming Fundamentals, and have come a
crossed an example program that shows how to use the 'getline' function.

It said:
"The getline function allows you to specify how many bytes you will
get from
the users input. Each character the user's types takes up one byte.
So if,
in the getline function, you specify four bytes, and the user types
in the
word 'computer', you will only retrieve 'comp', the first four letters."

Then gave the example:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char text[10];
cout << "Please enter a word.\n";
cin.getline(text,10);
cout << text << endl;
return 0;
}

Ok so I did that and gave it a test. I'm also going to assume that "char
text[10];" is where I specify how many bytes the user can input
(which would
be 10 bytes, or 10 characters, right?). So I run the program and type in
"Dictionary". The result was that the 'Y' was cut off.

That went wrong?

Thanks.
.vK

Nothing went wrong, the description in the book is wrong and confusing.

The size that is specified in getline is the size of the array. It is
there to make sure that getline does not attempt to write beyond the
end of the array (getline has no other way of knowing how big the array
is).

Now what the book forgot to mention is that if you use a char array to
represent a string then the end of the string is indicated by a NUL
char (i.e. '\0'). So if you have a 10 character array then there is
only room for a 9 character string because one position is taken up
with the NUL char.

Of course what you should really be doing is using C++ strings, your
book should be teaching you this instead of being stuck with old
fashioned char arrays.

#include <iostream>
#include <string>

int main()
{
std::string text;
std::cout << "Please enter a word.\n";
std::getline(std::cin, text);
std::cout << text << '\n';
return 0;
}

Now there are no limits, the user can enter as many characters as they
like.

john

But wasn't part of the example to show that getline does
have a buffer size argument that can be used to limit
the size of the input gotten?

jmh
 
K

Karl Heinz Buchegger

jmh said:
[snip]
Of course what you should really be doing is using C++ strings, your
book should be teaching you this instead of being stuck with old
fashioned char arrays.

#include <iostream>
#include <string>

int main()
{
std::string text;
std::cout << "Please enter a word.\n";
std::getline(std::cin, text);
std::cout << text << '\n';
return 0;
}

Now there are no limits, the user can enter as many characters as they
like.

john

But wasn't part of the example to show that getline does
have a buffer size argument that can be used to limit
the size of the input gotten?

True.
But seriously this is of much less use in practice then it
looks in the first light. You somehow need to get rid of
possibly unread input. Note: I said 'possibly'. That
includes that you don't know if there is a '\n' character
pending or not.
This turns out to be not that easy. Just reading all
the input as string and then just use what you need
is often the easiest solution to that.
 
R

Roshan Naik

#include said:
#include <string>

int main()
{
std::string text;
std::cout << "Please enter a word.\n";
std::getline(std::cin, text);
std::cout << text << '\n';
return 0;
}

Now there are no limits, the user can enter as many characters as they
like.

john

As a general practice.... avoid reading unbounded amounts of data in one
go...this generally leads to security vulnerabilties.
Always specify a limit on how much you are willing to read.
-Roshan
 
O

Owen Jacobson

As a general practice.... avoid reading unbounded amounts of data in one
go...this generally leads to security vulnerabilties.
Always specify a limit on how much you are willing to read.

Hence the use of std::string instead of a fixed-size buffer. Arbitrarily
limiting the amount of input is marginally pointless when you have a
buffer that can size itself on the fly.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top