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 8812351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:35:12+00:00 2026-06-14T03:35:12+00:00

I’m currently trying to write a program which will read words from a text

  • 0

I’m currently trying to write a program which will read words from a text file. Finally I plan to read certain words from a file and so on but at the moment I can’t get my current code to work.

I have 3 files. Header file, main file and implementation file.

ReadWords.h

#ifndef READWORDS_H
#define READWORDS_H
/**
* ReadWords class. Provides mechanisms to read a text file, and return
* capitalized words from that file.
*/
using namespace std;

#include <string>
#include <fstream>

 class ReadWords
 {
   public:
    /**
     * Constructor. Opens the file with the default name "text.txt".
     * Program exits with an error message if the file does not exist.
     */
     ReadWords();

    /**
     * Constructor. Opens the file with the given filename.
     * Program exits with an error message if the file does not exist.
     * @param filename - a C string naming the file to read.
     */
     ReadWords(char *filename);

    /**
     * Closes the file.
     */
     void close();


   // working storage.
   private:
     string nextword;
     ifstream wordfile;
     bool eoffound;

 };

 #endif

ReadWords.cpp

#include "ReadWords.h"
#include <iostream>
using namespace std;

//:: Defines function as member of class.


ReadWords::ReadWords(char *filename)
{
    ifstream str;
    str.open("hamlet.txt");
    char c;
    while ((c = str.get()) !=EOF){
        cout << c << endl;
    }
}

void close()
{

}

main.cpp

#include "ReadWords.h"

int main()
{
    ReadWords rw;
    rw.ReadWords("hamlet.txt");
}

Now I know I’m clearly doing something wrong, but I’m not 100% sure what.
The error I receive in compilation is as follows:

main.cpp: In function `int main()':
main.cpp:6: error: invalid use of `class ReadWords'

Tool completed with exit code 1

Any help is greatly appreciated. 🙂

  • 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-14T03:35:13+00:00Added an answer on June 14, 2026 at 3:35 am

    In your main.cpp, you missed the quotes in the #include ReadWords.h directive. To fix this you should use #include "ReadWords.h".

    Also, you should note that std::istream::get returns only a character. If you want to read a whole word in a (for example) std::string, you should use std::istream::operator >> like this:

    std::ifstream in("my_file");
    std::string word;
    
    if (in.is_open()) {
        while (in >> word) {
            //do something with word
        }
    }
    

    Another thing that stands out is that in rw.ReadWords("hamlet.txt") you’re calling a constructor as if it were a member function. The proper way to use that overload is: ReadWords rw("hamlet.txt").

    As a side note: the constructor’s job is to initialize the object. It’s not a good practice to do more than that inside it’s body.

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

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I have a text area in my form which accepts all possible characters from
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm trying to select an H1 element which is the second-child in its group
I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a reasonable size flat file database of text documents mostly saved in

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.