Link to home
Start Free TrialLog in
Avatar of sa9813
sa9813

asked on

getline

Using this very simple program, if I enter a string like this "Firstname Lastname" I have to press enter twice before it outputs the string. How come? Can I alter this behavior?

#include <iostream>
#include <string>

using namespace std;

int main(){
 string name;
 cout << "Enter your name ";
 getline(cin, name);
 cout << "Hello, " + name << endl;
 return 0;
}

Thanks
Niclas

Avatar of Axter
Axter
Flag of United States of America image

Sure.

Example:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string Firstname, Lastname;
cout << "Enter your Firstname and Lastname " << endl;
cin >> Firstname >> Lastname;
cout << "Hello, " << Firstname << " " << Lastname << endl;
return 0;
}

Avatar of sa9813
sa9813

ASKER

Okey, sorry about my bad english, what I wonder is how can I use getline(cin, variable) to read a line into variable? Not having to press enter twice. =)
#include <iostream>
#include <string>

using namespace std;


int main(int argc, char* argv[])
{

cout << "Enter a sentence:" << endl;
     
string str;
char description[CHAR_MAX];
     
//ugly but works!
(cin >> str).getline(description,CHAR_MAX);
str+=description;

cout << str << endl;
}



daniel
This SHOULD work.  It DEFINATELY works with regular character arrays, and should work just as well with strings.

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string name;
  cout << "Enter your name ";
  //read 40 characters max with the default delimiter ('\n')
  cin.getline(name, 40);      

  cout << "Hello, " + name << endl;
  return 0;
}



The getline function is design to retrieve data until it hits the End-Line charactor.  That's why it is called getline.
If you want to get two or more variables from the same line, then you should not use the getline function.
Instead, you should use operator>>() function.  That is the function you need for your requirement.

Is there a specific reason why you don't want to use the operator>>() function?
Actually that is not exactly correct.
The getline defaults to the eof character, but you can in fact have it stop at an character you wish.

There are two ways to use getline

istream &getline(char *buf, streamsize num);
or
istream &getline(char *buf, streamsize num, char delim);

The latter allowing you to specify any delimiter you please.
I think smitty1276 alluded to this in his answer.

ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>I think smitty1276 alluded to this in his answer.

I don't think so.  He was setting the limit on quantity of input characters.

>>The getline defaults to the eof character, but you can
>>in fact have it stop at an character you wish.
My point is that the getline primary purpose is for getting a line of text.
If multiple variables needs to be retrieved from the same line of text, then the operator>>() function should be used instead of the getline function.

>>istream &getline(char *buf, streamsize num, char delim);

Have you tried using this method?
I've tried it, and it does not do what you think it would do.
You can set the deliminator, but the getline function does not return, until you hit the return key.
When it does return, the buffer will have information filled upto the specified deliminator.
This is clearly not the desired effect.
Example:
#include <iostream>
#include <string>
using namespace std;

int main()
{
     string Firstname, Lastname;
     cout << "Enter your Firstname and Lastname " << endl;
     getline(cin,Firstname,' ');
     getline(cin,Lastname);
     cout << "Hello, " << Firstname << " " << Lastname << endl;
     return 0;
}

If you run the above code, the code will not step to the second getline function, until the user hits the enter key.
It does fill Firstname with information up until it hits the ' ' space charactor.  But the user still has to hit the enter key twice.

FYI:
smitty1276's code is using a std::string variable where it should be using a char[] type variable.
Only the global getline() function will work with the std::string varaible.  The cin.getline function works with a char buffer.
Ah! I wasn't sure whether the string class had the appropriate overloaded operators to use with getline as a method of cin or not... but I did specify that it would work with regular character arrays and MAYBE with strings.  I can't believe it doesn't have a (char *) casting operator.

BTW, I said that cin.getline(string, 40) would read up to 40 characters using the default delimiter, which is the newline character.  The fact that it is a DEFAULT delimiter very definately alludes to an overloaded version of the funciton that allows you to specify an alternate delimiter.  Otherwise it would be THE delimiter rather than the DEFAULT delimiter.
>>would read up to 40 characters using the default delimiter,

Sorry, I didn't see the comment mixed in your code.
>>I can't believe it doesn't have a (char *) casting
>>operator
You'll notice that almost all, if not all, the stl class avoid using casting operators.
There are many advance C++ programmers who believe casting operators in a class is a bad thing, so is very unlikely that you'll see a casting operator in an ANSI C++ class.
>>I can't believe it doesn't have a (char *) casting operator.

It is convenient to be able to have a cast to get access to the existing data in a string, but it is only wishful thinking to expect to have a cast that makes a string into a buffer.  That would be far more than a cast -- the code would need to know how to allocate the string and increase its size as more data was read in.

-- Dan
True.