im having a problem...i have a program to count number of senteces one inputs..the words and asking for a letter. The end of "\\close count" works fine...but for some reason the sentence and word programs doesnt work. could anyone please check this out. I think Iam missing something. I have 3 three separate but I need them to link together into one for it to work. Thanks for the help

#include <string.h>
#include <iostream.h>

using namespace std;

void main()
{
		int count(char *, char), words, senten;
       char userString[51], letter;
		 cout << "Enter a sentence : ";
		 cin.getline(userString, 51);
		 cout << "Enter a letter \n";
       cin >> letter;
       cout << letter << " appears ";
       cout << count(userString, letter) << " times.\n";
} //close main
int count(char *str, char ch)
{
       int times = 0;
       while(*str != '\0')
		 {
		 if (*str == ch)
		 times++;
		 str++;
		 }//close while
       return times;
}// close count

int words
{
		int words = 1;
		while(*str != '\0')
		{
		string userString;
		getline(cin, userString);
		for(i = 0; i < userString.length(); i++)
		{
	  if(userString.at(i) == ' ')
			words++;
		}
	  cout << "Number of words in text: " << words << endl;
        }//close words
}

int senten
{
	int senten = 0;
	while (*str == ch)
    {
        switch (ch) 
		{
                case '.' || case '?' :
                sentence_count++;
                break;
               
    cout << "There is/are " << senten << " sentence/s" << endl;

    return 0;

<< moderator edit: added [code][/code] tags >>

Dani AI

Generated

Summary and quick diagnosis: compilation fails mainly from basic C++ syntax errors, misplaced blocks, and missing function calls. As pointed out, the word/sentence logic never gets invoked from main; as noted, constructs like int words { ... } and using void main() are not valid C++ style. The original posts also mix old headers and C-style I/O with mistaken scoping and name collisions, which hides the real logic problems.

Recommended, robust approach: read the whole line into a std::string with std::getline, then implement three small functions that each take a const std::string& — one to count a given character, one to count words, and one to count sentence terminators. Count words by detecting transitions from whitespace to non-whitespace (this ignores multiple spaces and tabs), count sentences by checking . ? !, and count letters with an optional case-insensitive compare. Avoid unsafe functions like gets or nonstandard headers.

Example of the core helpers (modern C++ style):

int countWords(const std::string &s) {
    bool inWord = false;
    int words = 0;
    for (unsigned char ch : s) {
        if (!std::isspace(ch)) {
            if (!inWord) { inWord = true; ++words; }
        } else {
            inWord = false;
        }
    }
    return words;
}

int countSentences(const std::string &s) {
    int sentences = 0;
    for (char ch : s) if (ch=='.' || ch=='?' || ch=='!') ++sentences;
    return sentences;
}

Practical tips and edge cases: test empty input, strings with leading/trailing/multiple spaces, mixed tabs, and letter-case differences. Beware that simple punctuation-based sentence counting will miscount abbreviations like "Mr." or ellipses; fixing that needs more advanced parsing. Also avoid reusing variable names that match function names, declare prototypes or define functions before main, and compile with a standards-compliant compiler (use int main() and #include <iostream> / #include <string>). and provided useful alternatives, but their examples illustrate why the transition-based word test above is more robust for multiple spaces.

Recommended Answers

All 9 Replies

Umm you haven't called them in main? Or was there more to this?

no more to this.. I wanted the program to count...but i put Int words, sentence and and i assigned a character.

Reread your text on basic syntax.

int words
{
		int words = 1;

It looks like VB. Functions should have a parenthesized parameter list, and it's not a good idea to name a local variable the same as the function.

I changed the words around..again using

#include <string.h>
#include <iostream.h>

void main()
{
		int count(char *, char), wor, senten;
       char userString[51], letter;
		 cout << "Enter a sentence : ";
		 cin.getline(userString, 51);
		 cout << "Enter a letter \n";
       cin >> letter;
       cout << letter << " appears ";
		 cout << count(userString, letter) << " times.\n";
		 cout << "Number of words in text: " << words << endl;
		 cout << "There is/are " << senten << " sentence/s" << endl;
      }//end main
int count(char *str, char ch)
{
       int times = 0;
       while(*str != '\0')
		 {
		 if (*str == ch)
		 times++;
		 str++;
		 }//close while
       return times;
}// close count

int wor;
{
		int words = 1;
		while(*str != '\0')
		{
		string userString;
		getline(cin, userString);
		for(i = 0; i < userString.length(); i++)
		{
	  if(userString.at(i) == ' ')
			words++;
		}
	  cout << "Number of words in text: " << words << endl;
		  }//close words
}

int senten
{
	int sentence_count = 0;
	while (*str == ch)
    {
        switch (ch) 
		{
                case '.' || case '?' :
                sentence_count++;
                break;
	 }
	 cout << "There is/are " << senten << " sentence/s" << endl;
	  }
	 return 0;
}

<< moderator edit: added [code][/code] tags >>

example of what should happen:
Enter a sentence: programming is very hard. [enter]
There are 4 words
There is/are 1 sentence/s
Enter a letter: m [enter]
m is used 2 times

I need to know what is going on with this program.

I need to know what is going on with this program.

It fails to compile because of basic syntax errors.

Oh i see thanx

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[60];
int i,j=0;
clrscr();
for(i=0;i<=60;i++)
a[i]='\0';
printf("\nEnter the string:");
scanf("%[^\n]",a);

for(i=0;i<=strlen(a);i++)
{
if(a[i]==' '&& a[i+1]!=' '&&a[i]!='\t')
{
if(i!=0)
j++;
}
else
{
if(a[i]=='\t'&&a[i+1]!=' '&&a[i+1]!='\t'){
j++;
}}
}
j++;
printf("\nThe number of the words:%d",j);
getch();
}
commented: Five years late to post crappy code? You're in a hurry to show off your poor coding abilities? Or your inability to read forum rules? Or show off that you don't know how to post? +13
commented: You're kidding, Right? -2
commented: Correcting Dave's green -2

Code by chellag is failing if i Give input as :
This is a string

where the space between any 2 words is 4-5 spaces

Consider this:-

#include<iostream> 
using namespace std; 
int main() 
{ 
char str[80];
int len, count=0;
cout << "Enter a string (Max 80 characters):";
gets(str);
len=strlen(str);
for (int i=0; i<len; i++)
	{ if ((i!=len-1 && str[i] == ' ' && str[i+1] != ' ') )
	  count =count+1;
		}
cout << "Number of words in the string are:"<<count <<endl;
getchar();
return 0;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.