my continue isnt working


void countletters(char*s)
{
int cword=0;
int size=strlen(s)+2;
for(int i=0;i<=size;i++)
{
cword++;
if((int)s[i]==32||(int)s[i]==0)
{
cout<<cword-1<<' ';
continue;
}

}
}
What do you expect it to do?
go to the next itration..
And why do you think this does not work?

Note: after your continue there is no code to skip so its pretty useless
my prog needs to sum the num of letters in the string something like:

Good luck in this exam today.
4 4 2 4 4 5
You nearly got it right though...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void countletters(const char* s)
{
    int cword = 0;
    int size=strlen(s) + 1; // counts the 0 at the end as well
    for(int i=0; i < size; i++) // allways use < for iterating in arrays
    {  
      	cword++; // increase letter count
        if(s[i]==10 || s[i]==32) 
        { 
            std::cout<< cword-1 <<' ';
            cword = 0; // reset letter count after word
            // continue; // serves no purpose
      	}
    }
} 
Last edited on
C4996
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

void countLetters(char*s)
{
	int cword=0;
	int size=strlen(s)+1;
	for(int i=0;i<size;i++)
	{
		if(isspace(s[i])||s[i]==0)
		{
			cout<<cword<<' ';
			cword=0;
		continue;
			
		}
	
		cword++;
	    
	}
}
Please dont spam the like that, Just edit your post instead.

Read below.
Last edited on
Well done!

By the way: you should change the decleration of the function.
You should use const char* s because you don't modify the data.

Usually when having a function like yours the compiler gives you a warning because a conversion from a string-constant to char* is deprecated (compiler warning C4996) and because of that the use of const char* is to be prefered.
That means calling that function like this is deprecated: countLetters("Hallo"); // "Hallo" = string-constant
I don't go in to much details about it because you seem to be quite new in software developing and this topic would go down to "memory of constants"

void countLetters(const char*s) {...} // perfectly fine to call it like that now

edit:
so alltogether it should look like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void countLetters(const char* s)
{
    int cword=0;
    int size=strlen(s)+1;
    for(int i=0;i<size;i++)
    {
	if(isspace(s[i])||s[i]==0)
	{
                cout<<cword<<' ';
		cword=0;
	        continue;
        }
	cword++;
    }
}

int main(void)
{
    countLetters("Hello world!");
    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void countLetters(const char* s)
{
    int cword=0;
    int size=strlen(s)+1;
    for(int i=0;i<size;i++)
    {
	if(isspace(s[i])||s[i]==0)
	{
                cout<<cword<<' ';
		cword=0;
	        continue;
        }
	cword++;
    }
}

int main(void)
{
    countLetters("Hello world!");
    return 0;
}


this is an amatures prog i'm getting the string from the user
What do you want to know?
Or does it work just fine?
Topic archived. No new replies allowed.