#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

Dani AI

Generated

Common causes of the wrong counts here are easy to spot: starting word at 1 and treating only the space character as a word separator (so multiple spaces, tabs or newlines are missed), not handling uppercase vowels, and relying on while(fin) which can behave unexpectedly. The approach below reads input line-by-line, stops when * appears, tokenizes each line with a stringstream (so sequences of whitespace count correctly), and counts vowels case-insensitively. This ties together points made by , , , and while avoiding the off-by-one and EOF pitfalls.

#include <iostream>
#include <string>
#include <sstream>
#include <cctype>

inline bool isVowel(char ch) {
    ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
    return ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u';
}

int main() {
    std::string line;
    int lines = 0, words = 0, vowels = 0;
    while (std::getline(std::cin, line)) {
        auto pos = line.find('*');
        if (pos != std::string::npos) line.erase(pos);
        if (!line.empty() && line.back() == '\r') line.pop_back(); // handle CRLF
        ++lines;
        for (unsigned char ch : line) if (std::isalpha(ch) && isVowel(ch)) ++vowels;
        std::istringstream iss(line);
        std::string token;
        while (iss >> token) ++words;
        if (pos != std::string::npos) break;
    }
    std::cout << "No of lines " << lines << '\n'
              << "No of words " << words << '\n'
              << "No of vowels " << vowels << '\n';
    return 0;
}

Notes: contractions and hyphenated words are treated as single tokens by >>; if different behavior is needed, pre-process tokens or use a regex. Avoid system("pause") for portability. If the original intent is to read/write a file, the same logic works with std::ifstream/std::ofstream instead of std::cin/std::cout.

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:

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.