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

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.

#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();
}

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Comments
Correcting Dave's green
You're kidding, Right?
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?

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

Edited 6 Years Ago by manali04: not posted correctly

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;

}
This article has been dead for over six months. Start a new discussion instead.