jayoguntino
jayoguntino

Reputation: 133

How do I remove all the punctuation from the file that I am reading from?

This is my main method in a file called main.cpp:

#include <iostream>
#include "ReadWords.h"
#include "Writer.h"
#include <cctype>
#include <string>

using namespace std;

int main() {

    int count;
    int length;
    string word;

    cout << "Please enter a filename: " << flush;
    char filename[30];
    cin >> filename;

This is where I am trying to delete all the punctuation in the file. Currently I am able to read the file and print all the values. I am struggling to access the file and delete the punctuation. I know that I am to use ispunct. I have tried many different implementations but cannot get it to work. How do I delete all the punctuation in the file?

    ReadWords reader(filename);
    while (reader.isNextWord()){
        count = count + 1;
        reader.getNextWord();


    }

    cout << "There are: " << count << " words in this text file" << endl;    
    return 0;
}

This is another file called ReadWords where I have all my method declarations: I don't think this is relevant/needed

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

void ReadWords::close(){

}
ReadWords::ReadWords(const char *filename) {
    //storing user input to use as the filename

        //string filename;

        wordfile.open(filename);

        if (!wordfile) {
            cout << "could not open " << filename << endl;
            exit(1);
        }

}

string ReadWords::getNextWord() {

    string n;
    if(isNextWord()){
        wordfile >> n;
        return n;

    }

}

bool ReadWords::isNextWord() {

        if (wordfile.eof()) {
            return false;
        }
        return true;
}

Upvotes: 0

Views: 635

Answers (1)

Jeremy Friesner
Jeremy Friesner

Reputation: 73041

Read from the input file one character at a time.

For each character you read:

  • If the character is not punctuation, write it to the output file.
  • If the character is punctuation, don't write it to the output file.

Upvotes: 2

Related Questions