I am trying to count the number of words in a string. I believe I have it set up right, except it seems to be looping through unlimited times. Why will it not stop?
I am trying to get it to say 1 for each space. (I know I will have to add 1 at the end for the first word, but I am just trying to get this program to do what I want now). It is just looping forever though.
This is the part where I believe it messes up because I am skeptical. I thought that this would start i at 0 (hence int i = 0), and then loop it and look at every character in sentence, starting with 0 (the first one). I don't know how to get it to stop at the end of the user's sentence.
What do you think is sentence[i]? That would be the character at position i in your string. Each character has its own value: http://www.asciitable.com/
So basically, your loop will continue as long as i is less than the value of the character in position i of your string. Eventually, i is going to go higher than 50, and then where would you be?
You need to think logically about it. What's the condition you need to stop at? You need to stop when you've examined all the letters. How can you know when that is? Well, in C (and C++), the end of a C-string (which is an array of char) is marked with a zero value. So you need to stop when the letter you're looking at is that zero value:
for (int i = 0; sentence[i]!= 0; i++)
Since this is C++, though, you really should use a C++ string instead of an array of char. C++ strings are proper class objects with class methods such as length
for (int i = 0; i < sentence.length(); i++)
Don't forget, = is not the same as ==. if (i == space)
I am sorry I fixed the i=space to i==space. That was a silly mistake.
But Machop, from what I understand, the for loop will now say, i=0, while i is less than the length of the (user entered) sentence, iterate 1 i. This should work, but I am getting a compiler error "request for member 'length' in 'sentence'. which is of non-class type 'char[50]'". Any ideas?
and sorry, I fixed the +1 to "1". I was concentrating on the for loop and wasn't paying much attention when I wrote that.
I am looking at it. I am reading a book and watching youtube videos in order to compliment to book. The book has asked me to do this program, but has not gotten to * yet. Is there a quick explanation for *, or should I continue to read before I finish this program?
#include <iostream>
usingnamespace std;
int main()
{
const size_t N = 50;
char sentence[N];
constchar space = ' ';
cout << "Enter a sentence: ";
cin.getline( sentence, N );
size_t count = 0;
size_t i = 0;
while ( sentence[i] )
{
while ( sentence[i] == space ) ++i;
if ( sentence[i] ) ++count;
while ( sentence[i] && sentence[i] != space ) ++i;
}
cout << "There are " << count << " words in the sentence\n";
return 0;
}
This should work, but I am getting a compiler error "request for member 'length' in 'sentence'. which is of non-class type 'char[50]'". Any ideas?
length is a member function of the C++ class string. You're not using that class. You chose to use a C style array of char to hold the input, instead of a string.