-5

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

PafflesWancakes
  • 851
  • 1
  • 7
  • 5
  • 2
    what's your question? – AndyG Apr 15 '15 at 03:47
  • You should post the library.h as well! – Maher Apr 15 '15 at 03:49
  • @AndyG my question is how to finish the code. It's not for homework, rather I have an exam tomorrow and I just wanted to learn how to do this properly. – PafflesWancakes Apr 15 '15 at 03:49
  • @Maher This is the link to the libraries that we use: http://rabbit.eng.miami.edu/class/library/index.html – PafflesWancakes Apr 15 '15 at 03:50
  • This is not a service to do your homework for free; if you have a *specific* question or issue, that's what you should be posting here. – MrEricSir Apr 15 '15 at 03:50
  • 1
    checking for `eof()` is also a bad idea. [Source 1](http://stackoverflow.com/questions/5837639/eof-bad-practice) [Source 2](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) [Source 3](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – AndyG Apr 15 '15 at 03:50
  • @PafflesWancakes: It looks like you have a decent start. What part about replacing the characters of the word with asterisks can you not do? Show an attempt. – AndyG Apr 15 '15 at 03:51
  • @MrEricSir if you read the reply to one of the comments above, I clearly state that I am NOT asking for homework. I'm asking for help so that I can learn how to do it incase I encounter a similar question in a future midterm. – PafflesWancakes Apr 15 '15 at 03:52
  • @AndyG Okay. Here goes. It might be slightly bad. if(array[i].word == "f" || array[i].word =="F") { }. I'm not sure how to proceed from this point. If I think about it from a logical viewpoint, I know what to do. But from a programming viewpoint, I don't know how to get to where I want to.. – PafflesWancakes Apr 15 '15 at 03:53
  • @PafflesWancakes: Give us something that will compile. What do you expect `array[i].word` to return exactly? What do your compiler errors say? If/when you can compile can you step through with a debugger. It honestly appears that you've done very little work yourself and you expect us to do it for you. – AndyG Apr 15 '15 at 03:56
  • You're opening naughty.txt but never reading from it. What code's populated array and with what content? Anyway, you might reasonably use `std::string mystring; while (fin >> mystring) { ... }` to read the file, then `if (mystring[0] == 'F' || mystring[0] == 'f') ...`. And Visual C++ 9??? Your lecturer isn't earning his pay, keeping the curriculum relevant. `library.h` is attrocious: "`void main();` `using namespace std;`; C++11 has equivalents to `int_to_string` and `thread`. – Tony Delroy Apr 15 '15 at 03:59
  • @PafflesWancakes Whatever the case, you're posting the question you've been given to solve rather than any specific issue you're actually having. Come back when you have a clear problem statement and an [MCVE](http://stackoverflow.com/help/mcve). – MrEricSir Apr 15 '15 at 04:34
  • @AndyG I compiled the code and got a result that was different thanw hat I was expecting. – PafflesWancakes Apr 15 '15 at 05:31
  • @MrEricSir I compiled the code and in my file "naughty.txt" I had this statement: "We don't like fried pickles or fried anything." My file "nice.txt" had "Wedon'tlikefriendpicklesorfriedanything" – PafflesWancakes Apr 15 '15 at 05:32
  • @PafflesWancakes You need to ask a specific question. It's not clear what help you need. Do you understand why there are no spaces in your output? Do you understand what's wrong with your algorithm? Have you worked out a fixed algorithm? Do you know how to implement it? It's not at all clear what you want. (That's why you're getting downvotes.) – David Schwartz Apr 15 '15 at 05:34
  • @DavidSchwartz I just spent about an hour fixing it and getting it from the first code to the second code. I'm not sure why there are no spaces and why fried isn't being replaced with *****. I've done most of the code and it actually compiles. – PafflesWancakes Apr 15 '15 at 05:36
  • @DavidSchwartz i just want to know how to do the *****. I'm sure I'll be able to get it. – PafflesWancakes Apr 15 '15 at 05:36
  • @PafflesWancakes Okay, good. That's easy to answer. See my answer. – David Schwartz Apr 15 '15 at 05:37
  • @DavidSchwartz I just fixed the issue with spaces. The initial code had fout << array[i].words. I fixed it by changing it to fout << array[i].words << " " ; – PafflesWancakes Apr 15 '15 at 05:37
  • Why after code update filename become `"naughty.txt.txt"`? – VolAnd Apr 15 '15 at 05:56
  • That's becuase when I created the file, I accidentally added .txt to the end of my data txt file name. – PafflesWancakes Apr 15 '15 at 06:01
  • You're nearly there... just change to `if(array[i].words[0] == 'f' || array[i].words[0] == 'F')` and `if (i > 0) fout << ' '; fout << array[i].words;`. – Tony Delroy Apr 15 '15 at 06:49
  • @TonyD sorry for the delayed response, it was 3 am here and I went to sleep. The modifications I made are in the code above. – PafflesWancakes Apr 15 '15 at 10:29
  • @PafflesWancakes Oh - you need to put the output of the word into an `else` statement so it happens when the "if F" bit isn't used.... – Tony Delroy Apr 15 '15 at 11:21
  • @TonyD it worked! Except it deleted the very first word from my output file, i.e. the "we"! – PafflesWancakes Apr 15 '15 at 11:25
  • Yes - you want the `if(i > 0)` bit just inside the `{`, so it controls the output of the space but not the entire scope. – Tony Delroy Apr 15 '15 at 11:30
  • That makes sense! The only thing I'm confused about is why it deleted the first word. Would you mind explaining that to me? – PafflesWancakes Apr 15 '15 at 11:35
  • @TonyD Also, thank you so much for helping me – PafflesWancakes Apr 15 '15 at 11:36
  • @Tony D. Would you mind explaining why the first word is deleted? I also noted that if I add a sentence after the first sentence, it *'s out the period and the space between the end of the first sentence and the beginning of the next. – PafflesWancakes Apr 15 '15 at 11:37
  • The reason it deleted the first word is that the `if` that was meant to prevent printing a space before the first word was instead preventing printing of both space and word if it was the first word. The code says it more clearly than I can! :-) Just noticed this too: `int j = array[i].words.find_first_of("f" );` - that should just be `int j = 0;`. If you fix that, do you still have problems you can't solve? – Tony Delroy Apr 15 '15 at 12:24
  • I decided to exchange int j = array[i].words.find_first_of("f")' with a for loop: for(int j = 0; j < length; j++)'. It gives me similar results, including the omission of the first word! – PafflesWancakes Apr 15 '15 at 12:49

