FIZZ BUZZ -challenge program with adjustable intervals This is again a solution to the codeEval challenge #1. It was accepted by codeEval, but please let me know if I can improve it anyway somehow?

#include&lt;iostream&gt;
#include&lt;vector&gt;
#include&lt;fstream&gt;
#include&lt;string&gt;
#include&lt;chrono&gt;

struct input_rec{
    int first;
    int second;
    int last;
};

void parseRecord( std::string &amp;record, input_rec&amp; rec )
{
    int first  = 0;
    int second = 0;
    int last   = 0;
    bool good  = 0;

    size_t start = 0;
    size_t end =  record.find_first_of(" ");
    first = std::atoi(record.substr(start,end-start).c_str());
    if( ( 1 &lt;= first ) &amp;&amp; ( first &lt;= 20 ) )
    {
        good = 1;
    }
    else
    {
        return;
    }

    start = end + 1;
    end = record.find_first_of( " ", start );
    second = std::atoi( record.substr( start, end-start ).c_str() );
    if( good &amp;&amp; ( ( 1 &lt;= second ) &amp;&amp; ( second &lt;= 20 ) ) )
    {
         good=1;
    }
    else
    {
        return;
    }
    start = end + 1;
    last = std::atoi( record.substr( start, std::string::npos ).c_str() );
    if( good &amp;&amp; ( (21 &lt;= last ) &amp;&amp; ( second &lt;= 200 ) ) )
    {
        good=1;
    }
    else
    {
        return;
    }
    if(good)
    {
        rec.first=first;
        rec.second=second;
        rec.last=last;
    }
}
void readInputFile( std::string fileName, std::vector&lt; input_rec &gt; &amp; rec )
{

    //std::cout&lt;&lt;"readInputFile: filename:"&lt;&lt; filename&lt;&lt;std::endl;
    std::ifstream infile( fileName );

    std::string record;
    input_rec temp;
    while( std::getline( infile, record ) )
    {

        parseRecord( record, temp );
        rec.emplace_back( temp );
    }
}
void checkfuzzbuzz( input_rec&amp; rec )
{ 
    int x     = 1;
    int flag1 = 0;
    int flag2 = 0;

    while( x &lt; rec.last )
    {
        if( ( x % rec.first ) == 0)
        {
            flag1 = 1;
        }
        if ( ( x % rec.second ) == 0 )
        {
            flag2 = 1;
        }

        if( ( flag1 + flag2 ) == 2 )
        {
            std::cout &lt;&lt; "FB" &lt;&lt; " ";           
        }
        else if( flag1 == 1)
        {
            std::cout &lt;&lt; "F" &lt;&lt; " ";
        }   
        else if( flag2 == 1 )
        {
            std::cout &lt;&lt; "B" &lt;&lt; " ";
        }
        else
        {
            std::cout &lt;&lt; x &lt;&lt; " ";
        }
        flag1 = 0;
        flag2 = 0;
        ++x;
    }
    if( x == rec.last )
    {
        if( ( x % rec.first ) == 0 )
        {
            flag1 = 1;
        }
        if ( ( x % rec.second ) == 0 )
        {
            flag2 = 1;
        }

        if( ( flag1 + flag2 ) == 2 )
        {
            std::cout &lt;&lt; "FB";           
        }
        else if( flag1 == 1)
        {
            std::cout &lt;&lt; "F";
        }   
        else if( flag2 == 1 )
        {
            std::cout &lt;&lt; "B";
        }
        else
        {
            std::cout &lt;&lt; x &lt;&lt; " ";
        }
        flag1 = 0;
        flag2 = 0;
        ++x;
    }
    std::cout &lt;&lt; "\n";
}
int main( int argc, char* argv[] )
{
    auto start = std::chrono::steady_clock::now();
    if( argc &lt; 2 )
    {
        std::cout &lt;&lt; "usage: fuuzbuzz fuuzbuzz.txt" &lt;&lt; "\n";
        exit( 0 );
    }

    std::ios_base::sync_with_stdio( false );
    std::vector&lt; input_rec &gt; input;
    readInputFile( argv[ 1 ], input );    
    for( auto &amp;x : input)
    {
       checkfuzzbuzz(x);
    }
   auto end      = std::chrono::steady_clock::now();
   auto duration = std::chrono::duration_cast&lt;std::chrono::microseconds&gt;(end-start);
   std::cout&lt;&lt; duration.count() &lt;&lt; "\n";
   return 0;
}
Sort your includes.

Your input_rec has poor names. Something like fizz, buzz and length would be better. If you don't want to give it better names, just use a tuple.

parseRecord takes a line and tries to fill a record. It would be more senible to just return a record (out-parameters are largely a bad idea for small types). This does mean you actually need to handle the early-return cases. For the moment, I'll just return {0, 0, 0} - this is at least better than the old arbitrary-data return value. The input should probably be taken by const &amp;.

The constraints 


  
  The number of test cases ≤ 20
  "X" is in range [1, 20]
  "Y" is in range [1, 20]
  "N" is in range [21, 100]
  


should not be part of your code. They only serve to make it less general.

This gets us to

