#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
    cout<<"Start entering your text\n Put a * to terminate\n";
    ofstream fout;
    fout.open("1818.txt");
    
    int i=0;
  
    char c;
    while((c=getchar())!='*')
    {
        fout<<c;
                      
    }
    
                            
     fout.close();
     ifstream fin;
     fin.open("1818.txt");
     int word=1;
     int line=0;
     int vowels=0;
     while(fin)
     {
    fin.get(c);
     if(c==' ')
     word++;
     else if(c=='\n')
     line++;
     else if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
     vowels++;
     }
     fin.close();
     cout<<"\nNo of lines"<<line;
     cout<<"\nNo of words"<<word;
     cout<<"\nNo of vowels"<<vowels;
     
    system("pause");
}

i am getting the count wrong........pls anybody help me debugg it.......thanks in advance

Hint: is a space the only thing that can separate two words? Also, if the user hits enter, that's the second line. You're very close here.

A new line is also a word separator (in most cases). You are also missing the last line since it will not end with a new-line character, you should count * as a line separator, or start with line set to 1. You should also initialize word to 0.

Just a few things to consider:

  • You should think about your use of while(fin) , I would go for something like while(fin.fail() == false) instead.
  • What if there are two consecutive spaces (like people tend to do after a period? Or if, for some reason, the sentence begins with a space?
  • How would your program perform on this sentence: His name is Alex. ?

I'm not trying to cause offence, but since your code is so nearly finished, you can start making some new considerations :)

I noticed this:

else if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
     vowels++;

What happens if the read character is "capital" ('A', 'E', 'I', 'O', or 'U')? This will miss those because there is a difference. I think it would be advisable for you to either create an isvowel() function or "normalize" the input by using tolower(). Either way, you're currently only accounting for 5 of the 10 vowels possible in the ASCII character set and need to figure out how to pick up the other 5.

Edited 5 Years Ago by Fbody: n/a

Comments
Good point

What happens if the read character is "capital" ('A', 'E', 'I', 'O', or 'U')?

That's what I was getting at with my third point (above) ;)

Comments
So I see. I missed it before. :)

in this header file are useful character manipulation functions: http://www.cplusplus.com/reference/clibrary/cctype/

and on a random side note, I would set the loop up like this:

while( fin.get(c) )
{
 if( c == '\n' || c == '\r' )//<-- not guaranteed to work.
  lines++;
//...

}

\r is the carriage return, which you may have forgotten about, or never used.
\n is the line feed, which is common on my Windows operating system.

Edited 5 Years Ago by pseudorandom21: n/a

That's what I was getting at with my third point (above) ;)

Oh, I see that now. I originally thought it had something to do with the line count...

Oh, I see that now. I originally thought it had something to do with the line count...

Yeah, I think I was being a little cryptic! It was an effort to make the OP think about possible problems without explicitly stating them. Turns out it was just confusing. Never mind :)

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