• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

c++ question re: getline() function

Shark

Member
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.
 
You need to give more details on what x_ptr->var is pointing to, and what do you mean by "hang"? Infinite loop?
 
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.
 
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).
 
#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....
 
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.
 
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.
 
I'm running this on Red Hat 8(?) and I'm just typing it up in gedit.

compile using: g++ filename.c -o filename
 
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.
 
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.
 
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...
 
Back
Top