GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Jan-2006, 22:14
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road
Talking

a quick question


hello friends, it's been a long time.
i had a quick question about the "getline" function.
i was wondering if there was a way to use that function without including the number of characters to be taken in since that will vary.
????
  #2  
Old 29-Jan-2006, 23:16
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: a quick question


No. And why would you? If you did, all you'd do is cause grief because you would have converted the safe getline() into the dangerous gets() (see this link).

And welcome back!
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 30-Jan-2006, 08:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: a quick question


Quote:
Originally Posted by ap6118
hello friends, it's been a long time.
i had a quick question about the "getline" function.
i was wondering if there was a way to use that function without including the number of characters to be taken in since that will vary.
????
The first argument that you give to getline is the address of an array of chars that will receive the data.
The second arbument is the maximum number of chars that you want getline to store (including the terminating zero byte). If the user hits "[Enter]" getline returns at that point:

You would use it something like this:
CPP / C++ / C Code:
#include <iostream>
#include <cstring>

using namespace std;

int main()
{

  char inbuf[100];
  cout << "Enter a line of something: ";
  cin.getline(inbuf, 100);
  cout << "You entered <" << inbuf << ">" << endl;
  cout << "The number of characters entered was " << strlen(inbuf) << endl;

  return 0;
}

Here's a sample run:

Code:
Enter a line of something: This is a test You entered <This is a test> The number of characters entered was 14

Regards,

Dave
  #4  
Old 30-Jan-2006, 15:48
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

Re: a quick question


thanks for the reply guys.actualy, the code is to read strings from a file.these strings a seperated by a period.the strings being read is not include the period so i have to set the period to be my delimeter in getline.the size of those strings vary ofcourse and that's where my problem come in.
  #5  
Old 30-Jan-2006, 17:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: a quick question


Quote:
Originally Posted by ap6118
thanks for the reply guys.actualy, the code is to read strings from a file.these strings a seperated by a period.the strings being read is not include the period so i have to set the period to be my delimeter in getline.the size of those strings vary ofcourse and that's where my problem come in.


Are you reading std::strings with the getline() function? There is no parameter for string size (std::string sizes take care of themselves --- that's one of the big (really big) advantages of std::strings compared to arrays of char). The way you worded the question, I thought you were referring to the istream::getline() member function, which reads into an array of char, and it has a parameter that tells what the maximum number of chars that you will allow.

In general, if you have a question, here are some suggested steps:

Write a program that reads whatever you want, using whatever function you want. If it doesn't work the way you had in mind, then

1. Show us the code.

2. Tell us what you gave the program as an input.

3. Tell us what you got as an output.

4. Tell us what you expected as an output.

5. Tell us what you didn't understand about the difference between (3) and (4).



If your program won't compile, and you don't understand why, then

1. Show the code.

2. Show the compiler messages (Exact messages including line numbers).

3. Tell us what you don't understand about the messages.

4. Tell us what compiler/operating system you are using. Sometimes it makes a difference to someone who is trying to help.


Regards,

Dave
  #6  
Old 30-Jan-2006, 22:54
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

Re: a quick question


Quote:
Originally Posted by davekw7x
In general, if you have a question, here are some suggested steps:
Sounds suspiciously like the link below....
__________________

Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with.
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick, Insertion, and Partition silicon C++ Forum 0 18-May-2005 21:49
Merge and Heap...which is really faster silicon C++ Forum 0 10-May-2005 14:46
non-member function question crq C++ Forum 1 03-Feb-2005 22:59
Simple question on arrays--please help! brookeville C++ Forum 16 18-Nov-2004 00:23
Repetition structure problem and question brookeville C++ Forum 17 29-Oct-2004 18:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.