1

I'm working on one of the easy challenges on CodeEval right now. You're required to take input from a file line by line, and each line contains hexadecimal numbers and binary numbers separated by a pipe. The objective is the sum up all of the hexadecimal numbers on the left and sum up the binary numbers on the right, and test which sum is bigger. If the right side(the binary side) is greater than or equal to the hex side, then you print "True", if not, then you print "False". An example line would be "5e 7d 59 | 1101100 10010101 1100111", and the output would be true because the right side is greater than the left. My code prints the correct output on my computer, but on CodeEval, there is no resulting output, and I get a zero for the score. There are no errors listed. Is there a problem that I just cannot see?

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <math.h>
using namespace std;

fstream myFile;
string line;
vector<string> panNums;
int sum1, sum2;

int toDec(string s);

int main(int argc, char *argv[])
{
  //open the file
  // get numbers by line
  myFile.open(argv[1]);
  while(getline(myFile, line))
    {
      //cout << line << endl;
        istringstream mystream(line);
        string nums;
        // read in each number into string nums one by one
        // then add that number to the vector that was created
        while(mystream)
          {
            mystream >> nums;
            panNums.push_back(nums);            
          }
        bool afterSep = false;
        sum1 = 0;
        sum2 = 0;
        for(int i = 0; i < panNums.size() - 1; i++)
            {
                stringstream stream;
                if(panNums.at(i) == "|")
                 {
                    sum1 = sum2;
                    sum2 = 0;
                    afterSep = true;
                    i++;
                 }  

                // if true, do stuff
                if(afterSep)
                 {
                    // deals with the binary side
                    sum2 += toDec(panNums.at(i));
                 }
                // if false, do other stuff
                else
                 {
                    // deals with the hexidecimal side
                    istringstream f(panNums.at(i));
                    int temp;
                    // reading hex number into int(AKA converting to int)
                    f >> hex >> temp;
                    sum2 += temp;
                 }  

            }
                // cout << "sum1 " << sum1 << endl;
                // cout << "sum2 " << sum2 << endl;
                if(sum2 >= sum1)
                 {
                    cout << "True" << endl;
                 }
                else
                 {
                    cout << "False" << endl;
                 }
        // clear the current vector in order to exclusively have the next line of text stored
        panNums.clear();
    }

}

int toDec(string s)
{
    int num = 0;
    int i = s.size() - 1;

    // starts at index 0
    // which is really the 2^6 or however big the binary number is
    for(int a = 0; a < s.size(); a++)
      {
        if(s.substr(i, 1) == "1")
         {
            num += pow(2, a);
         }
        i--;
      }
    // cout << num << endl;
    return num;

}
Sherbs
  • 37
  • 6
  • I'm not familiar with CodeEval but this kind of site usually delivers input through standard input, not files. – molbdnilo May 31 '16 at 19:42
  • Yea, that's typically what I see on other coding sites too, but CodeEval uses a path to a file as the input. – Sherbs May 31 '16 at 19:50
  • `#include ` — you seem to be depending on it being pulled in by something else, which is platform-dependent. – Nick Matteo May 31 '16 at 19:52
  • Look for ways to blow up your program. The CodeEval "judge" probably feeds the submissions with incorrect values. – Thomas Matthews May 31 '16 at 19:52
  • Maybe change `while(mystream)` to `while(mystream >> nums)`. See [why eof is bad](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Thomas Matthews May 31 '16 at 19:54
  • Why the `-1` in `panNums.size() - 1`? – Thomas Matthews May 31 '16 at 19:56
  • Sorry that I didn't mention this beforehand, but for each submission, they list the input they used. I've tried their input with my program, and it still works on my end. – Sherbs May 31 '16 at 19:57
  • 1
    Change `num += pow(2,a);` to `num += 1 << a;`. The `pow` function is a waste for powers of 2 and squaring. The left shift is simpler and most processors have an instruction that performs the operation, versus calling a function to do this. – Thomas Matthews May 31 '16 at 20:00
  • 1
    Replace `if (s.substr(i,1) == "1")` with `if (s[i] == '1')`. You don't need to extract a 1 character substring. Takes too long. Just look at 1 character. The string can be treated as an array of characters. – Thomas Matthews May 31 '16 at 20:03
  • Thanks for these improvements and the explanations for them. I've made the changes, but still no luck. – Sherbs May 31 '16 at 20:12
  • It turns out that my declaration of fstream should have been ifstream because CodeEval's environment doesn't allow permissions to writing to fstreams. Thanks for all of the help. – Sherbs May 31 '16 at 21:29

1 Answers1

0

Are both computers the same OS? If so they should be fine, but otherwise you would need to compile on both systems which means having an executable for each. That works here anyway when I write stuff on mac and want to run it on mac and linux.

Harmony
  • 1
  • 2
  • Thanks for the answer. I read on the submission guidelines, and the site noted that they use a unix type environment for compilation, so this should compile. That's the really confusing part because I've used this site in the past, and if the code couldn't compile on the site, it would actually give me an error. This time, the submission lists no errors. – Sherbs May 31 '16 at 19:53