Thread: Validate area codes within a list of area codes

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    11

    Validate area codes within a list of area codes

    I need help with my code. When I have i set to zero and I enter the first area code listed it says that it is a valid area code. However when I enter the next area code listed it says that it is an invalid area code, but it should actually be valid.

    Can someone help me figure out what I need to change so that when I enter each of the four area codes listed it says that they are valid area codes and when I enter any other area code it says that it is invalid. Thank you

    Code:
     9 #include <stdio.h>
    10 
    11 int main ()
    12 {
    13 
    14    
    15    int codes [4] = {303, 970, 720, 719};
    16    int codeToFind = 0;
    17    int codeToCheck = 0;
    18    int i = 0;
    19    
    20    
    21    printf("Enter area code to validate: ");
    22    scanf("%d", &codeToFind);
    23    
    24    for(i = 1; i < 4; i++)
    25    {
    26       
    27    if(codeToFind == codes[i])
    28    {
    29        printf("Valid Area Code");
    30    }
    31    else
    32    {
    33       printf("Invalid Area Code");
    34    }
    35       
    36    return 0;
    37 }
    38 }
    
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just delete this line and try again, starting at i = 0

    33 printf("Invalid Area Code");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    Ok, I tried that and now it doesn't print anything after entering an area code. When I set i = 0 it will give me the correct output for the first area code listed which is 303, but for all of the others listed it says they are invalid. I think I have to change something with my for loop, but I haven't quite figured it out yet

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you write a function like this:
    Code:
    int checkValidAreaCode(int areaCode)
    {
        static const int validAreaCodes[] = {303, 970, 720, 719};
        /* ...
           loop to check if areaCode is equal to any of the validAreaCodes elements
           ...
        */
    }
    Then call it in your main function like this:
    Code:
    int main(void)
    {
        int areaCode = 0;
        printf("Enter area code to validate: ");
        if (scanf("%d", &areaCode) == 1)
        {
            if (checkValidAreaCode(areaCode))
            {
                /* area code is valid */
            }
            else
            {
                /* area code is not valid */
            }
        }
        else
        {
            /* input error */
        }
    
        return 0;
    }
    The key is that at the moment, your loop to check for the validity of the area code keeps on printing on each iteration of the loop. With this checkValidAreaCode function, you would return 1 if there is a match, otherwise at the end of the function you will return 0. As such, the function will return a value without printing anything; the printing will be handled in the main function instead.

    By the way, notice that my code has consistent indentation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    I tried that too and it is still not working, now it says every number I put in is a valid area code and the exit code for the process is 15 rather than zero.

    Thank you for reminding me of the indentations

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    Here is my current code and it is pretty messed up. When I compile it tells me that line 29 is not a function. I am not sure if this is the best way to accomplish the task, I am very new to this.

    I don't really know how to write my own function very well, which is why I was not doing that with the previous code. I know the previous code can work with a few tweaks, but I haven't been able to figure out how to make it work yet.

    Thank you for any assistance

    Code:
     9 #include <stdio.h>
    10 
    11 int validCodes(int codes);
    12 
    13 int main ()
    14 {
    15 
    16    int codes [4] = {303, 720, 719, 970};
    17    int codeToFind = 0;
    18    int codeToCheck = 0;
    19    int i = 0;
    20    int used = 4;
    21    int validCode = 0;
    22    
    23    
    24    
    25    printf("Enter area code to validate: ");
    26     if (scanf("%d", &validCode) == 1)
    27       {
    28       
    29           if (validCode(codes));
    30           {
    31              printf("Area code is valid"); 
    32           }
    33           else
    34           {
    35             printf("Area code is not valid");
    36           }
    37       }
    38     else
    39     {
    40         printf("please try again");
    41     }
    42  
    43     return 0;
    44 }
    45 
    46       
    47         
    48 int validCodes(int codes)
    49 {
    50    int validCodes[] = {303, 970, 720, 719};
    51    for(i = 0; i < 4; i++)
    52    ans = codes[i]
    53    return ans;
    54 }
    
    

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Staja24
    Here is my current code and it is pretty messed up.
    Indeed. You need to indent it properly and consistently. If tabs are troubling you, I suggest using four spaces per indent level. Copy and paste the code as-is into [code][/code] bbcode tags. The forum software will then maintain your indentation (though there is a minor bug with that, but don't worry) and add line numbers and syntax highlighting. You do not need to copy and paste the line numbers yourself.

    Quote Originally Posted by Staja24
    When I compile it tells me that line 29 is not a function.
    On the line numbered 21, you declared validCode to be a variable of type int:
    Code:
    int validCode = 0;
    Therefore, in that context, validCode is not a function. Yes, you did define a function named validCode later, but that is a different context. If you wanted to use that function, you should have either forward declared it (usually by declaring the function prototype) before the definition of the main function, or move the definition of validCode to be before the main function, then don't declare a variable named validCode in the main function.

    Quote Originally Posted by Staja24
    I am not sure if this is the best way to accomplish the task, I am very new to this.
    The code that I outlined in post #4 is what I consider to be a good, if not the best, way to accomplish this task.

    Quote Originally Posted by Staja24
    I don't really know how to write my own function very well, which is why I was not doing that with the previous code.
    Do you understand what I wrote in post #4?

    Quote Originally Posted by Staja24
    I know the previous code can work with a few tweaks, but I haven't been able to figure out how to make it work yet.
    It is true that you do not need to write a helper function: you could use a boolean variable instead to record whether or not the area code entered is valid or invalid. Either way, the idea is to write a loop to check if the area code entered is equal to any of the valid area codes, and if so, print it as valid, otherwise to print it as invalid. You should not be printing on every iteration of the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    I thought I replied yesterday, but I guess it didn't post.

    I do not understand everything about post 4, I am not very good at writing functions or calling them in my program and no matter how I change things around i still get the same error that it is not a function.

    I do get that I should not be printing on every iteration of the loop, but I still haven't figured out how to fix this issue. So needless to say my program is still printing every entry as valid when they are not all valid

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should start by posting your latest code, along with any compiler warnings/errors you're receiving.

    You should also post code that is compilable ... that is, don't include line numbers (that is done for you automatically by the forum software).

    Make sure your code is neatly formatted and indented. As a courtesy, this is what it should look like:

    Code:
    #include <stdio.h>
    
    int validCodes(int codes);
    
    int main ()
    {
        int codes [4] = {303, 720, 719, 970};
        int codeToFind = 0;
        int codeToCheck = 0;
        int i = 0;
        int used = 4;
        int validCode = 0;
    
        printf("Enter area code to validate: ");
    
        if (scanf("%d", &validCode) == 1)
        {
            if (validCode(codes));
            {
                printf("Area code is valid");
            }
            else
            {
                printf("Area code is not valid");
            }
        }
        else
        {
            printf("please try again");
        }
    
        return 0;
    }
    
    int validCodes(int codes)
    {
        int validCodes[] = {303, 970, 720, 719};
    
        for(i = 0; i < 4; i++)
            ans = codes[i]
    
        return ans;
    }
    The first mistake is a basic one ... on line 18, you end the "if()" with a semi-colon. That is not what you want.

    In C, a semi-colon by itself is considered a valid statement (referred to as a "null statement"). It essentially means "do nothing". That is what you get by placing a semi-colon after the "if".

    For instance, in the following code, you would expect the print statement not to print:

    Code:
    int x = 5;
    
    if(x > 10);
        printf("x is %d\n",x);
    But it does print. Because of the misplaced semi-colon, the code is really interpreted as this:

    Code:
    int x = 5;
    
    if(x > 10)
        ;
    printf("x is %d\n",x);
    Regardless of the value of "x", the print statement is executed. If "x" is not greater than 10, the "if" fails and the rest of the code (including the print) occurs. If "x" is greater than 10, then the "if" is true ... but the null statement says nothing is to occur ... and the rest of the code (i.e. the print) still happens.

    Your code has still other errors.

    You're using the variables "i" and "ans" in the "validCodes()" function, but you don't declare those variables in that function.

    If you pass an array to a function, you need to declare it as an array (or a pointer) in the function parameter list.

    And in your "validCodes()" function, you're not using the "validCodes" array for anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Area of Circle Without Pi
    By Jhernandez860 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2013, 01:24 PM
  2. area of a circle
    By wise_ron in forum C Programming
    Replies: 2
    Last Post: 10-02-2006, 03:15 PM
  3. anyone have linked-list sample codes?
    By ling in forum C++ Programming
    Replies: 3
    Last Post: 07-03-2002, 02:24 PM
  4. area function using xy
    By rake in forum C++ Programming
    Replies: 10
    Last Post: 03-26-2002, 01:53 PM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM

Tags for this Thread