Hello every one
recently i have written this code to count letters and words in the phrase that the user enter , it worked once but when deleted (by mistake) and tried to rewrite it again it didn't work , so could you please help me to find out why it's not working ?

using namespace std;
int main()
    int chcount=0;
    int wdcount=1;
    char ch;
    cout<<"Enter A Phrase:";
        while((ch = getche()) !='/r')
         if(ch==' ')
        cout<<"\nWords = "<<wdcount<<endl;
        cout<<"Letters = "<<chcount<<endl;
    return 0;

Try using this code : (Simple and easy)

void main()
 char ch[50];
 int c=0,w=1;
 cout<<"Enter the phrase : ";
 for(int i=0;i<strlen(ch);i++)
   if(ch[i]==' ')
 cout<<"Number of words :"<<w<<endl;
 cout<<"Number of letters :"<<c<<endl;


NOT good to use gets ... NOR to revert to old style C++ and mix up OP using stdio.h when OP was using C++ <iostream>

Also since OP using C++, best to use C++ string.

So ... would recommend something like this instead:

#include <iostream>
#include <sstream> // re. istringstream
#include <string>

using namespace std;

int main()
    cout <<"Enter a phrase: ";
    string line;
    getline( cin, line );

    istringstream iss( line ); // construct iss obj. from line

    // now parse and get word count and character count
    int chcount = 0;
    int wdcount = 0;
    string word;
    while( iss >> word )
        chcount += word.size();

    // report ...
    cout << "\nWords = " << wdcount << endl;
    cout << "Characters = " << chcount << endl;

    cout << "\nPress 'Enter' to continue/exit ... ";
    return 0;

Edited 2 Years Ago by David W: added code example

Also @Avishek_1 ...

Please note that in standard C++ (and C)

it is int main ( NOT void main ) i.e. main returns an int

Thank You Avishek And David
Both codes works fine with me
But would you please tell me what's the mistake in my code

**After looking again to my code i realized that it should be '\r' instead of '/r' , thanks for helping me **

This question has already been answered. Start a new discussion instead.