continue not 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;
}

}
}
In your loop there is nothing happening after continue so no code will be skipped.

Note that your function is looping 3 characters past the end of the string.

 
int size=strlen(s)+2; // Why +2 here? 

 
for(int i=0;i<=size;i++) // You probably want < instead of <= here. 
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

and..


int size=strlen(s)+2; // Why +2 here?


for(int i=0;i<=size;i++) // You probably want < instead of <= here.
its by porpuse
if no continue how my loop will go to the next iteration?
Why it would not go?

my prog needs to sum the num of letters

In other words, you want to print the length of each word in the string?

Lets look at your code again. I'll copy it here and use code tags (you should too) and indent to my liking:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
    }
  }
}

You don't reset the cword, so after the loop the value of cword == size. The "Good luck" ougth to print "4 9 ".

its by porpuse

A C-string is terminated by null character. The strlen() returns the count of characters before the null. The character array does not need to have any more elements than strlen()+1. Your loop dereferences elements strlen()+2 and strlen()+3, which could be an out-of-range error (luckily you don't write to them), and definitely breaks the logic, because the program will report (undefined) word-lengths that are not part of the assumed input.

Please, explain the purpose.


The explicit casts to int on line 8 are unnecessary, because they will occur implicitly (and from documentation point every programmer should know about implicit char->int cast). Furthermore, you are using C-style cast syntax. Please avoid it. The C++ has a more precise static_cast<int>( s[i] ).

What if there are more than one space between consecutive words? What if there is some other whitespace (tab, newline) in the string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void countletters(char*s)
{
	int cword=0;
	int size=strlen(s)+1;
	for(int i=0;i<=size;i++)
	{
		cword++;
		if(s[i]==32||s[i]==0)
		{	
			cout<<cword-1<<' ';
			cword=0;
			
		}
		
	}
}


nevertheless its not working for a string ony for one word.
i cheked and from unknown resone it gives (s[i]) -3 value for the next itration
furthermore it consider "space" as 0??
isnt it 32???
in ascii
if(s[i]==32||s[i]==0)
1.false 2.true

this what is shown in the bp.
when i get to space char.

why??!!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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++;
	    
	}
}


this is not working whyy
The problem was in the main
for getting a string with cin.
i have a new problem
warning C4496
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

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++;
	    
	}
}
int main()
{
	cout<<"Enter string:\n";
	char *str=new char[];
	gets(str);
	countLetters(str);
	delete []str;
	system ("pause");
}
[code]
[/code]
Last edited on
@Gamer2015 Answered your question in your other Thread. Next time dont double post.

http://www.cplusplus.com/forum/general/161383/
char *str=new char[];

No.

How many characters are in that dynamically allocated array? I see no number that should be in the brackets. It is a wonder it the compiler doesn't bark about that.

Even if you had a number there, what would be the correct number? You cannot possibly know in advance how many character the user will write.


PS. Compilers have both errors and warnings. Errors are fatal. Warnings do not prevent completion of compilation, but should draw your focus to questionable code.
Last edited on
what are u suggesting?
I suggest you read my code o.O

http://www.cplusplus.com/forum/general/161383/
saw it didn't like it too longgg...
as far as i consornd nevertheless thanks .

I still didn't understand your explanation about C4496
saw it didn't like it too longgg...

wat?

I still didn't understand your explanation about C4496

This may help you: http://lmgtfy.com/?q=c%2B%2B+C4996
Topic archived. No new replies allowed.