cin and getline
I don't know if I need to post the whole code but here's the deal...I've basically got a list song titles, and all the program does is allow the user to add or remove songs.
So, the code works right now, so long as I use "cin>>name;" and the song titles are only one word. So to fix this I tried to replace 'cin>>name;" with "getline(cin,name);" and now when I run the program it will run up until where I'm supposed to type in the new song name, and it just skips the whole getline step and adds a new song to the list with "" as the input.
So basically it looks like this before the getline command:
1.) song 1
2.) song 2
3.) song 3
4.) song 4
and then it runs the getline code and does this:
1.)
2.) song 1
3.) song 2
4.) song 3
5.) song 4
is there a way to determine what I'm doing wrong, or do I need to give more info?
[949 byte] By [
BonzoHarry] at [2007-11-20 11:43:56]

# 1 Re: cin and getline
Well getline looks for a delimiting symbol. Look at your file with a hex editor.
Is there a in the beginning of your file?
This is what you got:
<0x0A><0x0D>
song 1<0x0A><0x0D>
this is what you want:
song 1<0x0A><0x0D>
....
you could always just ignore the first getline.
getline reads a line up to the default delimeter or "\n".
HTH,
# 2 Re: cin and getline
Well getline looks for a delimiting symbol. Look at your file with a hex editor.
Is there a in the beginning of your file?
It's not a file he's using, its cin.
So I think you're using getline incorrectly if you're going getline(cin, name). The correct way to use it is like this:
char name[256];
cin.getline(name, 256);
Sometimes you might get a blank line because somebody pushed enter somewhere and it stored itself in the input stream. If this happens, you can just ignore any blank strings you find:
if (name[0] == '\0')
//ignore
# 3 Re: cin and getline
It's not a file he's using, its cin.
So I think you're using getline incorrectly if you're going getline(cin, name). The correct way to use it is like this:
char name[256];
cin.getline(name, 256);
I've been using 'name' as a string, butI tried using that code you gave me and the same thing happens.
It seems when I use getline the program doesn't even prompt for input, it just immediatly reads in "" and doesn't even give the user a chance to input a song name.
Is there any other way I could cin a line of text? Or another getline method? It really bugs me this works fine with single words but not with multiple words.
# 4 Re: cin and getline
1) You are probably using operator >> to read in a menu item and
then using getline(). When you use operator >> , the '\n' will still
be in the input stream, so the getline() stops immediately. You need
to get rid of the '\n'. See my post at the following link:
http://www.dev-archive.com/forum/showthread.php?t=437360
2) stay with using a std::string ... no need to go to char arrays.
# 5 Re: cin and getline
1) You are probably using operator >> to read in a menu item and
then using getline(). When you use operator >> , the '\n' will still
be in the input stream, so the getline() stops immediately. You need
to get rid of the '\n'.
amazing, that was dead on. I did what your post said and it fixed it, thanks a lot!