input_rec parseRecord( std::string const &amp;record )
{
    int fizz = 0;
    int buzz = 0;
    int length = 0;

    size_t start = 0;
    size_t end =  record.find_first_of(" ");
    fizz = std::atoi(record.substr(start,end-start).c_str());

    start = end + 1;
    end = record.find_first_of( " ", start );
    buzz = std::atoi( record.substr( start, end-start ).c_str() );

    start = end + 1;
    length = std::atoi( record.substr( start, std::string::npos ).c_str() );

    return {fizz, buzz, length};
}


The next thing to do is initialize late:

input_rec parseRecord( std::string const &amp;record )
{
    size_t start = 0;
    size_t end =  record.find_first_of(" ");
    int fizz = std::atoi(record.substr(start,end-start).c_str());

    start = end + 1;
    end = record.find_first_of( " ", start );
    int buzz = std::atoi( record.substr( start, end-start ).c_str() );

    start = end + 1;
    int length = std::atoi( record.substr( start, std::string::npos ).c_str() );

    return {fizz, buzz, length};
}


This is much simpler with an istringstream:

#include&lt;sstream&gt;

input_rec parseRecord( std::string const &amp;record )
{
    std::istringstream stream(record);

    int fizz, buzz, length;
    stream &gt;&gt; fizz &gt;&gt; buzz &gt;&gt; length;
    return {fizz, buzz, length};
}


Your readInputFile isn't needed - one can interleave reading and writing. Letting checkfuzzbuzz take its argument by value, this is just:

std::ifstream infile( argv[ 1 ] );

std::string record;
while( std::getline( infile, record ) )
{
    checkfuzzbuzz(parseRecord( record ));
}


Your exit( 0 ); in main can jus be return;, although you probably want return 1;.

checkfuzzbuzz has a typo. Further, the whole flag setting things is meaningless overhead. Just do

void checkfizzbuzz( input_rec rec )
{ 
    int x = 1;
    while( x &lt; rec.length )
    {
        if( ( x % rec.fizz ) == 0 &amp;&amp; ( x % rec.buzz ) == 0 ) {
            std::cout &lt;&lt; "FB" &lt;&lt; " ";           
        }
        else if( ( x % rec.fizz ) == 0)
        {
            std::cout &lt;&lt; "F" &lt;&lt; " ";
        }
        else if ( ( x % rec.buzz ) == 0 )
        {
            std::cout &lt;&lt; "B" &lt;&lt; " ";
        }
        else {
            std::cout &lt;&lt; x &lt;&lt; " ";
        }
        ++x;
    }
    if( x == rec.length )
    {
        if( ( x % rec.fizz ) == 0 &amp;&amp; ( x % rec.buzz ) == 0 ) {
            std::cout &lt;&lt; "FB";           
        }
        else if( ( x % rec.fizz ) == 0)
        {
            std::cout &lt;&lt; "F";
        }
        else if ( ( x % rec.buzz ) == 0 )
        {
            std::cout &lt;&lt; "B";
        }
        else {
            std::cout &lt;&lt; x;
        }
    }
    std::cout &lt;&lt; "\n";
}


Further, you can combine the two to do

void checkfizzbuzz( input_rec rec )
{ 
    for( int x = 1; x &lt;= rec.length; ++x )
    {
        if( ( x % rec.fizz ) == 0 &amp;&amp; ( x % rec.buzz ) == 0 ) {
            std::cout &lt;&lt; "FB";           
        }
        else if( ( x % rec.fizz ) == 0)
        {
            std::cout &lt;&lt; "F";
        }
        else if ( ( x % rec.buzz ) == 0 )
        {
            std::cout &lt;&lt; "B";
        }
        else {
            std::cout &lt;&lt; x;
        }

        if( x &lt; rec.length ) {
            std::cout &lt;&lt; " ";
        }
    }
    std::cout &lt;&lt; "\n";
}


You can drop the return 0;.

You should really find a consistent formatting. Since I'm lazy, I just ran it through clang-format with the default settings (LLVM-style) except with 4-space tabs.

It's probably best not to call std::ios_base::sync_with_stdio(false); - there's no real reason to avoid flushing unless you are actually needing the speed. I also removed the timing code, since it seems to be for debugging purposes.

This all gives:

#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;

struct input_rec {
    int fizz;
    int buzz;
    int length;
};

input_rec parseRecord(std::string const &amp;record) {
    std::istringstream stream(record);

    int fizz, buzz, length;
    stream &gt;&gt; fizz &gt;&gt; buzz &gt;&gt; length;
    return {fizz, buzz, length};
}

void checkfizzbuzz(input_rec rec) {
    for (int x = 1; x &lt;= rec.length; ++x) {
        if ((x % rec.fizz) == 0 &amp;&amp; (x % rec.buzz) == 0) {
            std::cout &lt;&lt; "FB";
        } else if ((x % rec.fizz) == 0) {
            std::cout &lt;&lt; "F";
        } else if ((x % rec.buzz) == 0) {
            std::cout &lt;&lt; "B";
        } else {
            std::cout &lt;&lt; x;
        }

        if (x &lt; rec.length) {
            std::cout &lt;&lt; " ";
        }
    }
    std::cout &lt;&lt; std::endl;
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        std::cout &lt;&lt; "usage: fuuzbuzz fuuzbuzz.txt" &lt;&lt; std::endl;
        return 1;
    }

    std::ifstream infile(argv[1]);
    std::string record;

    while (std::getline(infile, record)) {
        checkfizzbuzz(parseRecord(record));
    }
}


Note that there's basically no real error checking in this, which is worth fixing.