something wrong in function, but what?

K

Kees Hoogendijk

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<<endl;
cin>>code.cijfer;
cin.getline(code.letter,3);
cout<<"soort :"<<endl;
cin>>soort;

PostcodeContr(goed);
cout<<goed;
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
|| stricmp(soort, "F") == 0 )
&& pcode.cijfer == 0 && strcmp(pcode.letter," ")==0)
return juist = 1;

else
return juist = 2;

}
 
D

Dan Cernat

Kees Hoogendijk said:
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<<endl;
cin>>code.cijfer;
cin.getline(code.letter,3);
cout<<"soort :"<<endl;
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<<goed;
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
 
K

Kees Hoogendijk

Hi Dan,

Thank You Very Much!!
It works now.
Happy New Year!

Wen.

Dan Cernat said:
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<<endl;
cin>>code.cijfer;
cin.getline(code.letter,3);
cout<<"soort :"<<endl;
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<<goed;
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
 
M

Martijn Lievaart

#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<<endl;
cin>>code.cijfer;
cin.getline(code.letter,3);
cout<<"soort :"<<endl;
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
 

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
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top