while can't go to the 2nd record.

W

Wen

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(access);

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

Victor Bazarov

Wen said:
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(access);

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

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.
 
C

CrayzeeWulf

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

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

if ( MutatieRec.klantNr == HV ) {
cout << "Mutatie.klantNr is equal to HV" << endl ;
}

OpenBestand(access);

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

Wen

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(Client & MutatieRec, int & klantNr);

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

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

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

mutatie >> MutatieRec.klantNr >> puntkomma;
mutatie.getline(MutatieRec.soort, 2, ';');
mutatie.getline(MutatieRec.naam, 26, ';');
mutatie.getline(MutatieRec.adres, 26, ';');
mutatie.getline(MutatieRec.postcode,7, ';');
mutatie.getline(MutatieRec.plaats, 16, ';');
mutatie >> MutatieRec.bankNr >> puntkomma;
mutatie >> MutatieRec.giro >> puntkomma;
mutatie.getline(MutatieRec.mutcode, 2, ';');
mutatie.getline(MutatieRec.tariefAfspr, 2, ';');
if (mutatie.eof())
MutatieRec.klantNr = HV;
}

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

constant.open("constant.csv");
if(constant.fail())
{
cerr << "Het constantenbestand kon niet worden geopend" << endl;
access = 0;
}

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

mutatie.open("mutatie.csv");
if(mutatie.fail())
{
cerr << "Het mutatiebestand kon niet worden geopend" << endl;
access = 0;
}
}
 
W

Wen

10001;A;Ahold;Kruidenierstraat 2;1234AA;Zaandam;123456789;1234567;M;X
10002;D;Albert Heijn;Industrieterrein
5;1111BN;Zaandam;341259878;1254444;V;Q
10003;E;Kruidvat;Holtenbroek 123;2300CB;Schiedam;331215171;3459770;T;S
10004;F;Blokker;Amstelveensweg 331;1111SH;Diemen;812355565;9002352;M;H
10005;A;Houtman;Rotterdamsvaart 123;3800ET;Gouda;567568456;3334444;V;P
10006;E;Visa;Rodekruisweg 23;1156SH;Diemen;80335658;7456891;T;O
10007;B;Hema;Zilverkruisweg 523;5056AA;Apeldoorn;753356564;7456891;T;O


These are information form the "mutatie.csv" file.
 
C

CrayzeeWulf

Wen wrote:

void LeesMutatie(Client & MutatieRec, int & klantNr)
{
ifstream mutatie ("mutatie.csv");
char puntkomma = ';';
This will open the file "mutatie.csv" again and again every time
LessMutatie() is called. So you will only read the first record.

Later,
 
T

Thierry Miceli

void LeesMutatie(Client & MutatieRec, int & klantNr)
{
ifstream mutatie ("mutatie.csv");

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(const 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<<MutatieRec.klantNr;
}

//...
}

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





Thierry
 
W

Wen

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

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

mutatie >> MutatieRec.klantNr >> komma;
mutatie.getline(MutatieRec.soort, 2, komma);
mutatie.getline(MutatieRec.naam, 26, komma);
mutatie.getline(MutatieRec.adres, 26, komma);
mutatie.getline(MutatieRec.postcode,7, komma);
mutatie.getline(MutatieRec.plaats, 16, komma);
mutatie >> MutatieRec.bankNr >> kommamutatie.getline(MutatieRec.mutcode, 2, komma);
mutatie.getline(MutatieRec.tariefAfspr, 2, komma);
if (mutatie.eof())
MutatieRec.klantNr = HV;
}
 
C

CrayzeeWulf

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

