How to use lib function "getline"

C

Chen shuSheng

I have a code:
---------------------------
#include <iostream.h> \\ where I assume getline() is inner.
#include <stdlib.h>

int main()
{ int max=15;
char line[max];
getline(line,max);
system("PAUSE");
return 0;
}
 
J

Jens Theisen

Chen said:
I have a code:

This includes istream, of which cin is an instace. istream has getline
as a member function. You better do

#include <iostream>

as the other notion is deprecated.
#include <stdlib.h>

This a legacy c header which you don't need.
int main()
{ int max=15;
char line[max];
getline(line,max);

This must be std::cin.getline(line, max), as it is a member function.
system("PAUSE");

What is this supposed to do?
return 0;
}
But it can not pass the complier. Message of complier is implicit
declararion of function 'int getline(....)'

Which means that the compiler doesn't know about it, assuming you want
to declare it implicitly, as is allowed in C.

One more tip: Use std::string's getline instead:

#include <iostream>

int main()
{
std::string line;
getline(std::cin, line);
std::cout << line;
return 0;
}

Jens
 
D

Default User

Chen said:
I have a code:
---------------------------
#include <iostream.h> \\ where I assume getline() is inner.
#include <stdlib.h>

int main()
{ int max=15;
char line[max];
getline(line,max);
system("PAUSE");
return 0;
}


This bit of code has already been discussed extensively on comp.lang.c
(with much confusion, including on my part). The OP first ran across
the getline() in example programming in K&R, without understanding that
it was one they created in a prior example. He then read an outdated
C++ book that indicated it was in <iostream.h>. The declaration for
line he probably got using gcc extensions or something.

The OP needs stop and figure out which language he's working in, and
especially throw away that other book (not K&R) which apparently
purported to be a C/C++ text.




Brian
 
R

red floyd

Jens said:
This includes istream, of which cin is an instace. istream has getline
as a member function. You better do

#include <iostream>

as the other notion is deprecated.
It's not even deprecated. The Standard makes no mention of it one way
or the other. Most pre-Standard compilers provided it, but there was no
official statement about its contents.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top