473,586 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getline function

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(tex t,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

Jul 22 '05 #1
5 7743
On Wed, 28 Jul 2004 05:26:19 GMT, vknid <vk***@hotmail. com> wrote:
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(tex t,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(st d::cin, text);
std::cout << text << '\n';
return 0;
}

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

john
Jul 22 '05 #2
jmh
John Harrison wrote:
On Wed, 28 Jul 2004 05:26:19 GMT, vknid <vk***@hotmail. com> wrote:
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(tex t,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(st d::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

Jul 22 '05 #3
jmh wrote:

[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(st d::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.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #4
> #include <iostream>
#include <string>

int main()
{
std::string text;
std::cout << "Please enter a word.\n";
std::getline(st d::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

Jul 22 '05 #5
On Fri, 30 Jul 2004 21:42:27 +0000, Roshan Naik wrote:
#include <iostream>
#include <string>

int main()
{
std::string text;
std::cout << "Please enter a word.\n";
std::getline(st d::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.


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.

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #6

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

Similar topics

2
3547
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline function at first and then I use the cin>> function then the next time cin.getline function does not work.
1
2140
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I could state getline's first parameter supports "FROM". The second parameter supports "TO". // later
10
5599
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert parameter 1 from 'const char *' to 'char *' Conversion loses qualifiers I have a data file called Standard.udf The Data File (not the problem):...
14
3869
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a ..txt file that I want to read into a multi-dimensional string array. Each line of the file needs to be read into the array. OK..sounds easy...
18
8240
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include <iostream> #include <fstream> #include <cstdlib> #include <string>
22
3036
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the case of the struct its 868 instances of said structure. The compiler obviously allows VLA however it craps out after the above amount of bytes. I was told i was attempting to put everythng on the...
33
25197
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
2
1425
by: FightingWolf | last post by:
Hey, first of all sorry for my bad english, but I'm from Germany ;) I found something special in the getline() function, I don't understand. If I use the getline function in case of an Excel-sheet or an image - to get the image-data - the getline function breakes after a NULL termination ( \0 ) fstream oFile_e;
1
1529
by: hauser | last post by:
Hey, first of all sorry for my bad english, but I'm from Germany I found something special in the getline() function, I don't understand. If I use the getline function in case of an Excel-sheet or an image - to get the image-data - the getline function breakes after a NULL termination ( \0 )
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.