while can't go to the 2nd record.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wen

    while can't go to the 2nd record.

    Hallo,
    Can someone tell me why my while doenst read the second, third.... record?
    (This's a part of my code.)
    I've looked for it in the books and on internet, but juist don't see the
    probleem.
    TI@
    Wen


    int klantNr, access;
    Client MutatieRec, MasterRec;
    OpenBestand(acc ess);

    LeesMutatie( MutatieRec, klantNr);
    while( MutatieRec.klan tNr !=HV) //HV is defined as 99999
    {
    cout<<MutatieRe c.klantNr;
    LeesMutatie( MutatieRec, klantNr);
    }
    cin.get();

    --

    Met vriendelijke groet,

    Wen


  • Victor Bazarov

    #2
    Re: while can't go to the 2nd record.

    "Wen" <Wen.Hoogendijk @hccnet.nl> wrote...[color=blue]
    > Can someone tell me why my while doenst read the second, third.... record?
    > (This's a part of my code.)
    > I've looked for it in the books and on internet, but juist don't see the
    > probleem.
    > TI@
    > Wen
    >
    >
    > int klantNr, access;
    > Client MutatieRec, MasterRec;
    > OpenBestand(acc ess);
    >
    > LeesMutatie( MutatieRec, klantNr);
    > while( MutatieRec.klan tNr !=HV) //HV is defined as 99999
    > {
    > cout<<MutatieRe c.klantNr;
    > LeesMutatie( MutatieRec, klantNr);
    > }
    > cin.get();[/color]

    AFAICT, 'LeesMutatie' has not been defined in this program. Could it
    be the source why it's not working? Post complete code, and not some
    fragments that are impossible to validate.


    Comment

    • CrayzeeWulf

      #3
      Re: while can't go to the 2nd record.

      Wen wrote:
      [color=blue]
      > Hallo,
      > Can someone tell me why my while doenst read the second, third.... record?
      > (This's a part of my code.)
      >[/color]
      Modify the code as show below. If it prints "Mutatie.klantN r is equal to
      HV", then problem is not in the code fragment you provided.

      // ---------------------------------------------------------
      int klantNr, access;
      Client MutatieRec, MasterRec;

      if ( MutatieRec.klan tNr == HV ) {
      cout << "Mutatie.klantN r is equal to HV" << endl ;
      }

      OpenBestand(acc ess);

      if ( MutatieRec.klan tNr == HV ) {
      cout << "Mutatie.klantN r is equal to HV" << endl ;
      }
      LeesMutatie( MutatieRec, klantNr);
      if ( MutatieRec.klan tNr == HV ) {
      cout << "Mutatie.klantN r is equal to HV" << endl ;
      }
      while( MutatieRec.klan tNr != HV)  //HV is defined as 99999
      {
      cout << MutatieRec.klan tNr;
      LeesMutatie( MutatieRec, klantNr);
      }
      cin.get();
      // ---------------------------------------------------------

      --
      CrayzeeWulf

      Comment

      • Wen

        #4
        Re: while can't go to the 2nd record.

        Well, I can't post all my code, it's really to much. But I'd select this
        part, so that you can can see what's wrong with it.
        Regards,
        Wen

        #include <iostream>
        #include <ctime>
        #include <string>
        #include <cstring>
        #include <fstream>

        const int HV= 99999;

        struct Client
        {
        int klantNr;
        char soort[2];
        char naam[26];
        char adres[26];
        char postcode[7];
        char plaats[16];
        int bankNr;
        int giro;
        char mutcode[2];
        char tariefAfspr[2];
        };

        using namespace std;
        void OpenBestand(int & access);
        void LeesMutatie(Cli ent & MutatieRec, int & klantNr);

        int main()
        {
        int klantNr, access;
        Client MutatieRec, MasterRec;
        OpenBestand(acc ess);

        LeesMutatie( MutatieRec, klantNr);
        while( MutatieRec.klan tNr !=HV)
        {
        cout<<MutatieRe c.klantNr;
        LeesMutatie( MutatieRec, klantNr);
        }
        cin.get();
        }

        void LeesMutatie(Cli ent & MutatieRec, int & klantNr)
        {
        ifstream mutatie ("mutatie.csv") ;
        char puntkomma = ';';

        mutatie >> MutatieRec.klan tNr >> puntkomma;
        mutatie.getline (MutatieRec.soo rt, 2, ';');
        mutatie.getline (MutatieRec.naa m, 26, ';');
        mutatie.getline (MutatieRec.adr es, 26, ';');
        mutatie.getline (MutatieRec.pos tcode,7, ';');
        mutatie.getline (MutatieRec.pla ats, 16, ';');
        mutatie >> MutatieRec.bank Nr >> puntkomma;
        mutatie >> MutatieRec.giro >> puntkomma;
        mutatie.getline (MutatieRec.mut code, 2, ';');
        mutatie.getline (MutatieRec.tar iefAfspr, 2, ';');
        if (mutatie.eof())
        MutatieRec.klan tNr = HV;
        }

        void OpenBestand(int & access)
        {
        ifstream constant, klant, mutatie;

        constant.open(" constant.csv");
        if(constant.fai l())
        {
        cerr << "Het constantenbesta nd kon niet worden geopend" << endl;
        access = 0;
        }

        klant.open("kla nt.dat", ios::binary );
        if(klant.fail() )
        {
        cerr << "Het klantenbestand kon niet worden geopend" << endl;
        access = 0;
        }

        mutatie.open("m utatie.csv");
        if(mutatie.fail ())
        {
        cerr << "Het mutatiebestand kon niet worden geopend" << endl;
        access = 0;
        }
        }
        "Victor Bazarov" <v.Abazarov@com Acast.net> schreef in bericht
        news:5IoIb.7985 6$VB2.162883@at tbi_s51...[color=blue]
        > "Wen" <Wen.Hoogendijk @hccnet.nl> wrote...[color=green]
        > > Can someone tell me why my while doenst read the second, third....[/color][/color]
        record?[color=blue][color=green]
        > > (This's a part of my code.)
        > > I've looked for it in the books and on internet, but juist don't see the
        > > probleem.
        > > TI@
        > > Wen
        > >
        > >
        > > int klantNr, access;
        > > Client MutatieRec, MasterRec;
        > > OpenBestand(acc ess);
        > >
        > > LeesMutatie( MutatieRec, klantNr);
        > > while( MutatieRec.klan tNr !=HV) //HV is defined as 99999
        > > {
        > > cout<<MutatieRe c.klantNr;
        > > LeesMutatie( MutatieRec, klantNr);
        > > }
        > > cin.get();[/color]
        >
        > AFAICT, 'LeesMutatie' has not been defined in this program. Could it
        > be the source why it's not working? Post complete code, and not some
        > fragments that are impossible to validate.
        >
        >[/color]


        Comment

        • Wen

          #5
          Re: while can't go to the 2nd record.

          10001;A;Ahold;K ruidenierstraat 2;1234AA;Zaanda m;123456789;123 4567;M;X
          10002;D;Albert Heijn;Industrie terrein
          5;1111BN;Zaanda m;341259878;125 4444;V;Q
          10003;E;Kruidva t;Holtenbroek 123;2300CB;Schi edam;331215171; 3459770;T;S
          10004;F;Blokker ;Amstelveensweg 331;1111SH;Diem en;812355565;90 02352;M;H
          10005;A;Houtman ;Rotterdamsvaar t 123;3800ET;Goud a;567568456;333 4444;V;P
          10006;E;Visa;Ro dekruisweg 23;1156SH;Dieme n;80335658;7456 891;T;O
          10007;B;Hema;Zi lverkruisweg 523;5056AA;Apel doorn;753356564 ;7456891;T;O


          These are information form the "mutatie.cs v" file.

          "Victor Bazarov" <v.Abazarov@com Acast.net> schreef in bericht
          news:5IoIb.7985 6$VB2.162883@at tbi_s51...[color=blue]
          > "Wen" <Wen.Hoogendijk @hccnet.nl> wrote...[color=green]
          > > Can someone tell me why my while doenst read the second, third....[/color][/color]
          record?[color=blue][color=green]
          > > (This's a part of my code.)
          > > I've looked for it in the books and on internet, but juist don't see the
          > > probleem.
          > > TI@
          > > Wen
          > >
          > >
          > > int klantNr, access;
          > > Client MutatieRec, MasterRec;
          > > OpenBestand(acc ess);
          > >
          > > LeesMutatie( MutatieRec, klantNr);
          > > while( MutatieRec.klan tNr !=HV) //HV is defined as 99999
          > > {
          > > cout<<MutatieRe c.klantNr;
          > > LeesMutatie( MutatieRec, klantNr);
          > > }
          > > cin.get();[/color]
          >
          > AFAICT, 'LeesMutatie' has not been defined in this program. Could it
          > be the source why it's not working? Post complete code, and not some
          > fragments that are impossible to validate.
          >
          >[/color]


          Comment

          • CrayzeeWulf

            #6
            Re: while can't go to the 2nd record.

            Wen wrote:

            [color=blue]
            > void LeesMutatie(Cli ent & MutatieRec, int & klantNr)
            > {
            > ifstream mutatie ("mutatie.csv") ;
            > char puntkomma = ';';
            >[/color]
            This will open the file "mutatie.cs v" again and again every time
            LessMutatie() is called. So you will only read the first record.

            Later,
            --
            CrayzeeWulf

            Comment

            • Wen

              #7
              Re: while can't go to the 2nd record.

              ok, so I'd put the ';' away. I'll try that.
              Thank you.
              Wen

              "CrayzeeWul f" <crayzeewulf@no spam.gnudom.org > schreef in bericht
              news:M4pIb.3447 1$C87.34213@twi ster.socal.rr.c om...[color=blue]
              > Wen wrote:
              >
              >[color=green]
              > > void LeesMutatie(Cli ent & MutatieRec, int & klantNr)
              > > {
              > > ifstream mutatie ("mutatie.csv") ;
              > > char puntkomma = ';';
              > >[/color]
              > This will open the file "mutatie.cs v" again and again every time
              > LessMutatie() is called. So you will only read the first record.
              >
              > Later,
              > --
              > CrayzeeWulf[/color]


              Comment

              • Wen

                #8
                Re: while can't go to the 2nd record.

                I'd try this, but it doens't work neither.
                Regards,
                Wen

                void LeesMutatie(Cli ent & MutatieRec, int& klantNr)
                {
                ifstream mutatie ("mutatie.csv") ;
                char komma;

                mutatie >> MutatieRec.klan tNr >> komma;
                mutatie.getline (MutatieRec.soo rt, 2, komma);
                mutatie.getline (MutatieRec.naa m, 26, komma);
                mutatie.getline (MutatieRec.adr es, 26, komma);
                mutatie.getline (MutatieRec.pos tcode,7, komma);
                mutatie.getline (MutatieRec.pla ats, 16, komma);
                mutatie >> MutatieRec.bank Nr >> komma[color=blue][color=green]
                >> MutatieRec.giro >> komma;[/color][/color]
                mutatie.getline (MutatieRec.mut code, 2, komma);
                mutatie.getline (MutatieRec.tar iefAfspr, 2, komma);
                if (mutatie.eof())
                MutatieRec.klan tNr = HV;
                }
                "CrayzeeWul f" <crayzeewulf@no spam.gnudom.org > schreef in bericht
                news:M4pIb.3447 1$C87.34213@twi ster.socal.rr.c om...[color=blue]
                > Wen wrote:
                >
                >[color=green]
                > > void LeesMutatie(Cli ent & MutatieRec, int & klantNr)
                > > {
                > > ifstream mutatie ("mutatie.csv") ;
                > > char puntkomma = ';';
                > >[/color]
                > This will open the file "mutatie.cs v" again and again every time
                > LessMutatie() is called. So you will only read the first record.
                >
                > Later,
                > --
                > CrayzeeWulf[/color]


                Comment

                • CrayzeeWulf

                  #9
                  Re: while can't go to the 2nd record.

                  Wen wrote:
                  [color=blue]
                  > I'd try this, but it doens't work neither.
                  > Regards,
                  > Wen
                  >
                  > void LeesMutatie(Cli ent & MutatieRec, int& klantNr)
                  > {
                  > ifstream mutatie ("mutatie.csv") ;[/color]
                  Wen,

                  Its not the comma. The problem is that you are creating an ifstream instance
                  named "mutatie" every time you call LessMutatie(). Every the first line of
                  this function is executed, you create an ifstream instance and open the
                  file "mutatie.cs v". At this point, the rest of the function reads the file
                  from the very beginning.

                  In order to fix this, you will have to redesign your code to make sure that
                  you do not open the file on every call to LessMutatie(). For example, you
                  can try opening the file outside LessMutatie() and pass it a reference to
                  an ifstream instance:

                  // -------------------------------------------------------------------------
                  void LeesMutatie(ifs tream& input_stream, Client & MutatieRec, int &klantNr);

                  int main()
                  {
                  int klantNr, access;
                  Client MutatieRec, MasterRec;
                  OpenBestand(acc ess);
                  ifstream mutatie( "mutatie.cs v" ) ;

                  LeesMutatie( mutatie, MutatieRec, klantNr);
                  while( MutatieRec.klan tNr != HV )
                  {
                  cout << MutatieRec.klan tNr << ":" << MutatieRec.soor t << endl ;
                  LeesMutatie( mutatie, MutatieRec, klantNr);
                  }
                  cin.get();
                  }

                  void LeesMutatie(ifs tream& mutatie, Client & MutatieRec, int &klantNr)
                  {
                  char puntkomma = ';';
                  if ( ! mutatie ) {
                  std::cout << "mutatie kaput." << endl ;
                  exit(1) ;
                  }
                  mutatie >> MutatieRec.klan tNr >> puntkomma;
                  mutatie.getline (MutatieRec.soo rt, 2, ';');
                  mutatie.getline (MutatieRec.naa m, 26, ';');
                  mutatie.getline (MutatieRec.adr es, 26, ';');
                  mutatie.getline (MutatieRec.pos tcode,7, ';');
                  mutatie.getline (MutatieRec.pla ats, 16, ';');
                  mutatie >> MutatieRec.bank Nr >> puntkomma;
                  mutatie >> MutatieRec.giro >> puntkomma;
                  mutatie.getline (MutatieRec.mut code, 2, ';');
                  mutatie.getline (MutatieRec.tar iefAfspr, 2, ';');

                  if ( mutatie.eof() )
                  MutatieRec.klan tNr = HV;
                  }
                  // -------------------------------------------------------------------------

                  There are other problems with the original LessMutatie() besides the
                  reopening of "mutatie.cs v". But that is a separate issue that you will have
                  to figure out.

                  --
                  CrayzeeWulf

                  Comment

                  • CrayzeeWulf

                    #10
                    Re: while can't go to the 2nd record.

                    Wen wrote:
                    [color=blue]
                    > I'd try this, but it doens't work neither.
                    > Regards,
                    > Wen
                    >
                    > void LeesMutatie(Cli ent & MutatieRec, int& klantNr)
                    > {
                    > ifstream mutatie ("mutatie.csv") ;[/color]
                    Wen,

                    Its not the comma. The problem is that you are creating an ifstream instance
                    named "mutatie" every time you call LessMutatie(). Every time the first line
                    of this function is executed, you create an ifstream instance and open the
                    file "mutatie.cs v". At this point, the rest of the function reads the file
                    from the very beginning.

                    In order to fix this, you will have to redesign your code to make sure that
                    you do not open the file on every call to LessMutatie(). For example, you
                    can try opening the file outside LessMutatie() and pass it a reference to
                    an ifstream instance:

                    // -------------------------------------------------------------------------
                    void LeesMutatie(ifs tream& input_stream, Client & MutatieRec, int &klantNr);

                    int main()
                    {
                    int klantNr, access;
                    Client MutatieRec, MasterRec;
                    OpenBestand(acc ess);
                    ifstream mutatie( "mutatie.cs v" ) ;

                    LeesMutatie( mutatie, MutatieRec, klantNr);
                    while( MutatieRec.klan tNr != HV )
                    {
                    cout << MutatieRec.klan tNr << ":" << MutatieRec.soor t << endl ;
                    LeesMutatie( mutatie, MutatieRec, klantNr);
                    }
                    cin.get();
                    }

                    void LeesMutatie(ifs tream& mutatie, Client & MutatieRec, int &klantNr)
                    {
                    char puntkomma = ';';
                    if ( ! mutatie ) {
                    std::cout << "mutatie kaput." << endl ;
                    exit(1) ;
                    }
                    mutatie >> MutatieRec.klan tNr >> puntkomma;
                    mutatie.getline (MutatieRec.soo rt, 2, ';');
                    mutatie.getline (MutatieRec.naa m, 26, ';');
                    mutatie.getline (MutatieRec.adr es, 26, ';');
                    mutatie.getline (MutatieRec.pos tcode,7, ';');
                    mutatie.getline (MutatieRec.pla ats, 16, ';');
                    mutatie >> MutatieRec.bank Nr >> puntkomma;
                    mutatie >> MutatieRec.giro >> puntkomma;
                    mutatie.getline (MutatieRec.mut code, 2, ';');
                    mutatie.getline (MutatieRec.tar iefAfspr, 2, ';');

                    if ( mutatie.eof() )
                    MutatieRec.klan tNr = HV;
                    }
                    // -------------------------------------------------------------------------

                    There are other problems with the original LessMutatie() besides the
                    reopening of "mutatie.cs v". But that is a separate issue that you will have
                    to figure out.

                    --
                    CrayzeeWulf

                    Comment

                    • Thierry Miceli

                      #11
                      Re: while can't go to the 2nd record.

                      >[color=blue]
                      > void LeesMutatie(Cli ent & MutatieRec, int & klantNr)
                      > {
                      > ifstream mutatie ("mutatie.csv") ;[/color]

                      mutatie.cvs file is reopened on each call of LeesMutatie. Thus you read only
                      the begining of the file on each call.
                      You can fix the problem by initializing the input file stream in your main
                      and passing the ifstream as an argument to LeesMutatie as follow:

                      void LeesMutatie(con st ifstream& mutatie, Client & MutatieRec, int &
                      klantNr);

                      int main()
                      {

                      // Some code....

                      ifstream mutatie ("mutatie.csv") ;
                      if ( !mutatie )
                      {
                      // Error could not open file!!!
                      // ...do something
                      }

                      while( !mutatie.eof() ) // You can now test for end of file here
                      {
                      LeesMutatie( mutatie, MutatieRec, klantNr);
                      cout<<MutatieRe c.klantNr;
                      }

                      //...
                      }

                      void LeesMutatie(con st ifstream& mutatie , Client & MutatieRec, int &
                      klantNr)
                      {
                      // Remove previous mutatie definition and initialization
                      // and use mutatie
                      }





                      Thierry


                      Comment

                      • Victor Bazarov

                        #12
                        Re: while can't go to the 2nd record.

                        "Wen" <Wen.Hoogendijk @hccnet.nl> wrote...[color=blue]
                        > I'd try this, but it doens't work neither.
                        > Regards,
                        > Wen
                        >
                        > void LeesMutatie(Cli ent & MutatieRec, int& klantNr)
                        > {
                        > ifstream mutatie ("mutatie.csv") ;[/color]

                        Open this file once, outside this function, and pass it (by
                        reference) into this function, otherwise every time this func
                        returns the 'mutatie' stream gets _CLOSED_ and every time
                        you call this function again, it _REOPENS_ the stream. And,
                        of course, it reads the very first record again.

                        Do you understand what we are talking about?


                        Comment

                        • Chris Mantoulidis

                          #13
                          Re: while can't go to the 2nd record.

                          [snip some other code]
                          [color=blue]
                          > void LeesMutatie(con st ifstream& mutatie , Client & MutatieRec, int &[/color]

                          ^^^^^ not const!!! you're reading the file, so it
                          is changed and I don't think it'll compile. do: ifstream & mutatie...

                          [snip]

                          Comment

                          • Wen

                            #14
                            Re: while can't go to the 2nd record.

                            Thank you so much for help!

                            I'd try your code, it also show 1 record after that comes the "mutatie
                            kaput." ,
                            and without this :
                            if ( ! mutatie ) {
                            std::cout << "mutatie kaput." << endl ;
                            exit(1) ;
                            }

                            it shows 10000 times the first record.
                            How come?
                            Regards
                            Wen


                            "CrayzeeWul f" <crayzeewulf@no spam.gnudom.org > schreef in bericht
                            news:pIpIb.3349 9$Vs3.9538@twis ter.socal.rr.co m...[color=blue]
                            > Wen wrote:
                            >[color=green]
                            > > I'd try this, but it doens't work neither.
                            > > Regards,
                            > > Wen
                            > >
                            > > void LeesMutatie(Cli ent & MutatieRec, int& klantNr)
                            > > {
                            > > ifstream mutatie ("mutatie.csv") ;[/color]
                            > Wen,
                            >
                            > Its not the comma. The problem is that you are creating an ifstream[/color]
                            instance[color=blue]
                            > named "mutatie" every time you call LessMutatie(). Every time the first[/color]
                            line[color=blue]
                            > of this function is executed, you create an ifstream instance and open the
                            > file "mutatie.cs v". At this point, the rest of the function reads the file
                            > from the very beginning.
                            >
                            > In order to fix this, you will have to redesign your code to make sure[/color]
                            that[color=blue]
                            > you do not open the file on every call to LessMutatie(). For example, you
                            > can try opening the file outside LessMutatie() and pass it a reference to
                            > an ifstream instance:
                            >
                            >[/color]
                            // -------------------------------------------------------------------------[color=blue]
                            > void LeesMutatie(ifs tream& input_stream, Client & MutatieRec, int[/color]
                            &klantNr);[color=blue]
                            >
                            > int main()
                            > {
                            > int klantNr, access;
                            > Client MutatieRec, MasterRec;
                            > OpenBestand(acc ess);
                            > ifstream mutatie( "mutatie.cs v" ) ;
                            >
                            > LeesMutatie( mutatie, MutatieRec, klantNr);
                            > while( MutatieRec.klan tNr != HV )
                            > {
                            > cout << MutatieRec.klan tNr << ":" << MutatieRec.soor t << endl ;
                            > LeesMutatie( mutatie, MutatieRec, klantNr);
                            > }
                            > cin.get();
                            > }
                            >
                            > void LeesMutatie(ifs tream& mutatie, Client & MutatieRec, int &klantNr)
                            > {
                            > char puntkomma = ';';
                            > if ( ! mutatie ) {
                            > std::cout << "mutatie kaput." << endl ;
                            > exit(1) ;
                            > }
                            > mutatie >> MutatieRec.klan tNr >> puntkomma;
                            > mutatie.getline (MutatieRec.soo rt, 2, ';');
                            > mutatie.getline (MutatieRec.naa m, 26, ';');
                            > mutatie.getline (MutatieRec.adr es, 26, ';');
                            > mutatie.getline (MutatieRec.pos tcode,7, ';');
                            > mutatie.getline (MutatieRec.pla ats, 16, ';');
                            > mutatie >> MutatieRec.bank Nr >> puntkomma;
                            > mutatie >> MutatieRec.giro >> puntkomma;
                            > mutatie.getline (MutatieRec.mut code, 2, ';');
                            > mutatie.getline (MutatieRec.tar iefAfspr, 2, ';');
                            >
                            > if ( mutatie.eof() )
                            > MutatieRec.klan tNr = HV;
                            > }
                            >[/color]
                            // -------------------------------------------------------------------------[color=blue]
                            >
                            > There are other problems with the original LessMutatie() besides the
                            > reopening of "mutatie.cs v". But that is a separate issue that you will[/color]
                            have[color=blue]
                            > to figure out.
                            >
                            > --
                            > CrayzeeWulf[/color]


                            Comment

                            • Thierry Miceli

                              #15
                              Re: while can't go to the 2nd record.

                              > > void LeesMutatie(con st ifstream& mutatie , Client & MutatieRec, int &[color=blue]
                              >
                              > ^^^^^ not const!!! you're reading the file, so it
                              > is changed and I don't think it'll compile. do: ifstream & mutatie...
                              >
                              > [snip][/color]
                              Your are right, my mistake.


                              Comment

                              Working...