How can I count how many strings?
For example, Input is:
How are you
3 strings
if input is:
Fine
1 string
How can I do it? What can I use? Thanks!!
How can I count how many strings?
For example, Input is:
How are you
3 strings
if input is:
Fine
1 string
How can I do it? What can I use? Thanks!!
read a whole line (using cin.getline say), then count the spaces.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Thank you!! I learn by myself, no textbook, no material, nothing!! So, would u mind giving me some hints that how to use cin.getline or give me some tutorial web address which talks about cin.getline?! Thank you very much!!
First I suggest you look up J. Liberty's book Teach Yourself C++ in 21 days. Not everyone likes it, but it is available online for free at several sites, and some people like it a lot.
the prototype for get line is
streamName.getline(char *, int, char = '\n');
meaning getline() is a member function of the istream class. Since ifstream class (used to read files) is derived from istream, getline() is also a member function of ifstream class.
getline() takes three parameters. The first is a char array to hold the string read in. The second is the maximum number of characters to be read in by getline() if the terminating char isn't found before the maximum number of char is read. The third parameter is the terminating char. If this character is encountered before the maximum number of characters is read in it will terminate input. The third parameter defaults to new line char, and is frequently left out of function calls for that reason.
getline() removes the terminating char from the input stream it is reading, unlike other istream member funcitons like >> and get().
getline() treats white space characters like space, tab, newline, etc. just like any other character unlike >>.
> Thank you!! I learn by myself, no textbook, no material, nothing!!
You're not going to get far like that
This should keep you off the streets for a while
http://directory.google.com/Top/Comp...and_Tutorials/
http://www.mindview.net/Books
Thinking in C++ seems a good bet as well.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Thank you very much!!
I get stuck! Can anyone post some example codes?? Not code for me, but teach me by telling examples... pls... thx!!~
here's a site that has Liberty's book on it. Go to chapter 16 section 7. He has a good example on the use of getline(). Beyond that post code that can be evaluated and assistance provided if appropriate.