void LeesMutatie(Client & MutatieRec, int& klantNr)
{
ifstream mutatie ("mutatie.csv");
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.csv". 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(ifstream& input_stream, Client & MutatieRec, int &klantNr);

int main()
{
int klantNr, access;
Client MutatieRec, MasterRec;
OpenBestand(access);
ifstream mutatie( "mutatie.csv" ) ;

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

void LeesMutatie(ifstream& mutatie, Client & MutatieRec, int &klantNr)
{
char puntkomma = ';';
if ( ! mutatie ) {
std::cout << "mutatie kaput." << endl ;
exit(1) ;
}
mutatie >> MutatieRec.klantNr >> puntkomma;
mutatie.getline(MutatieRec.soort, 2, ';');
mutatie.getline(MutatieRec.naam, 26, ';');
mutatie.getline(MutatieRec.adres, 26, ';');
mutatie.getline(MutatieRec.postcode,7, ';');
mutatie.getline(MutatieRec.plaats, 16, ';');
mutatie >> MutatieRec.bankNr >> puntkomma;
mutatie >> MutatieRec.giro >> puntkomma;
mutatie.getline(MutatieRec.mutcode, 2, ';');
mutatie.getline(MutatieRec.tariefAfspr, 2, ';');

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

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

CrayzeeWulf

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

void LeesMutatie(Client & MutatieRec, int& klantNr)
{
ifstream mutatie ("mutatie.csv");
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.csv". 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(ifstream& input_stream, Client & MutatieRec, int &klantNr);

int main()
{
int klantNr, access;
Client MutatieRec, MasterRec;
OpenBestand(access);
ifstream mutatie( "mutatie.csv" ) ;

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

void LeesMutatie(ifstream& mutatie, Client & MutatieRec, int &klantNr)
{
char puntkomma = ';';
if ( ! mutatie ) {
std::cout << "mutatie kaput." << endl ;
exit(1) ;
}
mutatie >> MutatieRec.klantNr >> puntkomma;
mutatie.getline(MutatieRec.soort, 2, ';');
mutatie.getline(MutatieRec.naam, 26, ';');
mutatie.getline(MutatieRec.adres, 26, ';');
mutatie.getline(MutatieRec.postcode,7, ';');
mutatie.getline(MutatieRec.plaats, 16, ';');
mutatie >> MutatieRec.bankNr >> puntkomma;
mutatie >> MutatieRec.giro >> puntkomma;
mutatie.getline(MutatieRec.mutcode, 2, ';');
mutatie.getline(MutatieRec.tariefAfspr, 2, ';');

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

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

Victor Bazarov

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

void LeesMutatie(Client & MutatieRec, int& klantNr)
{
ifstream mutatie ("mutatie.csv");

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?
 
C

Chris Mantoulidis

[snip some other code]
void LeesMutatie(const ifstream& mutatie , Client & MutatieRec, int &

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

[snip]
 
W

Wen

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


CrayzeeWulf said:
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.csv". 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(ifstream& input_stream, Client & MutatieRec, int &klantNr);

int main()
{
int klantNr, access;
Client MutatieRec, MasterRec;
OpenBestand(access);
ifstream mutatie( "mutatie.csv" ) ;

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

void LeesMutatie(ifstream& mutatie, Client & MutatieRec, int &klantNr)
{
char puntkomma = ';';
if ( ! mutatie ) {
std::cout << "mutatie kaput." << endl ;
exit(1) ;
}
mutatie >> MutatieRec.klantNr >> puntkomma;
mutatie.getline(MutatieRec.soort, 2, ';');
mutatie.getline(MutatieRec.naam, 26, ';');
mutatie.getline(MutatieRec.adres, 26, ';');
mutatie.getline(MutatieRec.postcode,7, ';');
mutatie.getline(MutatieRec.plaats, 16, ';');
mutatie >> MutatieRec.bankNr >> puntkomma;
mutatie >> MutatieRec.giro >> puntkomma;
mutatie.getline(MutatieRec.mutcode, 2, ';');
mutatie.getline(MutatieRec.tariefAfspr, 2, ';');

if ( mutatie.eof() )
MutatieRec.klantNr = HV;
}
// -------------------------------------------------------------------------
 
T

Thierry Miceli

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

[snip]
Your are right, my mistake.
 
M

Martijn Lievaart

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<<MutatieRec.klantNr;
}

I wouldn't do this. You can only detect eof() inside LeesMutatie by seeing
that a read failed.

Do something like:

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

while( LeesMutatie( mutatie, MutatieRec, klantNr) ) {
cout<<MutatieRec.klantNr;
}

And modify LeesMutatie accordingly (it can simply return the stream object
reference).

HTH,
M4
 
K

Kees Hoogendijk

Happy New Year, everyone.

Thank you once more. Finally I understand what you are talking about. And
with your help I've solved more my problems.

Best Regards,

Wen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top