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;
}