Code Error in *Nix Enviroment (Basic)

Jan 7, 2014 at 12:17pm
Hello,

I know that the bellow code wont work as intended in a *Nix Enviroment.
Also i am unable to identify what is the problem with it, so because of that i come to you guys in order to get some hints/advices here.
I know that part of the coding is bad and insecure, but i am just trying to do some practice while solving easy problems.

The problem is from Codeeval , the statement bellow as well as the code which works on win7. I use Eclipse with MinGW.

You are given two sorted list of numbers (ascending order). The lists themselves are comma delimited and the two lists are semicolon delimited. Print out the intersection of these two sets.

Input sample:

File containing two lists of ascending order sorted integers, comma delimited, one per line. E.g.

1,2,3,4;4,5,6
20,21,22;45,46,47
7,8,9;8,9,10,11,12

Output sample:

Print out the ascending order sorted intersection of the two lists, one per line. Print empty new line in case the lists have no intersection. E.g.

4

8,9


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main (int argc, char* argv[])
{
   ifstream file;
   string lineBuffer,line,comp;

  // file.open(argv[1]);
   file.open("test.txt");
   while (!file.eof())
   {
       getline(file, lineBuffer);
       if (lineBuffer.length() == 0)
           continue; //ignore all empty lines
       else
       {
           //do something here
    	   file.seekg(0,ios::beg);
    	   while (!file.eof())
    	   {
    		   file>>line;
    		   size_t found=line.find(";");
    		   for (unsigned int i=0;i<found;i++){
    			   for (unsigned int j=1;j<line.size()-found;j++)
    				   if (line[i]==line[found+j] && line[i]!=','){
    						   comp.push_back(line[i]);
    				   }
    		   }
    		   //cout<<comp<<endl;
    		   if (comp.size()>1){
    			   unsigned int i=0;
    			   cout<<comp[i];
    			   for (unsigned int i=1;i<comp.size();i++){
    				   cout<<','<<comp[i];
    			   }
    			   cout<<endl;
    		   }
    		   else
    			   cout<<comp<<endl;
    		   comp.clear();
    	   }
       }
    }
   file.close();
   return 0;
}
Jan 7, 2014 at 12:37pm
Why isn't it working? What's happening?
Jan 7, 2014 at 12:49pm
As i do not have the possibility to test this in Linux/UNIX and my only hints are from codeeval output window , see it bellow

Now this should be the output of my program in Unix when more txt files with the proper format are passed to it. As you can see it is wrong...

Any hints?

1,1,1,1,1,1,1,1,1,1,1,1
6,6,5,6,6,6,6,6,6,7,7,7,7,7,7,6,6,8,8,8,8,8,8,8,8,8,6,6,9,7,7,7,7,7,7,0,7,7,7,7,7,7,1,7,7,7,7,7,7,2,7,7,7,7,7,7,3,7,7,7,7,7,7,4,7,7,7,7,7,7,5,7,7,7,7,7,7,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,7,7,7,7,7,7,9
5,4,5,5,5,6,5,7,5,8,8,8,8,8,8,8,8,8,8,8,5,9,9,9,6,0,0,6,1,1,6,2,6,3,6,4,6,5,6,6,6,7,6,8,8,8,8,8,8,8,8,8,8,8
9,7,7,7,7,7,7,7,7,7,7,9,8,8,8,8,8,8,9,9,1,1,0,0,1,1,0,1,1,1,1,0,2,2,1,1,0,3,3
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,7,7,7,7,9,8,8,8,8,8,0,8,8,8,8,8,1,8,8,8,8,8,2,8,8,8,8,8,3,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,7,7,7,7
8,8,8,8,8,8,8,8,8,8,8,0,0,8,8,8,8,8,8,8,8,8,8,8,1,1,8,8,8,8,8,8,8,8,8,8,8,2,2,8,8,8,8,8,8,8,8,8,8,8,3,8,8,8,8,8,8,8,8,8,8,8,4,8,8,8,8,8,8,8,8,8,8,8,5,8,8,8,8,8,8,8,8,8,8,8,6,8,8,8,8,8,8,8,8,8,8,8,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,0,0

8,6,8,7,8,8,8,9,9,9,9,0,0,0,0,0,0,0,0,0,0,9,9,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8,8,8,8,8,8,8,8,8,8,8,0,8,8,8,8,8,8,8,8,8,8,8,8,1,8,8,8,8,8,8,8,8,8,8,8,8,2,8,8,8,8,8,8,8,8,8,8,8,8,3,8,8,8,8,8,8,8,8,8,8,8,8,4,8,8,8,8,8,8,8,8,8,8,8,8,5
9,0,9,1,9,2,9,3,9,4,4,9,5,9,6,9,7,7,7,7,7,7,7,9,8,8,8,8,8,8,9,9,1,0,0,1,0,1,1,0,2,1,0,3
3,4
7,7,7,8,7,9,9,9,9,9,9,9,8,0,0,0,0,0,0,0,8,1,1,1,1,1,1,1,8,2
5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,4,6,6,6,6,6,6,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,6,6,6,6,6,6,8,6,6,6,6,6,6,7
5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,4,6,6,6,6,6,6,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,6,6,6,6,6,6,8,6,6,6,6,6,6,7
Jan 7, 2014 at 2:38pm
Error Found
It is from my coding when i read from file, always I am doing it wrong.

THanks
Jan 7, 2014 at 3:28pm
I compiled and ran the code above on Linux, using the input also copied from above, and the ouptut I got is

4

8,9
8,9

Jan 9, 2014 at 10:52am
Yes indeed
It will work with one digit values. I noticed a bit later that when i have lists with two or more digit numbers and their intersection is not NULL the program wont work as intended, that's why right now i am working on a different version trying to make it a little different :)

Thank you very much for your help.
Topic archived. No new replies allowed.