Advertisement

Please help with C++, can't find this anyhwere!!

Started by March 10, 2001 12:41 AM
4 comments, last by caffeineaddict 23 years, 11 months ago
I am writing a program in C++ in dos that requires that we have a space between words, i was wondering how to do this, i have looked all over the internet, and came up with nothing, i think i read somewhere it''s like cin.getline or something, please tell me the exact function to use, and if i need to add any more .h files, this is greatly appreciated, and please help ASAP, if possible, thanks alot
You can use cin.getline(). use it like so:
char TheLine[128];
cin.getline(TheLine, 128, ''\n'');
TheLine is just storage space
128 is the maximum size to get
''\n'' is optional, you can leave it out, it''s the newline character and it''s what getline() waits for until it stops reading input (so when you push enter it will stop reading)

Or.. you can use gets() like so:
gets(TheLine);
that''s all there is to it, it will just read one line though.

it requires stdio.h
Advertisement
thanks for the reply quantum but i forgot to mention that I am using strings, any help there would be appreciated, sorry about the mix up
Hmm. Not so sure, but I think you might be able to replace TheLine with MyString.c_str(). Otherwise, I don''t know.
Don''t know if this is what you want but you could just add the line

  MyString += TheLine;  


after called cin.getline(TheLine,128);

That ought to do the trick.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Is it so bad to us string-class and it quite excellent
interfaces


You can define string in this way
//-------
string temp;
getlin(cin,temp);
//------------------

now temp is entire line you wrote. IT IS A GOOD THING. You don''t
have to worry about buffers and etc. dull and kinky stuff.

In C++, if you make mistakes about reading arrays - specially
char *array, you''ll learn more about compile errors and system
crashes than you ever wanted to. String handle ALL memory
problems in elegent way. The game programmers can focus on more
important stuff like optimazing self-made code.

Trust me, string-class is one of best friend of C++-programmer.

This topic is closed to new replies.

Advertisement