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

  • SEARCH
  • Home
  • 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 8548371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:28:47+00:00 2026-06-11T13:28:47+00:00

I was working on a program that printed out the word count, character count

  • 0

I was working on a program that printed out the word count, character count and line count depending on the user’s input. But I keep getting these error that are completely unknown to me. I was wondering if anyone could help.
** I’ve changed it and am still receiving errors. Sorry I’m new to C++.

The errors I got were

 filestat.cpp:47: error: ‘line’ was not declared in this scope
 filestat.cpp: In function ‘int wc(std::string)’:
 filestat.cpp:55: error: ‘line’ was not declared in this scope
 filestat.cpp: In function ‘int cc(std::string)’:
 filestat.cpp:67: error: ‘line’ was not declared in this scope


#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int lc(string fname);
int wc(string fname);
int cc(string fname);

int main(){
string fname,line,command;
ifstream ifs;
int i;
while(true){
    cout<<"---- Enter a file name : ";

    if(getline(cin,line)){
        if(line.length()== 4 && line.compare("exit")== 0){
            cout<<"Exiting";
            exit(0);
        }else{
            string command = line.substr(0,2);
            fname= line.substr(4, line.length() -5);
                if( ifs.fail()){
                    ifs.open(fname.c_str());
                    cerr<< "File not found" <<fname <<endl;
                    ifs.clear();
                }else{
                    if(command.compare("lc")){
                        lc(fname);
                    }else if (command.compare("wc")){
                        wc(fname);
                    }else  if(command.compare("cc")){           
                                      cc(fname);                    
                    }else
                        cout<<"Command unknown. ";


                }
        }
    }
}
return 0;
}

 int lc(string fname){
int count;
while(getline(fname, line)){
    count++;
}
cout<<"Number of lines: "<<count ; 
   }

  int wc(string fname){
int count;
while(getline(fname, line)){
    int pos=line.find_first_of("\n\t ",0);
    while(pos =! string::npos){
        int length=line.length();
        line = line.substr(pos+1, length - pos);
        count++;
    }
  }
cout<< "Number of words: " <<count; 
  }
 int cc(string fname){
int count;
while(getline(fname, line)){
    count = count + line.length();
}

cout<< "Number of words: " <<count;

    }
  • 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-06-11T13:28:49+00:00Added an answer on June 11, 2026 at 1:28 pm

    length() is a member function of std::string. You are missing the ()

    if(line.length== 4 && line.compare("exit")== 0) // line.length()
    

    Also std::string::length() returns an integer. 4should not be surrounded in "".

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

Sidebar

Related Questions

I was working on a program that printed out the word count, character count
I'm working on a program that reads in users input as a string, which
I'm working on a program that asks for input, then calculates and posts the
I'm working on a program that will sort files based on extension I currently
I am working on a program that uses IE to display its help pages.
I'm working on a program that will need to delete a folder (and then
I'm working on a program that creates thumbnails of JPEG images on the fly.
I'm working on a program that accesses StarTeam using the StarTeam SDK for .NET.
I am working on a program that extracts real time quote for 900+ stocks
I'm working on a program that takes in a Wavefront .obj file and eventually

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.