c++ question re: getline() function

Shark

Member
Jan 18, 2000
171
0
0
c++ question:

cin.getline(x_ptr->var, size)

Is there something wrong with this syntax? Specifically with the first argument? This line hangs in my program.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
You need to give more details on what x_ptr->var is pointing to, and what do you mean by "hang"? Infinite loop?
 

Shark

Member
Jan 18, 2000
171
0
0
Originally posted by: screw3d
You need to give more details on what x_ptr->var is pointing to, and what do you mean by "hang"? Infinite loop?

yeah sorry.

var is defined as char var[size]

It sits there... I can type forever and it will never go to execute the next line in the program.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
I still don't get it. Is there a lot of code? If not you can just post it there (if you don't mind).
 

Shark

Member
Jan 18, 2000
171
0
0
#define BUF_SIZE 4096

struct p_header
{
...
char from_User[BUF_SIZE];
...
}

........................................
struct p_header outgoing, incoming, *out_PTR;

out_PTR = &outgoing;

...

login_init(out_PTR);
...
void login_init(struct p_header *out_PTR)
{
// Initialize login fields of packet header

out_PTR->Type = '0';
out_PTR->Result = '0'; //set for anal-ness

cout << "Please enter user name: ";
cin.getline(out_PTR->from_User, BUF_SIZE);

cout << out_PTR->from_User;

out_PTR->User_len = sizeof(out_PTR->from_User);
out_PTR->Param_len = '0';
out_PTR->Data_len = '0';
out_PTR->sequence_Number = 0;
out_PTR->opt_Param[0] = '0';
out_PTR->opt_Data[0] = '0';
}


Well I tried to include what I thought was necessary, I wish I could just upload it somewhere....
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
try hitting cntrl-J.

Just a guess, I think maybe it is scanning for a linefeed char rather than a CR.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
What platform? What are you using to develop this?

The getline() function is used with input streams, and reads characters into buffer until either:

* num - 1 characters have been read,
* a newline is encountered,
* an EOF is encountered,
* or, optionally, until the character delim is read. The delim character is not put into buffer.

Hitting enter should satisfy #2, so apparently either the newline isn't getting to your program for some weird reason, or there's some kind of a bug somewhere.
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Hitting enter should satisfy #2, so apparently either the newline isn't getting to your program for some weird reason, or there's some kind of a bug somewhere.

Enter isn't a newline (LF) character, it is a CR character. Editors do translation of this when creating text files.

Cntrl-J is a newline character.
 

Shark

Member
Jan 18, 2000
171
0
0
I'm running this on Red Hat 8(?) and I'm just typing it up in gedit.

compile using: g++ filename.c -o filename
 

Shark

Member
Jan 18, 2000
171
0
0
I just did an ltrace on the program, and it appears that it does get hung well past this.

I was thrown off because my cout right after the getline() never writes anything.

1 bug covers the other.

sheesh - thanks for the eyes.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: glugglug
Hitting enter should satisfy #2, so apparently either the newline isn't getting to your program for some weird reason, or there's some kind of a bug somewhere.

Enter isn't a newline (LF) character, it is a CR character. Editors do translation of this when creating text files.

Cntrl-J is a newline character.

On unix, the enter key, ^J, and linefeeds (\n) are all the same thing. ^M is a carriage return.
 

Yobbo

Senior member
May 21, 2002
546
0
0
Originally posted by: Shark_II
I just did an ltrace on the program, and it appears that it does get hung well past this.

I was thrown off because my cout right after the getline() never writes anything.

1 bug covers the other.

sheesh - thanks for the eyes.

For the future, you should flush your buffer to make sure that you get all the output before the program hangs to help out with debugging...