Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:46:39+00:00 2026-05-23T23:46:39+00:00

#include <iostream> #include <string> #include <fstream> #include <cstring> using namespace std; int hmlines(ifstream &a){

  • 0
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;

int hmlines(ifstream &a){
int i=0;
string line;
while (getline(a,line)){
cout << line << endl;
i++;

}
return i;

}


int hmwords(ifstream &a){

int i=0;
char c;
while ((c=a.get()) && (c!=EOF)){
if(c==' '){
i++;
}

}

return i;


}








int main()
{
int l=0;
int w=0;
string filename;
ifstream matos;
start:
cout << "give me the name of the file i wish to count lines, words and chars: ";
cin >> filename;
matos.open(filename.c_str());
if (matos.fail()){
goto start;
}
l = hmlines(matos);
matos.seekg(0, ios::beg);
w = hmwords(matos);
/*c = hmchars(matos);*/
cout << "The # of lines are :" << l << ". The # of words are : " << w ;

matos.close();



}

The file that i am trying to open has the following contents.

Twinkle, twinkle, little bat!
How I wonder what you're at!
Up above the world you fly,
Like a teatray in the sky.

The output i get is:

give me the name of the file i wish to count lines, words and chars: ert.txt
Twinkle, twinkle, little bat!
How I wonder what you're at!
Up above the world you fly,
Like a teatray in the sky.
The # of lines are :4. The # of words are : 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:46:40+00:00Added an answer on May 23, 2026 at 11:46 pm
    int hmwords(ifstream &a){
      int i;
    

    You’ve forgotten to initialize i. It can contain absolutely anything at that point.

    Also note that operator>> on streams skips whitespace by default. Your word counting loop needs the noskipws modifier.

      a >> noskipws >> c;
    

    Another problem is that after you call hmlines, matos is at end of stream. You need to reset it if you want to read the file again. Try something like:

    l = hmlines(matos);
    matos.clear();
    matos.seekg(0, ios::beg);
    w = hmwords(matos);
    

    (The clear() is necessary, otherwise seekg has no effect.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <string> #include <regex> #include <ios> #include <locale> using namespace std; int
#include <iostream> #include <string> using namespace std; class BookData { string Title; int Qty;
I'm experiencing a weird problem with stringstream . #include stdafx.h #include iostream #include sstream
I'm attempting to implement quicksort in-place, using boost's threading facility. Unfortunately I'm getting an
I am developing a GUI app on WinXP but unfortunately std::cerr/cout goes nowhere. I
Here's the link to the problem: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1130 This is my code and it works
I am puzzled as to why this program crashes. This is the entire program
Following is my attempt to write BK-Tree , for 150000 word file it takes
I have a ini file which contains some sample values like: [Section1] Value1 =
This is a sample that represents my problem. The map will serialize perfectly fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.