Click to See Complete Forum and Search --> : cin>> and white space?? how to input white space
knavely
01-30-2002, 05:04 PM
great im supposed to write a program for my class that "inputs a phone number of the form 678 879 6654 as a string"
but apparantly cin ends the string as soon as it hits a white space, so i cant very well get the whole phone number as one string. Is there a way to do this? I miss gets() but i am being forced to use cin and cout...
Minime80
01-30-2002, 05:10 PM
I believe what you're looking for is called cin.getline( <defined string>, <defined string length> )
EDIT: Oh yeah, don't forget to add 1 to the string length for the null terminator. (so a 10 digit phone number with two spaces in it and a null terminator would be 13 characters long.)
[ 30 January 2002: Message edited by: Minime80 ]
recluse.
01-30-2002, 05:11 PM
IIRC,
cin >> ws;
I hope that works, it will be the first time (I think) that I've helped someone in the Programming Board. :D
knavely
01-30-2002, 05:27 PM
cin.getline( <defined string>, <defined string length> )
yeap that does the trick. Thanks, frankly im surprized it requires this much to do it though. I mean it seems to me that the C
gets() function is much more efficiant.
anyway thanks :cool:
Strogian
01-30-2002, 06:06 PM
Were you taught cin.getline? Doesn't make sense that your teacher would want you to use that unless you were taught it. Do you know any string concatenation functions? How about this:
char str[11]; // I don't how to use c++ strings
cin >> str;
cin >> str+3;
cin >> str+6;
Would you be able to use the "string" datatype using my method? I haven't really ever programmed in C++, so I don't know. Heck, I don't even know if that's how cin works. :)
tecknophreak
01-30-2002, 10:53 PM
Originally posted by Minime80:
<STRONG>
EDIT: Oh yeah, don't forget to add 1 to the string length for the null terminator. (so a 10 digit phone number with two spaces in it and a null terminator would be 13 characters long.)
</STRONG>
doesn't string have that naturally? the old c-strings did since they were just arrays, but string should have the null terminator within it's class.
paully1
01-31-2002, 12:01 AM
Originally posted by knavely:
<STRONG>cin.getline( <defined string>, <defined string length> )
yeap that does the trick. Thanks, frankly im surprized it requires this much to do it though. I mean it seems to me that the C
gets() function is much more efficiant.
anyway thanks :cool:</STRONG>
The problem with gets() in C is it doesnt take a parameter for the buffer length. Very easy to blow up your arrays by pulling to many elements. I rolled out my own MyGets() function that takes a parameter for buffer length.
paul
knavely
01-31-2002, 02:50 AM
Were you taught cin.getline? Doesn't make sense that your teacher would want you to use that unless you were taught it.
hehe well thats a good question :confused:
you see this class only has 1 lecture per week, and she seems to be bassing the course around a certain text "Deitel & Deitel"
the thing is, i cant really afford to shell out 130$ at this point.. :( but she hasnt gone over cout.getline in her lecture, but i strongly suspect it was in the reading.
anyone know any good cheaper C++ books?
the thing i didnt like when i skimmed the Deitel(other then the price :mad: yikes!) was the way its all these lessons. I mean, i think id prefer more of a reference type.
thanks again :)