Thread: Age Identifier Program Issue in C

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

    Question Age Identifier Program Issue in C

    Hello Everyone, I have an issue with a switch case in my program. I execute it and it does fine all the way up to where it says, "Answer (1, 2, or 3): ". When I enter 1, 2, or 3, it gives me' "Not an input choice!" from the default of the switch case. Can someone help, run the program if you need to, thanks!

    NOTE: I use Code::Blocks on Windows XP.

    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): "); /*where it stops*/
        fgets(ageclass, 50, stdin);
    
    
        switch (ageclass[50])
        {
            case'1':
            {
                printf("You are between the ages of:\n");
                printf("1. 0-12\n");
                printf("2. 13-18\n");
                printf("Answer(1 or 2): ");
                fgets(specificage, 50, stdin);
                   if(specificage == 1)
                    {
                        printf("You are a young minor.");
                    }
                    else
                    {
                        printf("You are an older minor.");
                    }
            }
                break;
    
    
            case'2':
            {
                printf("You are between the ages of:\n");
                printf("1. 19-50\n");
                printf("2. 51-65\n");
                printf("Answer(1 or 2): ");
                fgets(specificage, 50, stdin);
                    if(specificage == 1)
                    {
                        printf("You are a young adult.");
                    }
                    else
                    {
                        printf("You are an older adult.");
                    }
            }
                break;
    
    
            case'3':
            {
                printf("You are between the ages of:\n");
                printf("1. 66-90\n");
                printf("2. 91-110\n");
                printf("Answer(1 or 2): ");
                fgets(specificage, 50, stdin);
                    if(specificage == 1)
                    {
                        printf("You are a young senior citizen.");
                    }
                    else
                    {
                        printf("You are an older senior citizen.");
                    }
            }
                break;
    
    
            default:
                printf("Not an input choice!");
                break;
        }
    
    
        getchar();
        return 0;
    }

  2. #2
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Your problem lies in this line :

    Code:
    switch(ageclass[50])
    I'm not sure why you needed that much space for what looks like a single number answer in the first place, but the way you tested it was invalid. You need to test it at the beginning like this :

    Code:
    switch(ageclass[0])
    I didn't look at the rest of the code very closely, but that should in theory fix it for what you're doing. If you have any new errors, go ahead and post again.

    Edit :

    You should probably just enter an integer like this :

    Code:
    /* ... */
    int num;
    scanf("%d", &num);
    
    switch( num )
    {
    /* ... */
    Last edited by HelpfulPerson; 07-02-2013 at 06:52 PM.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    14
    Quote Originally Posted by HelpfulPerson View Post
    Your problem lies in this line :

    Code:
    switch(ageclass[50])
    I'm not sure why you needed that much space for what looks like a single number answer in the first place, but the way you tested it was invalid. You need to test it at the beginning like this :

    Code:
    switch(ageclass[0])
    I didn't look at the rest of the code very closely, but that should in theory fix it for what you're doing. If you have any new errors, go ahead and post again.

    Edit :

    You should probably just enter an integer like this :

    Code:
    /* ... */
    int num;
    scanf("%d", &num);
    
    switch( num )
    {
    /* ... */
    Thank you very much, the first solution worked great! And it is required that everything the user inputs should be read as a string. Thanks!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A follow up note about what HelpfulPerson wrote:

    Remember, in C, arrays start at 0. Thus, if you have char ageclass[50], valid indexes are 0 through 49. Attempting to read from or write to ageclass[50] is not how you access the entire array (there is not really a way to do that in C), it is accessing the array out of bounds, and results in undefined behavior. That means anything is allowed to happen, however, common manifestations are strange program behavior and crashes (seg fault).

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    14
    Hey guys, another question. The program runs all the way through, but whatever options I enter, the outcome is always the else of the else if statement. Thanks, guys.

    Here is the updated 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[0])
        {
            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(strcmp(specificage, "a") == 0)
                    {
                        printf("You are a young minor.");
                    }
    
    
                    else
                    {
                        printf("You are an old minor.");
                    }
            }
                break;
    
    
            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(strcmp(specificage, "a") == 0)
                    {
                        printf("You are a young adult.");
                    }
    
    
                    else
                    {
                        printf("You are an older adult.");
                    }
            }
                break;
    
    
            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(strcmp(specificage, "a") == 0)
                    {
                        printf("You are a young senior citizen.");
                    }
                    
                    else
                    {
                        printf("You are an older senior citizen.");
                    }
            }
                break;
    
    
            default:
                printf("Not an input choice!");
                break;
        }
    
    
        getchar();
        return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that if there is enough space, fgets will store the newline character from entering the input into the array.
    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
    Jun 2013
    Posts
    14
    Quote Originally Posted by laserlight View Post
    Recall that if there is enough space, fgets will store the newline character from entering the input into the array.
    So should I lessen the string's capacity? Sorry, new to programming. Or is there a function that could get rid of newline?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Nope, you should remove the newline if it is there, by replacing it with a null character. There are a few ways of doing this, e.g., read up on strcspn or strchr.

    Alternatively, you could write strcmp(specificage, "a\n") == 0, but that is a bit of a hack since it depends on the newline character being included. If later you change to use some other method of reading input, this may no longer happen, then you would have a bug.
    Last edited by laserlight; 07-02-2013 at 10:42 PM.
    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
    Jun 2013
    Posts
    14
    Ok, thank you, I will look into that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C program issue...
    By mbxs3 in forum C Programming
    Replies: 2
    Last Post: 02-02-2013, 10:41 PM
  2. Getting an undeclared identifier error in my program
    By jvharps in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2010, 10:23 AM
  3. Loop issue in tax program
    By mistymoon1966 in forum C Programming
    Replies: 4
    Last Post: 10-31-2009, 02:04 PM
  4. Odd issue with program
    By TigerTank77 in forum C Programming
    Replies: 8
    Last Post: 05-12-2009, 12:00 AM
  5. Loging Program issue
    By Keiyentai in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2005, 08:31 PM

Tags for this Thread