continue not working

Apr 5, 2015 at 6:51am

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

}
}
Apr 5, 2015 at 7:21am
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. 
Apr 5, 2015 at 7:40am
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
Apr 5, 2015 at 7:41am
if no continue how my loop will go to the next iteration?
Apr 5, 2015 at 8:47am
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.
Apr 5, 2015 at 9:06am
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
Apr 5, 2015 at 9:09am
furthermore it consider "space" as 0??
isnt it 32???
in ascii
Apr 5, 2015 at 9:12am
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 Apr 5, 2015 at 9:12am
Apr 5, 2015 at 10:24am
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
Apr 5, 2015 at 10:46am
The problem was in the main
for getting a string with cin.
Apr 5, 2015 at 10:47am
i have a new problem
warning C4496
Apr 5, 2015 at 10:47am
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 Apr 5, 2015 at 10:48am
Apr 5, 2015 at 11:02am
@Gamer2015 Answered your question in your other Thread. Next time dont double post.

http://www.cplusplus.com/forum/general/161383/
Apr 5, 2015 at 11:44am
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 Apr 5, 2015 at 11:44am
Apr 5, 2015 at 12:32pm
what are u suggesting?
Apr 5, 2015 at 12:44pm
I suggest you read my code o.O

http://www.cplusplus.com/forum/general/161383/
Apr 5, 2015 at 12:48pm
saw it didn't like it too longgg...
as far as i consornd nevertheless thanks .

I still didn't understand your explanation about C4496
Apr 5, 2015 at 1:51pm
Apr 5, 2015 at 4:51pm
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.