Hey i just start to play with some strings and i have a little trouble to write something....
I want to write a that allows to input 10 lines of text... I dont know how do you tell the program that after 10 lines of text, you cant wirte anymore.....
Ok, this string make more sense now :D. But can someone pls explain the difference between Moschops and Frameworks code.... I know that the first code doesnt allow to do white-spaces, the second one does. I dont completely understand how is that achived in second code. (Sry for bad english..)
Basically, getline( ) catches all white spaces until the enter or return key is pressed.
Your version of the code, Sibuns, is the same. The only difference I see is that the names are shorter( but less descriptive ) and you've replaced << At_line << with i+1. Other than that, it's basically the same.
I would like to count words in this whole string, but i dont know how to write the part of the code when you tell that whole text you wrote is in new string...exp lines[i]= string whole_text. Am i even thinking in the right way? I have imagine that i would do this like this. First i store the whole text in one string. Than i would get the length of whole string and do:
It looks like it would work, though you still need to consider the issue I mentioned above. And you may want to start "words" at 0, as it's possible you could have a string with only spaces/punctuation, which ought to have 0 words in it.
yea i will work with that later.. :D I need to get this code to work now.... :D.... It doesnt count my words correctly :D.. Any idea why.... It always return 1... dunno why...
voidwords (string lines)
{
int words = 0;
getline(cin, lines);
int length = lines.length();
for (int i = 0;i<length; i++)
{
if(lines[i] == ' ' ||lines[i] == '.' || lines[i] == ', ' || lines[i] == '!' || lines[i] == '?')
words++;
}
cout<<words;
}
And it doest work correct....
The function with input lines works good but when i use words function both functions stops working correctly...
-------------------------
Does this not work because i use getline(cin, lines); again in word count function?... Is there any other way that i can collect the whole text from all lines i input?