counting certain elements from a file

This is a discussion on counting certain elements from a file within the C++ Programming forums, part of the General Programming Boards category; haha i've done it! i've finished the whole stupid problem...and on such short notice too (sarcastic) ok the problem asked ...

  1. #16
    Registered User
    Join Date
    Dec 2005
    Posts
    15
    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. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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. #18
    Registered User
    Join Date
    Dec 2005
    Posts
    15
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21