# Thread: counting certain elements from a file

1. haha i've done it! i've finished the whole stupid problem...and on such short notice too (sarcastic)
ok the problem asked me to see wether the binary number from "nrbinar.in" was odd or not, and if the number was straight then, considering that number written as 2^x *odd number, i was supposed to find out the "x". (ie: 12, not being odd, can be written as 2^2 * 3 and in binary if you take away the two zeros from 1100(=12 in decimal) then you're left off with an odd number, namely 11(=3 in decimal), so the number of zeros you take away to form the bigest odd number possible is exactly the power of two i had to find.
[i know it took me a while...]
here's the final thing
Code:
```#include <fstream.h>
#include <conio.h>
unsigned long  int nr=0;
char  a,t;

char paritate(){
fstream f("nrbinar.in", ios::in);
while(f>>t);
return t;
}

void main(){int c=0;
if(paritate()=='1')
cout<<"the number is odd";
else{fstream f("nrbinar.in", ios::in);
while(f>>a){
if(a=='1') nr=0, c=1;
if(a=='0') nr++; }
if(c==0)
cout<<"there is no power for wich, dividing our number by 2^it, will result an odd number";
else
cout<<"the number is straight, and the power of two sought is  "<<nr;
}
getch();
}```
oh one last question ... i was thinking the declaration of the f file was redundant in both "paritate" and "main", and, so, tryed to declare the file as a general variable ..along with the other variables general one like t...but the program didn't work. it couted something out of place.i forgot what. maybe i didn't declare the file right ...hmmm

2. There are ways you could change your program to not open and read the file twice, but with your current setup having two fstreams is better.

If you want to get rid of the second fstream in paritate, think about what that function is doing, and compare that to what main is doing reading the file. See if you can do both things with the same fstream reading through the file once.

BTW, now that you have working code, you can think about fixing some things up about it. First, <fstream.h> is non-standard, and won't work on some modern compilers. You should use <fstream> with the std namespace like some of the other examples in this thread. If your instructor requires <fstream.h>, then use it, but know that you are learning outdated C++. Similarly, it should be int main, not void main, since void main isn't legal C++ and only works on some compilers. Finally, since you use cout, you should #include <iostream> (or <iostream.h> if you must use the old version).

3. hmm <fstream.h> works fine on our school compilers and it has the "cout/cin" commands included so i don't have to include <iostream.h>. honestly i've never used <fstream> with the std thing before ...hmm. haha, i always did have the faint impression i was speaking shakespearian c++. i'll work on "updating" my language...sometime.