#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

Recommended Answers

All 8 Replies

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.

commented: Good point +6

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

commented: So I see. I missed it before. :) +5

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.

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

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.