Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cin.getline being strange...

Status
Not open for further replies.

idNut

Programmer
Aug 2, 2002
6
US
I have a class and I've instantiated the variable I was going to use for the getline but when I run it the line is completely skipped for some reason.

Up in the constructor:
Address::Address()
{
cAddress = new char [ARYA];
//Other stuff too but this is the main issue.
}

Code in function:

void Address::GetAddress()
{
cout<<&quot;Your address: &quot;;
cin.getline(cAddress, ARYA,'.');
//Will end after they enter a period.
}

Can you help me with this? I've tried multiple things but this function just keeps getting skipped over. Like the cout will be displayed but no input is &quot;allowed&quot;; the next function is called and its cout information is displayed where the cin is supposed to be for the GetAddress() function.
 
In GetAddress, you are telling it to stop input when it reaches a '.'. Is that what you really want? Try taking out that parameter. It should stop when it hits a newline.
 
No, that doesn't work either. I just completely skips over the function, it's strange and I'm stumped. I figured someone on here maybe has had the same problem.
 
i wrote like this...it is working...

void main()
{
char str[256];
memset(str,0,255);
cout<<&quot;Enter some text of test : &quot;;
cin.getline(str,256,'.');
cout<<str<<endl;
}
 
That didn't work either. Here's what mine looks like.

#define ARYA 3000

class Address
{
private:
char * cAddress;

public:
Address(); //Constructor.
~Address(); //Destructor.
void GetAddress(); //Function used.
};

Address::Address()
{
cAddress = new char [ARYA];
}

Address::~Address()
{
delete []cAddress;
}

void Address::GetAddress()
{
cout<<&quot;Your address: &quot;;
cin.getline(cAddress, 3000);
}

So I don't know what's the story here. It's being weird. I've used cin.getline before and it worked fine, now this is just being screwy. Is there an error I'm overlooking?
 
This code snippet is working for me...i just added &quot;'.'&quot; to ur code at cin.getline. Earlier...memset will initialize ur variable to '0'.

#define ARYA 3000

class Address
{
private:
char * cAddress;

public:
Address(); //Constructor.
~Address(); //Destructor.
void GetAddress(); //Function used.
void PrintAddress();
};

Address::Address()
{
cAddress = new char [ARYA];
}

Address::~Address()
{
delete []cAddress;
}

void Address::GetAddress()
{
cout<<&quot;Your address: &quot;;
cin.getline(cAddress, 3000,'.');
}

void Address::printAddress()
{
cout<<cAddress<<endl;
}

void main()
{
Address addr;

addr.GetAddress();
addr.PrintAddress();
}
 
Just as a matter of interest: are you using <iostream> or <iostream.h>.

<iostream> is the new std
<iostream.h> is the old std.

There is a VC++ bug in std::getline where you have to input the first line twice. I don't know if the same bug exists in <iostream.h> cin.getline but <iostream> cin.getline should work.
 


Hi,

istream and ostream classes use predetermined derived streambuff classes for low-level buffering. In this case it is possible that buffer is not getting cleared before you call cin.getline(...). Try adding this line in your code

void Address::GetAddress()
{
cout<<&quot;Your address: &quot;;
cout.flush();
cin.getline(cAddress, 3000,'.');
}

flush() will flushout the buffer before you call the next function. I hope this works...

 
try to add fflush(stdin). It will flush the input buffer.

#include <stdio.h>
...
void Address::GetAddress()
{
cout << &quot;Your address: &quot;;
fflush(stdin);
cin.getline
}
 
I don't think it is a good idea to mix <iostream> cin/cout with <stdio.h> fflush. The systems do not know about each other and the I/O system will get very confused.
 
fflush(stdin) is not in the standard. If you look into it fflush() can only be used on output streams.
The problem you are experiencing is as some have said your streambuf is not empty. This commonly occurs because you are mixing calls to cin>> and cin.getline().It can be a good idea to use cin.getline for all input using atoi() and atof() as necessary.
you can clear the input buffer by writing a small function.

void InFlush( istream & is)
{
is.clear();
while ( is.get() != '\n' && !is.eof());
is.clear();
}

so if you include this in your code and before u call getline you call....
InFlush(cin);
then with a bit of luck your problems should be over.

Try to understand why you shouldn't really mix calls to getline and operator >>.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top