 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kees Hoogendijk Guest
|
Posted: Sat Dec 27, 2003 1:58 pm Post subject: something wrong in function, but what? |
|
|
Hi everyone,
I've tried for a few day to make a function to check out the zipcode. But it
just doens't work. Can someone help me?
In the zipcode, the figures must be larger than 1000 en the charaters may
not be empty, but when the letter kind is "A", "D" of "F" then the zipcode
may be "0000 ".
TI@,
Wen
#include <iostream>
#include <string>
struct Zipcode
{
int cijfer;
char letter[3];
};
int PostcodeContr(int& juist);
using namespace std;
int main()
{
char soort[2];
Zipcode code;
int goed;
cout<
cin>>code.cijfer;
cin.getline(code.letter,3);
cout<<"soort :"<
cin>>soort;
PostcodeContr(goed);
cout<
cin.get();
cin.get();
}
int PostcodeContr(int & juist)
{
Zipcode pcode;
char soort [2];
//1 is right, 2 is wrong.
if(pcode.cijfer > 1000 && strcmp(pcode.letter," ")!=0)
return juist = 1;
//als soort is gelijk aan A of D of F en postcode="0000 "
else if((stricmp(soort, "A") == 0 || stricmp(soort, "D") == 0
| Quote: | | stricmp(soort, "F") == 0 )
&& pcode.cijfer == 0 && strcmp(pcode.letter," ")==0) |
return juist = 1;
else
return juist = 2;
}
|
|
| Back to top |
|
 |
Dan Cernat Guest
|
Posted: Sat Dec 27, 2003 3:31 pm Post subject: Re: something wrong in function, but what? |
|
|
"Kees Hoogendijk" <nieuwskees (AT) haalditweg-hcopleidingen (DOT) nl> wrote
| Quote: | Hi everyone,
I've tried for a few day to make a function to check out the zipcode. But
it
just doens't work. Can someone help me?
In the zipcode, the figures must be larger than 1000 en the charaters may
not be empty, but when the letter kind is "A", "D" of "F" then the
zipcode
may be "0000 ".
TI@,
Wen
#include <iostream
#include
struct Zipcode
{
int cijfer;
char letter[3];
};
int PostcodeContr(int& juist);
using namespace std;
int main()
{
char soort[2];
Zipcode code;
int goed;
cout<
cin>>code.cijfer;
cin.getline(code.letter,3);
cout<<"soort :"<
cin>>soort;
PostcodeContr(goed);
|
you pass above goed which is an unitialised integer. shouldn't your function
accept a Zipcode?
like:
goed = PostcodeContr(code);
| Quote: | cout<
cin.get();
cin.get();
}
int PostcodeContr(int & juist)
and according to what I suggested, the function should be: |
int PostcodeContr(Zipcode & pcode)
| Quote: | {
Zipcode pcode;
^^^^^^^^^^^^^^^^ don't need this, you pass in the zipcode |
however, the way it was before, you were using an unitialised pcode
I stop here.
| Quote: | char soort [2];
//1 is right, 2 is wrong.
if(pcode.cijfer > 1000 && strcmp(pcode.letter," ")!=0)
return juist = 1;
//als soort is gelijk aan A of D of F en postcode="0000 "
else if((stricmp(soort, "A") == 0 || stricmp(soort, "D") == 0
|| stricmp(soort, "F") == 0 )
&& pcode.cijfer == 0 && strcmp(pcode.letter," ")==0)
return juist = 1;
else
return juist = 2;
}
|
Dan
|
|
| Back to top |
|
 |
Kees Hoogendijk Guest
|
Posted: Sat Dec 27, 2003 4:04 pm Post subject: Re: something wrong in function, but what? |
|
|
Hi Dan,
Thank You Very Much!!
It works now.
Happy New Year!
Wen.
"Dan Cernat" <cernat (AT) dan (DOT) com> schreef in bericht
news:vur8robrf7ace6 (AT) corp (DOT) supernews.com...
| Quote: |
"Kees Hoogendijk" <nieuwskees (AT) haalditweg-hcopleidingen (DOT) nl> wrote in
message
news:bsk3b8$knv$1 (AT) news (DOT) cistron.nl...
Hi everyone,
I've tried for a few day to make a function to check out the zipcode.
But
it
just doens't work. Can someone help me?
In the zipcode, the figures must be larger than 1000 en the charaters
may
not be empty, but when the letter kind is "A", "D" of "F" then the
zipcode
may be "0000 ".
TI@,
Wen
#include <iostream
#include
struct Zipcode
{
int cijfer;
char letter[3];
};
int PostcodeContr(int& juist);
using namespace std;
int main()
{
char soort[2];
Zipcode code;
int goed;
cout<
cin>>code.cijfer;
cin.getline(code.letter,3);
cout<<"soort :"<
cin>>soort;
PostcodeContr(goed);
you pass above goed which is an unitialised integer. shouldn't your
function
accept a Zipcode?
like:
goed = PostcodeContr(code);
cout<
cin.get();
cin.get();
}
int PostcodeContr(int & juist)
and according to what I suggested, the function should be:
int PostcodeContr(Zipcode & pcode)
{
Zipcode pcode;
^^^^^^^^^^^^^^^^ don't need this, you pass in the zipcode
however, the way it was before, you were using an unitialised pcode
I stop here.
char soort [2];
//1 is right, 2 is wrong.
if(pcode.cijfer > 1000 && strcmp(pcode.letter," ")!=0)
return juist = 1;
//als soort is gelijk aan A of D of F en postcode="0000 "
else if((stricmp(soort, "A") == 0 || stricmp(soort, "D") == 0
|| stricmp(soort, "F") == 0 )
&& pcode.cijfer == 0 && strcmp(pcode.letter," ")==0)
return juist = 1;
else
return juist = 2;
}
Dan
|
|
|
| Back to top |
|
 |
Martijn Lievaart Guest
|
Posted: Tue Dec 30, 2003 3:42 pm Post subject: Re: something wrong in function, but what? |
|
|
On Sat, 27 Dec 2003 14:58:22 +0100, Kees Hoogendijk wrote:
| Quote: | #include <iostream
#include
struct Zipcode
{
int cijfer;
char letter[3];
};
int PostcodeContr(int& juist);
using namespace std;
int main()
{
char soort[2];
Zipcode code;
int goed;
cout<
cin>>code.cijfer;
cin.getline(code.letter,3);
cout<<"soort :"<
cin>>soort;
|
Oopsa, this will read into a two character array, meaning you can store
one character and a terminating zero. Probably not what you want!
Even if you make soort larger ([3]), the user can still enter more than
two characters. Potentially (very high potential) crashing your program.
Why not use std::string? It was designed to get around this kind of
problems. I personally also always read complete lines and only the start
to parse them, so I personally never use << on input streams (except
stringstreams and there I use it often).
Groetjes,
M4
|
|
| Back to top |
|
 |
|
|
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
|
|