Problem with bool function

HI, EVERYBODY! I have problem with bool function. The full source code is:

input file contains:
5
2 3 4 5 6


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
#include <iostream>
#include <fstream>
using namespace std;

int subset[10];

// function checks whether the given integer is from subset or not
bool is_from_subset(int x)
{
     //x is maximum 4 digit long
     for(int j=1000; j>0; j/=10){
             if(x>j){ 
                    if( x/j%10 == 0) return 0; //removing zeros
                    if( ((x/j)%10)!=subset[((x/j)%10)]) return 0;
                    }
             }
     return true;
}

main()
{
      ifstream fin("crypt1.in");
      ofstream fout("crypt1.out");
      int N, d, i=0;
      fin>>N;
      for(i=0; i<10; i++) subset[i]=0;
      i=0;
      while(i<N){fin>>d; subset[d]=d; i++;}
      if(is_from_subset(22*222)) cout<<"22*222";
      getchar();
}


the problem is here:
 
if(is_from_subset(22*222)) cout<<"22*222";

it does not produce an output but when I do the following:

1
2
int num=22*222;
if(is_from_subset(num)) cout<<"22*222";

Output:

4884
which is correct output.
I do not understand why when the argument of the function
is_from_subset() is integer the output is correct, but when the argument of the function is any expression there is no output???
Please, can anyone explain me my mistake???
Last edited on
The problem is not in the argument.

What exactly do you mean by "subset"? Because I have no idea what you are trying to do...
closed account (S6k9GNh0)
I seem some strange behavior here.

1
2
int num=22*222;
if(is_from_subset(num)) cout<<"22*222";


Erm, this won't output 4884, it will output 22*222 which signals you aren't showing us something.

In your main function, you assign integer i to 0 twice which is pointless. You also use a while statement where a for statement would be much more appropriate. The integer N is also input into a file with junk. Your subset function also uses return statements of 0 which is the same as returning true in this case. I don't think this is the functionality you wanted.
Last edited on
Topic archived. No new replies allowed.