0

I'm trying to create a program that filters through speech text, removes any unwanted characters (",", "?", etc., etc.") and then produces a new speech where the words are jumbled based on what words follow or precede them. So for example, if you had the Gettysburg Address:

Four score and seven years ago our fathers brought forth, on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

my program would take that text, put it into a set of strings. i.e. ["Four","score","and","seven",...."continent,"..."Liberty,"..."equal."] Then it would remove any unwanted characters from each string using c++ .erase and c++ .remove, like "," or "." and capitals. After, you'd have a filtered string like ["four","score","and","seven",...."continent"..."liberty"..."equal."]

After that then the words would be rearranged into a new coherent, funnier speech, like: "Seven years ago our fathers conceived on men...", etc.

That was just so you know the scope of this project. My trouble at the moment has to do with either using my iterator properly or null terminators.

#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <string> 
#include <set> 
#include <iterator> //iterates through sets
#include <algorithm>

using namespace std;

int main() {
    set <string> speechSet;
    set <string> ::iterator itr; //forgot what :: means. Declares iterator as set
    int sum = 0;
    int x;
    string data;
    ofstream out;
    string setString;

    ifstream speechFile; //declare output file stream object. Unknown type name

    speechFile.open("./MySpeech");

    if (!speechFile) {
        cerr << "Unable to open file " << endl;
        exit(1);
    }

    char unwantedCharacters[] = ".";

    while (!speechFile.eof()) {
        speechFile >> data; //speechFile input into data
        for (unsigned int i = 0; i < strlen(unwantedCharacters); ++i) {
            data.erase((remove(data.begin(), data.end(),
                unwantedCharacters[i]), data.end())); //remove doesn't delete.    
            data.end() - 1 = '\0'; //Reorganizes
            cout << data << endl;
        }
        speechSet.insert(string(data));
    }

//Go through each string (word) one at a time and remove "",?, etc.
    /*for(itr = speechSet.begin(); itr != speechSet.end(); ++itr){
        if(*itr == ".")//if value pointed to by *itr is equal to '.'
            itr = speechSet.erase(itr);//erase the value in the set and  leave blank
            cout << " " << *itr;//print out the blank
        else{
            cout << " " << *itr;
        }
    }*/

    speechFile.close();
    return (0);

}

I keep getting an error that says error: no viable overloaded '='. At first I thought it might be due to .end() not being a command for a C++ string, but I checked the documentation and it shouldn't be an issue of mismatched data typed. Then I thought it might have to set the iterator itr equal to the end of the data.

iterator itr = data.end() - 1;

and then dereference that pointer and set it equal to the null terminator

itr* = '\0';

That removed the overload error, but I still had another error use of class template 'iterator' requires template arguments. Let me know if any more clarification is needed.

karel
  • 3,880
  • 31
  • 37
  • 42
  • Please try to create a [**Minimal**, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) to show us. I think much of the code in your program that you show could be removed and it would still be possible to replicate the error. Also please copy-paste (as text) the *full* and *complete* error output. Lastly, with the MCVE add comments on the lines mentioned in the error messages. – Some programmer dude Feb 25 '19 at 18:31
  • Unrelated to your problem, but please read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Some programmer dude Feb 25 '19 at 18:32
  • `*(data.end()-1) = '\0';` would get it to compile. You likely don't want to do this though - `std::string` automatically maintains a nul-terminated buffer. You would be stomping on the last *good* character of the string. Just drop that line. – Igor Tandetnik Feb 25 '19 at 20:10
  • As mentioned `std::string` is always terminated (since C++11), but with `itr* = '\0'` did you really mean `*itr = '\0'`? – Some programmer dude Feb 26 '19 at 11:59

1 Answers1

0

In the for loop, use auto for iterator so you don't have to specify its type like:

for(auto itr = speechSet.begin(); itr != speechSet.end(); ++itr){
Aman
  • 673
  • 1
  • 8
  • 25