Entering a SPACE

This is a discussion on Entering a SPACE within the C++ Programming forums, part of the General Programming Boards category; i am trying to get my program to read a SPACE, so i can validate a postcode. this is the ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    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;

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    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.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i have done that mate and the proram seems to go mad and mess up

    you know the reason for this?

    Thanks

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    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...
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  5. #5
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    Does that really matter?

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    how would i know? I dont know what the rest of the code looks like.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 02:43 PM
  2. disk space mysteriously gone
    By evader in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2004, 12:30 PM
  3. Replies: 12
    Last Post: 05-17-2003, 05:58 AM
  4. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 03:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21