CodeEval Fizzbuzz Challenge

I am just trying to figure out how to program the file to read input from however CodeEval.com does there thing. They say its from a *nix enviroment. I would've thought this code below would work, but I don't understand getline() very well. Also if I were reading line by line I don't think the way I'm reading in variables is the right way. Any help or explanation on how to do this challenge would be helpful. I just don't understand how they are wanting the file to be read in.

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]) {

ifstream stream(argv[1]);
string line;
while (getline(stream, line)) {

line >> divider_x >> divider_y >> counter;

for(int i = 1; i <= counter; i++)
{
if(i % divider_x == 0 && i % divider_y == 0)
cout << "FB ";
else if(i % divider_x == 0 && i % divider_y != 0)
cout << "F ";
else if(i % divider_y == 0 && i % divider_x != 0)
cout << "B ";
else
cout << i << " ";
}
}
return 0;
}
Last edited on
The getline() function reads a std::string. You don't want that. Just read the dx, dy, and count values directly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
using namespace std;

int main( int argc, char** argv )
{
  if (argc != 2) return 0;
  ifstream stream( argv[1] );
  int divider_x, divider_y, count;
  while (stream >> divider_x >> divider_y >> count)
  {
    for (int i = 1; i <= counter; i++)
    {
      ...
    }
  }
}

Also, you should be careful about your whitespace: the challenge specifically forbids trailing spaces in each line of output.

(Also, please use [code] tags.)

Hope this helps.
Thanks man. That looks good. I was just having a really hard time figuring out how to read in a *NIX environment.

Are you putting if (argc != 2) return 0; assuming they are only inserting 1 file into the program? I am just a little confused at how they are reading it in. I am guessing that they are compiling it and then running the executable from the command line and they are inserting just one file...right? How can they do that with a list of numbers needed for the FizzBuzz test?

Sorry for the probably dumb question.
Topic archived. No new replies allowed.