Home > Software engineering >  My C Loop is not working properly, and I can't figure out why
My C Loop is not working properly, and I can't figure out why

Time:12-17

I'm trying to write a program that copies (from an input.txt file) 30 lines to one file, then loops and copies the next 30 lines to a second file, then the next 30 to a 3rd file, etc. etc. This is what I wrote to try and make that happen:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    // initialize
    ifstream input("input.txt");
    string line;
    int i = 0;
    int fileNum = 0;
    cout << "Initialized";

    // Loop through text file and write to new files
    while (getline(input, line) && i < 30)
    {
        // Write to File
        ofstream outFile("frames/frame"   to_string(fileNum)   ".mcfunction");

        // Write Lines
        outFile << "data modify block " << (i   1) << " -48 20 HeldItem.Item.tag.display.Name set value '{\"text\":\"" << line << "\"}'" << endl;

        // Increment Counters
        i  ;
        if (i == 30)
        {
            //  Add Schedule and Scoreboard Commands to Bottom of Each File
            outFile << "scoreboard players set Frame: BadApple "   to_string(fileNum) << endl;
            outFile << "schedule frame"   to_string(fileNum   1)   ".mcfunction 5s" << endl;

            // Reset Counter & File Number
            i = 0;
            fileNum  ;
            cout << i << " ";

        }
    }

    return 0;
}

But it only ends up copying one line from the input file to each document. Everything seems to be working aside from that.

Any help would be much appreciated, I've been trying to debug this for a while now but as a naive beginner, I haven't gotten very far. Thank you all in advance.

CodePudding user response:

You keep reopening the file on every iteration of the loop. So then it keeps overwriting the first line.

CodePudding user response:

The problem is that you are opening and closing the output file once per line, instead of once every 30 lines.

I suggest that you restructure your program to use a nested loop. The outer loop should handle one output file per loop iteration (i.e. 30 lines), and the inner loop should handle a single line per loop iteration.

Here is an example:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream input( "input.txt" );
    std::string line;
    int fileNum = 0;
    bool finished = false;

    std::cout << "Initialized\n";    

    //process one file per loop iteration
    for ( int fileNum = 0; !finished; fileNum   )
    {
        //open new output file
        std::ofstream outFile("frames/frame"   std::to_string(fileNum)   ".mcfunction");

        //process one line per loop iteration
        for ( int i = 0; i < 30; i   )
        {
            if ( !std::getline(input, line) )
            {
                finished = true;
                break;
            }

            //write lines
            outFile << "data modify block " << (i   1) << " -48 20 HeldItem.Item.tag.display.Name set value '{\"text\":\"" << line << "\"}'\n";
        }

        //add schedule and scoreboard commands to bottom of each file
        outFile << "scoreboard players set Frame: BadApple "   std::to_string(fileNum) << '\n';
        outFile << "schedule frame"   std::to_string(fileNum   1)   ".mcfunction 5s\n";
    }

    return 0;
}

Note that in my code above, I have removed using namespace std;, for the reason described here:

Why is "using namespace std;" considered bad practice?

Also, you should generally only use std::endl if you want to actually flush the buffer. Otherwise, you can simply print "\n", which is better for performance. See the following question for further information:

"std::endl" vs "\n"

I have therefore removed std::endl from the program.

  • Related