I made the changes you reccomended, and added a few things myself epecially in the void wordswap definition.I am still getting errors which is fine that may just take a little more reading(i hope) but i still have a few logic issues that i had added as notes throught the program once again
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <string>
#include <iomanip>
//Object: read in a datafile named badWords.txt that contains words that cannot appear in another file.
//The output file should be the exact same as the input file except whenever a word from
//badwords.txt is encountered in the input it should be replace with asterisks, the asterisks
// should match the number of letters in the censored word.
using namespace std;
using std::string;
int main()
{
ifstream inFile;
ofstream oFile;
int length;
string words;
int i;
void wordswap(string &); //function prototype
inFile.open("namess.txt"); //file with the many sentances that need to be censored.
while(getline(inFile,words)) //Main loop
{
wordswap(words); // function call
}
int pause; // to stop program.
cin>>pause;
return 0;
}
void wordswap(string &words) //Heart of program, Hopefully to find and replace the words with asterisks
{ // problem ive encounterd here is that this needs to run through all the bad words
//for each use of getline(inFile,words)
int x,;
string badWords;
string tempString
string tempLength; // temp// to determine # of *to replace chars with
ifstream inFile // do i need this declaration twice?
inFile.open("words.txt"); ///ERROR
while(getline(inFile, badWords))
{
x=words.find(badWords); //search for the badword in the txt file
tempLength=words.length(badWords); // what is the length of the word to be replaced, to then be changed to asterisks.
for(int f=0;f>=tempLength;f++) // loop to add asterisks depending on how many charactors are in string
tempString.insert('*');
words.replace(x,tempLength,tempString); // To replace the badword in words.txt at position x, with temp variable tempString
tempString.clear(); // Clears the temp string for use in the next getline function
}
inFile.close(); // to close my badwords file
}
// On a last note-- lets say theres a sentance in my words.txt that reads "school is great" with the quotation marks
// Then in the badwords file there is a badword school without quotation marks
// will word.find(badwords) return the position of the word inludeing the paranthesis and then asterisk them all out or
// will it not censor any of it, or just part of it
|
Heres my error log:
Assignment 1/Project1.2.cpp: In function `void wordswap(std::string&)':
Assignment 1/Project1.2.cpp:42: error: `tempString' does not name a type
Assignment 1/Project1.2.cpp:44: error: `inFile' does not name a type
Assignment 1/Project1.2.cpp:47: error: `inFile' undeclared (first use this function)
Assignment 1/Project1.2.cpp:47: error: (Each undeclared identifier is reported only once for each function it appears in.)
Assignment 1/Project1.2.cpp:50: error: `tempLength' undeclared (first use this function)
Assignment 1/Project1.2.cpp:50: error: no matching function for call to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::length(std::string&)'
C:/Dev-Cpp/include/c++/3.4.2/bits/basic_string.h:537: note: candidates are: typename _Alloc::size_type std::basic_string<_CharT, _Traits, _Alloc>::length() const [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
Assignment 1/Project1.2.cpp:52: error: `tempString' undeclared (first use this function)
make.exe: *** ["Assignment 1/Project1.2.o"] Error 1
Execution terminated