problem

int textInfo::get_wordCount(void)
{
int start;
int i;
int count;

count = 0;
start = 0;
i = 0;
while (i <= text.length())
{
if (separator(text[i]) == true)
{
if (is_number(text.substr(start, i - start)) == false)
count++;
while (separator(text[i]) == true)
i++;
start = i;
}
i++;
}

return count;
}


So somewhere in here there is a problem which jams whole program , when I compile it shows no errors , but all I get is empty console window . Any ideas why ?
bool textInfo::separator(char symb)
{
int i;

for (i = 0; i <= separators.length(); i++)
if (symb == separators[i])
return true; //simbolis yra skirtukas

return false; //simbolis nera skirtukas
}

bool textInfo::is_number(const string &word)
{
int i;

for (i = 0; i <= word.length() - 1; i++)
if ((word[i] < 0x30) || (word[i] > 0x39)) //jei simbolis nera skaitmuo, pagal ASCII
return false;

return true;
}


this might help too maybe there is an error somewhere in here
There's an empty console since there's no output. you need std::cout or printf for that purpose.
If you are trying to count how many words there in the given string, check this:

1
2
3
4
5
6
7
8
9
10
11
12
int word_count(char *str)
{
	std::string s(str);
	int count = 0;
	char *pc = strtok((char *)s.c_str(), " ");
	while(pc != NULL)
	{
		count++;
		pc = strtok(NULL, " ");	
	}
	return count;
}


Also, you'll need to std::cout the returned count.
[code] Your code goes here [/code]
Arrays goes from 0 to n-1.

Also
1
2
3
4
5
6
7
8
if (separator(text[i]) == true)//redundant
if( separator(text[i]) )

if (is_number(text.substr(start, i - start)) == false) //obfuscated
if ( not is_number(text.substr(start, i - start)) ) // or use !

if ((word[i] < 0x30) || (word[i] > 0x39))//obfuscated
if( (word[i] < '0') || (word[i] > '9') )
@Slumdog: Don't cast the compiler errors. You are breaking the structure of the string. (also, why string if you are going to use it as a char * ?
You could use string::find or string::find_first_of instead of strtok
Topic archived. No new replies allowed.