-
Entering a SPACE
i am trying to get my program to read a SPACE, so i can validate a postcode. this is the code below but wont read the space properly.
can anyone help?
Code:
bool isValidPostcode(char postcode[])
{
int errorcounter = 0;
int spaceCheck = 0;
if(isalpha(post1[0]))
{
cout << "this is correct" <<endl;
}
else
{
cout << "this is incorrect"<<endl;
errorcounter++;
}
if(isalpha(post1[1]))
{
cout << "this is correct"<<endl;
}
else
{
cout << "this is incorrect"<<endl;
errorcounter++;
}
if(isdigit(post1[2]))
{
cout << "this is correct"<<endl;
}
else
{
cout << "this is incorrect"<<endl;
errorcounter++;
}
spaceCheck = ((int)post1[3]);
spaceCheck = 0;
if (spaceCheck == 32)
{
cout <<"this is correct" <<endl;
}
else
{
cout <<"this is incorrect" <<endl;
errorcounter++;
}
// no space so check remainding 3 chars
if (isdigit(post1[4]))
{
cout << "this is correct" <<endl;
}
else
{
cout << "this is correct"<<endl;
errorcounter++;
}
if (isalpha(post1[5]))
{
cout << "this is incorrect" <<endl;
}
else
{
cout << "this is correct" <<endl;
errorcounter++;
}
if (isalpha(post1[6]))
{
cout << "this is incorrect" <<endl;
}
else
{
cout << "this is correct" <<endl;
errorcounter++;
}
if (errorcounter ==0)
{
cout << "the post code it VALID";
return true;
}
else
{
cout <<"the postcode is INVALID please enter in again";
return false;
-
Code:
spaceCheck = ((int)post1[3]);
spaceCheck = 0;
if (spaceCheck == 32)
{
cout <<"this is correct" <<endl;
}
first things first: here you first assign spaceCheck to be the integer value of what is stored in post1[3]. After that you assign spaceCheck to be 0, and thus the following if will always be false.
Second, how do you know that a space always has the value 32? That will not always be the case.
A better way to do it would be
Code:
if (post1[3] == ' ')
{
cout <<"this is correct" <<endl;
}
Here I test whatever you have in post1[3] to see if it is a space. This is not dependant on what computer you run this on.
-
i have done that mate and the proram seems to go mad and mess up :(
you know the reason for this?
Thanks
-
Now when i take a second look at your code I see something fishy. All the time when you check everything you use post1, but the variable you pass to the function is postcode...
-
-
how would i know? I dont know what the rest of the code looks like.