2 Answers2

1
                if(array[i].words == "f" || array[i].words == "F") 

This checks if the word is f or F rather than whether it starts with f or F.

David Schwartz
  • 166,415
  • 16
  • 184
  • 259
  • I changed part of my coding to: if(((array[i].words == "f") || (array[i].words == "F") && (array[i].words != "" || (array[i].words != "\n"))) and I got: *We ****don't ***like ****fried ******pickles *or *******anything ****fried – PafflesWancakes Apr 15 '15 at 05:45
  • I realize that this is far from the desired solution, but it's getting there. – PafflesWancakes Apr 15 '15 at 05:46
  • Again, you're still not checking if the word *starts* with an `f` or `F`. Now you're checking if it *is* an `f` or an `F` *AND* a bunch of other things. But `friend` isn't an `f` or an `F`, so no matter what else you AND onto that, it won't match. – David Schwartz Apr 15 '15 at 06:09
  • I'm thinking here. I think the fact that I'm unable to figure out how to check whether it begins with an f or F is what is keeping this program incomplete. I'm working on how to figure it out. It's just taking a while, I'm sorry. – PafflesWancakes Apr 15 '15 at 06:13
  • Can I implement find_first_of( ) – PafflesWancakes Apr 15 '15 at 06:26
  • Why not just compare the first character? – David Schwartz Apr 15 '15 at 06:50
0

Because you haven’t posted library.h the following example uses only standard libraries. I have added some comments to clarify the idea of char-by-char reading (not word-by-word as you expect):

#include <iostream>
#include <fstream> 
#include <cctype> 
using namespace std;

int main(void)
{
    // open file streams
    ifstream fin; // input stream
    ofstream fout; // output stream
    char c; // bufer (just one character)
    bool word = false; // become true when new word is found
    bool replace = false; // become true when f-word is processed
    // open and check
    fin.open("naughty.txt", ifstream::in);
    if( !fin.is_open() )
    {
        cerr << "ERROR: Source file cannot be open!" << endl;
        return 1;
    }
    fout.open("nice.txt", ofstream::out);
    if( !fout.is_open() )
    {
        cerr << "ERROR: Destiation file cannot be open!" << endl;
        return 1;
    }
    // read and write until end of file reached or reading fail
    while (fin.get(c)) 
    {
        // check the read character for being not a letter (part of word)
        if( !isalpha(c) )   // consider changing condition to: if( isspace(c) ) 
        {   // it is lot a letter, so it is not a word and nothing to be changed
            word = false;
            replace = false; 
            // And just repeat this character
            fout.put(c);
        }
        else
        { // it is a letter, so it is a word
            if( !word ) // if it is a new word
            {
                word = true;
                // check the first letter
                if( c == 'F' || c == 'f' )
                {
                    replace = true; // now replasement is needed
                }
            }
            if( replace )
            {   // if replacement is needed
                fout.put('*');  
            }
            else
            {   // or just repeat the character
                fout.put(c);
            }
        }
    }
    // close files
    fin.close();
    fout.close();
}
VolAnd
  • 6,034
  • 3
  • 19
  • 40
  • We haven't been taught fout.put or fout.get etc. I believe library.h is located in one of the comment links I placed above. I'll include it here: http://rabbit.eng.miami.edu/class/library/index.html – PafflesWancakes Apr 15 '15 at 06:02
  • @PafflesWancakes: I do not think you are prohibited to learn `put()` and `get()` methods, so read http://www.cplusplus.com/reference/fstream/ifstream/ and http://www.cplusplus.com/reference/fstream/ofstream/ – VolAnd Apr 15 '15 at 06:19