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!

ignore.getline problem

Status
Not open for further replies.

cre8orrr

Technical User
May 6, 2002
21
AU
hi everyone... i am just a begginer with a c++ and i need your help... what i want to do is following:

enter an array of characters and after delete everything from the buffer after ' ' (white space) appears. it looks very simple but i am having big problems while using cin.getline, cin.clear or cin.ignore... it works when i do this:
cin.getline(name, 255, ' '); and then
cin.clear();
cin.ignore(255, ' ');

but in this case if i enter a name without space it keeps waiting to input a space.
any sugestions???
many thanks
vanco
 
in this case if i enter a name without space it keeps waiting to input a space. That's because that is what you told it to do.
Code:
cin.getline(name, 255, ' ');
says, get 255 characters OR until a space has been entered. It sounds like what you need to do is get line (all of it) then parse the line to the space and ignore everything after that or use cin's whitespace manipulator. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
James, thanks for that. now is more clear.
but, when i use cin.getline(name,255, '\n') then
cin.clear(); and
cin.ignore(255,' '); doesnt clear anything. or maybe i am not using them properly.

vanco
 
Try cin.ignore(255,'\n'). That just a quess.
James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Try using just
Code:
get
and checking each character returned. if it's not a space or newline, put it in your buffer (
Code:
name
); if it is, do the
Code:
ignore
.
 
that is probably one solution but i have to input the name at once, and do the validation on the name. here is how i have done it so far:

cin >> a;
cin >> b;
cin.clear();
cin.getline(b,255,'\n');

this program does clear anything remaining after the white character but because of some reason the content of 'a' has been rewriten with the remaining characters in 'b'. for example: if i input:
a='abc'
b='test 123456'
then as a result i have:
a='123456'
b='abc'.

it is weird isn't?
thanks for responding me
 
That does seem weird. Are you sure this isn't what happened?:

Input:
a = 'abc 12345'
b = 'test'

Output:
a = 'abc'
b = '12345'

That would make a lot more sense.

Just as a guess, using that last piece of code you posted, it looks like you'd want to do an
Code:
ignore
after the first
Code:
cin
.
 
the input was
a='abc'
b='test 12345'
and output
a='12345'
b='test'

i dont need actually to input a, so without cin >> a; i have the wanted result.
thanks alot
cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top