Problem stated: Write the function flip_lines(). Open the input and output files file using the parameters. Return true if both files can be opened and processed, false otherwise. If both files can be opened, write each successive line of output in reverse order. (In other words, switch line 1 and 2, then line 3 and 4 and so on.) Remember that there may be blank lines in the file and that the file may contain an even or odd number of lines.

Can some one help me with the part where I need to flip the lines. I am trying to study for the final exam. Please help me with this.

bool flip_lines(string in_file7, string out_file8)
{
    ifstream fin;
    fin.open(in_file7.c_str());
    if(!fin)
    {
        return EXIT_FAILURE;
        fin.close();
    }
    ofstream fout;
    fout.open(out_file8.c_str());
    if(!fout)
    {
        return EXIT_FAILURE;
        fout.close();
    }

    return true;
}
// 4. Flip each line in a file
    string in_file7 = "file7.txt";
    string out_file8 = "file8.txt";
    result = flip_lines(in_file7, out_file8);
    
    cout << "\nresult EXPECTED true; ACTUALLY it was " << result <<endl;
    cout << "\nEXPECTED file contents are: " << endl;
    cout << "did gyre and gimble in the wabe.\nTwas brillig and the slithy toves\n"
        << "and the mome raths outgrabe.\nAll mimsey were the borogroves,\n"
        << "\"Beware the Jabberwock, my son,\n\nBeware the JubJub bird and shun\n"
        << "the jaws that bite, the claws that catch,\nthe frumious bandersnatch.\"\n";
    cout << "\nACTUAL file contents are:" <<endl;
    system("cmd /c type file8.txt");

This is the file7.txt:

Twas brillig and the slithy toves
did gyre and gimble in the wabe.
All mimsey were the borogroves,
and the mome raths outgrabe.

"Beware the Jabberwock, my son,
the jaws that bite, the claws that catch,
Beware the JubJub bird and shun
the frumious bandersnatch."

Recommended Answers

All 2 Replies

in your function, try the following at #17:

string temp1, temp2;

while(getline(fin, temp1))
{
     //Handle even number of lines
     if(getline(fin, temp2))
     {
          fout << temp2 << endl;
          fout << temp1 << endl;
     }
     //Handle odd number of lines
     else
     {
          fout << temp1;
     {
}

Thanks a lot for that =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.