Thread: 'a' undeclared (first use in this function) Error

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    14

    Question 'a' undeclared (first use in this function) Error

    Hello Programmers, I am trying to make a program that will allow a user to enter their name, from this point, three options should be present for the user to choose, then from that point, two more options should be present. I have if else statements in a switch case and I get the "undeclared" error for the a in the first " if(specificage == a) ". Does anyone know the fix to this, it is the only error I get. I'm new to programming, sorry if I sound like a rookie. Run it if you need, thanks!

    NOTE: I use Code::Blocks.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
        char name[50];
        char ageclass[50];
        char specificage[50];
    
    
        printf("Please enter your name: ");
        fgets(name, 50, stdin);
        printf("Hello %s", name);
    
    
        printf("Are you a:\n");
        printf("1. Minor\n");
        printf("2. Adult\n");
        printf("3. Senior Citizen\n");
        printf("Answer(1, 2, or 3): ");
        fgets(ageclass, 50, stdin);
    
    
        switch (ageclass[50])
        {
            case'1':
            {
                printf("You are between the ages of:\n");
                printf("a. 0-12\n");
                printf("b. 13-18\n");
                printf("Answer(a or b): ");
                fgets(specificage, 50, stdin);
                   if(specificage == a)
                    {
                        printf("You are a young minor.");
                    }
                    else
                    {
                        printf("You are an older minor.");
                    }
            }
    
    
            case'2':
            {
                printf("You are between the ages of:\n");
                printf("a. 19-50\n");
                printf("b. 51-65\n");
                printf("Answer(a or b): ");
                fgets(specificage, 50, stdin);
                    if(specificage == a)
                    {
                        printf("You are a young adult.");
                    }
                    else
                    {
                        printf("You are an older adult.");
                    }
            }
    
    
            case'3':
            {
                printf("You are between the ages of:\n");
                printf("a. 66-90\n");
                printf("b. 91-110\n");
                printf("Answer(a or b): ");
                fgets(specificage, 50, stdin);
                    if(specificage == a)
                    {
                        printf("You are a young senior citizen.");
                    }
                    else
                    {
                        printf("You are an older senior citizen.");
                    }
            }
        }
    
    
        getchar();
        return 0;
    }
    Last edited by critixal; 07-02-2013 at 05:49 PM.

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    14
    Don't reply, issue resolved!

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have two problems. First
    Code:
    if(specificage == a)
    a here is treated as a variable. If you want to check that the user input the letter a, you need to put single quotes around it: 'a'. If you want a string containing one character, the letter a, use double quotes: "a"

    Second, specificage is a char array/string. You can't compare strings with ==. You can compare individual characters in the string/array with ==, or use strcmp/strncmp:
    Code:
    if (strcmp(specificage, "some string") == 0)  // strcmp returns 0 if they're equal
    // or
    if (specificage[3] == 'q')  // check that the 4th (remember, arrays in C start at 0) character is a 'q'
    I'm guessing you were more interested in the second example.

    Two more notes:
    1. fgets leaves the newline character (remember, the user pressed enter after 'a' or 'b'). If I enter "foo" followed by enter, the string fgets gives back contains "foo\n". If you do strcmp, it will not match "foo", since the \n is significant. If you need to get rid of it, use the strchr trick here: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.
    2. The user may enter uppercase 'A' or 'B' for some reason, your program should handle it. Easy enough, use the toupper() or tolower() functions before comparing (remember to #include <ctype.h>)

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    14
    Quote Originally Posted by anduril462 View Post
    You have two problems. First
    Code:
    if(specificage == a)
    a here is treated as a variable. If you want to check that the user input the letter a, you need to put single quotes around it: 'a'. If you want a string containing one character, the letter a, use double quotes: "a"

    Second, specificage is a char array/string. You can't compare strings with ==. You can compare individual characters in the string/array with ==, or use strcmp/strncmp:
    Code:
    if (strcmp(specificage, "some string") == 0)  // strcmp returns 0 if they're equal
    // or
    if (specificage[3] == 'q')  // check that the 4th (remember, arrays in C start at 0) character is a 'q'
    I'm guessing you were more interested in the second example.

    Two more notes:
    1. fgets leaves the newline character (remember, the user pressed enter after 'a' or 'b'). If I enter "foo" followed by enter, the string fgets gives back contains "foo\n". If you do strcmp, it will not match "foo", since the \n is significant. If you need to get rid of it, use the strchr trick here: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.
    2. The user may enter uppercase 'A' or 'B' for some reason, your program should handle it. Easy enough, use the toupper() or tolower() functions before comparing (remember to #include <ctype.h>)
    Oh, actually this helped a lot, I will make these changes, thank you tons!
    Could you check out my other post, the more recent one, thanks! It involves the same program.
    Last edited by critixal; 07-02-2013 at 06:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: ‘numberOfElements’ undeclared
    By leahknowles in forum C Programming
    Replies: 6
    Last Post: 03-23-2011, 08:58 AM
  2. Replies: 7
    Last Post: 07-10-2010, 04:37 AM
  3. Replies: 13
    Last Post: 11-13-2008, 09:40 AM
  4. error: undeclared (need some help)
    By Nadril in forum C++ Programming
    Replies: 17
    Last Post: 02-22-2008, 12:59 PM
  5. Compiler error error C2065: '_beginthreadex; : undeclared identifier
    By Roaring_Tiger in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2003, 01:54 AM

Tags for this Thread