Array of character pointers

This is a discussion on Array of character pointers within the C Programming forums, part of the General Programming Boards category; Could anyone check out this code and give me a hint of what I'm doing wrong? I'm trying to make ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    32

    Array of character pointers

    Could anyone check out this code and give me a hint of what I'm doing wrong? I'm trying to make a code that has the user enter a number 1-7, and it displays a day corresponding to that number. For example, 1 = sunday, 2 = monday, etc. Strings are kind of confusing to me right now, so I'm not sure if I'm really far off or close. If anyone has any recommendations on a good string tutorial that would be nice

    When I run this, it asks the user for a number, but regardless of what number I put in, even if it's between 1 and 7, it displays the 'else' statement.

    Thanks!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void main()
    {
            char *nameOfDay[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
            int i;
    
    
            printf("Please enter a number 1 through 7 that corresponds to a day of the week.\n");
    
    
            scanf("%d", &nameOfDay);
    
    
            if(i <= 1 && i >= 7)
            {printf("%s", nameOfDay[i]);}
    
    
            else
            {printf("You have entered an incorrect number, please run the program again and enter a number 1 through 7.\n");}
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    IL
    Posts
    7
    Well, there are a couple of problems. One is your scanf. You have the user enter a number between 1 and 7, but it stores that input into nameOfDay. Your if statement is taking action based on the value in i, but I is not initialized so it contains a garbage value. Also I wouldn't us && for comparison. If you say it out loud, "if i is less than or equal to 1 AND i is greater than or equal to 7". 2 is not less than one and not greater than 7. Also remember that the first index of an array is zero.
    Last edited by xxc0dephr34kxx; 11-27-2011 at 06:55 PM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Shoot. I forgot about it starting at 0. Is there a way to make it so it would make [1] equal to sunday, 2 = monday, etc.? Or would I have to put some other value before 'sunday' so [0] is equal to something?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just subtract one from what they type?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ^^^
    nameofDay[i-1];

    Sounds good to me!

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Yep, that fixed it. Thanks a lot guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I remove a character from character array?
    By nick753 in forum C++ Programming
    Replies: 25
    Last Post: 12-08-2010, 11:27 AM
  2. I think I need a ragged array of character pointers?
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 01:44 PM
  3. Navigating a character string array from pointers
    By Boxknife in forum C Programming
    Replies: 3
    Last Post: 04-16-2009, 02:32 PM
  4. array of character pointers
    By neandrake in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2004, 02:56 AM
  5. array of character pointers
    By RedRum in forum C++ Programming
    Replies: 6
    Last Post: 05-06-2002, 11:05 PM

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