C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

while can't go to the 2nd record.
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Wen
Guest





PostPosted: Wed Dec 31, 2003 12:04 am    Post subject: while can't go to the 2nd record. Reply with quote



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< LeesMutatie( MutatieRec, klantNr);
}
cin.get();

--

Met vriendelijke groet,

Wen


Back to top
Victor Bazarov
Guest





PostPosted: Wed Dec 31, 2003 12:25 am    Post subject: Re: while can't go to the 2nd record. Reply with quote



"Wen" <Wen.Hoogendijk (AT) hccnet (DOT) nl> wrote...
Quote:
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< 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.



Back to top
CrayzeeWulf
Guest





PostPosted: Wed Dec 31, 2003 12:32 am    Post subject: Re: while can't go to the 2nd record. Reply with quote



Wen wrote:

Quote:
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();
// ---------------------------------------------------------

--
CrayzeeWulf

Back to top
Wen
Guest





PostPosted: Wed Dec 31, 2003 12:37 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

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< 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;
}
}
"Victor Bazarov" news:5IoIb.79856$VB2.162883 (AT) attbi_s51 (DOT) ..
Quote:
"Wen" <Wen.Hoogendijk (AT) hccnet (DOT) nl> wrote...
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< 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.





Back to top
Wen
Guest





PostPosted: Wed Dec 31, 2003 12:47 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

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.

"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> schreef in bericht
news:5IoIb.79856$VB2.162883 (AT) attbi_s51 (DOT) ..
Quote:
"Wen" <Wen.Hoogendijk (AT) hccnet (DOT) nl> wrote...
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< 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.





Back to top
CrayzeeWulf
Guest





PostPosted: Wed Dec 31, 2003 12:51 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

Wen wrote:


Quote:
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,
--
CrayzeeWulf

Back to top
Wen
Guest





PostPosted: Wed Dec 31, 2003 1:10 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

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

"CrayzeeWulf" <crayzeewulf (AT) nospam (DOT) gnudom.org> schreef in bericht
news:M4pIb.34471$C87.34213 (AT) twister (DOT) socal.rr.com...
Quote:
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,
--
CrayzeeWulf



Back to top
Thierry Miceli
Guest





PostPosted: Wed Dec 31, 2003 1:16 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

Quote:

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< }

//...
}

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





Thierry



Back to top
Wen
Guest





PostPosted: Wed Dec 31, 2003 1:19 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

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 >> komma
Quote:
MutatieRec.giro >> komma;
mutatie.getline(MutatieRec.mutcode, 2, komma);

mutatie.getline(MutatieRec.tariefAfspr, 2, komma);
if (mutatie.eof())
MutatieRec.klantNr = HV;
}
"CrayzeeWulf" <crayzeewulf (AT) nospam (DOT) gnudom.org> schreef in bericht
news:M4pIb.34471$C87.34213 (AT) twister (DOT) socal.rr.com...
Quote:
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,
--
CrayzeeWulf



Back to top
CrayzeeWulf
Guest





PostPosted: Wed Dec 31, 2003 1:33 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

Wen wrote:

Quote:
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.

--
CrayzeeWulf

Back to top
CrayzeeWulf
Guest





PostPosted: Wed Dec 31, 2003 1:34 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

Wen wrote:

Quote:
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.

--
CrayzeeWulf

Back to top
Victor Bazarov
Guest





PostPosted: Wed Dec 31, 2003 2:19 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

"Wen" <Wen.Hoogendijk (AT) hccnet (DOT) nl> wrote...
Quote:
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?



Back to top
Chris Mantoulidis
Guest





PostPosted: Wed Dec 31, 2003 7:49 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

[snip some other code]

Quote:
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]

Back to top
Wen
Guest





PostPosted: Wed Dec 31, 2003 9:39 am    Post subject: Re: while can't go to the 2nd record. Reply with quote

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" news:pIpIb.33499$Vs3.9538 (AT) twister (DOT) socal.rr.com...
Quote:
Wen wrote:

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.

--
CrayzeeWulf



Back to top
Thierry Miceli
Guest





PostPosted: Wed Dec 31, 2003 7:11 pm    Post subject: Re: while can't go to the 2nd record. Reply with quote

Quote:
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.




Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.