Question: Write a program that reads a text file called "naughty.txt" and creates another file called "nice.txt" that is exactly the same, except for one thing: every word that begins with the letter F or f is replaced by a string of stars that are the same length as the removed word.
Note! Our univesity has their own libraries created that allow us to basically just use "library.h" and be able to code.
My Code:
#include "library.h"
struct mytype{string word;};
void replace(mytype array[], int size)
{
ifstream fin("naughty.txt");
if(fin.fail())
{
cout << "Error opening file "<< endl;
exit(1);
}
else {
while(!fin.eof())
{
for(int i = 0; i < size; i++)
{
if(array[i].word =="f" || array[i + 1].word == "F ")
Fixed Code:
#include "library.h"
struct mytype{string words;};
void read(mytype array[], int size)
{
ifstream fin("naughty.txt.txt");
if(fin.fail())
{
cout << "Input file didn't open." << endl;
}
else
{
ofstream fout("nice.txt");
for(int i = 0; i < size; i++)
{
fin >> array[i].words;
if(array[i].words == "f" || array[i].words == "F")
{
int length = array[i].words.length();
int j = 1;
while(j < length)
{
fout << "*";
j++;
}
}
fout << array[i].words;
}
}
}
void main()
{
int size = 1000;
mytype array[1000];
read(array, size);
}
The problem is that my file called naughty.txt has this in it" We don't like fried pickles or fried anything."
My nice.txt file is outputting "wedon'tlikefriendpicklesorfriedanything"
Third Edit: #include "library.h"
struct mytype{string words;};
void read(mytype array[], int size)
{
ifstream fin("naughty.txt");
if(fin.fail())
{
cout << "Input file didn't open." << endl;
}
else
{
ofstream fout("nice.txt");
for(int i = 0; i < size; i++)
{
fin >> array[i].words;
if(array[i].words[0] == 'f' || array[i].words[0] == 'F')
{
int length = array[i].words.length();
int j = array[i].words.find_first_of("f" );
while(j < length)
{
fout << "*";
j++;
}
}
else if(i > 0){
fout << ' ';
fout << array[i].words << " ";}
}
}
}
void main()
{
int size = 1000;
mytype array[1000];
read(array, size);
}
Data in the file "nice.txt" is We don't like ***** fried pickles or anything ***** fried