#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    14
    Rep Power
    0

    C: Storing Words in variables


    I'm a beginner so I wanted to know if there is a way to store words such as the day of the week in a variable

    The program asks for a number from 1-7 and then outputs the correspnding day of the week.

    Ex.
    1=monday, 2=tuesday. ........etc

    I know how to write the program I just can't figure out how to store the days into a varible I can just use in a printf statement.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Fairly basic. A word can be stored in a char array. This would be printed out in printf with the %s format descriptor (s for "string").
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    14
    Rep Power
    0
    Thank you very much but how would I initialize this at the beginning?

    and when I'm writing the "if" statements is this the correct format?

    if (input == 1)
    output = "Monday";
    if ..................................


    printf("The day is %s\n", output);
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Code:
    char *days[] = {"Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday", "Sunday"};
    ...
    ...
    
    printf("Today is %s\n", days[input - 1]);
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    14
    Rep Power
    0
    This is my program but it is giving me an error when I compile.

    "operand must be a modifiable lvalue"

    #include <stdio.h>

    int main( )

    {
    char mn[] = {"January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December"};
    int month;

    printf("Please enter a number between 1 and 12:\n");
    scanf("%d", &month);

    if (month == 1)
    mn = "January";
    else if (month == 2)
    mn = "February";
    else if (month == 3)
    mn = "March";
    else if (month == 4)
    mn = "April";
    else if (month == 5)
    mn = "May";
    else if (month == 6)
    mn = "June";
    else if (month == 7)
    mn = "July";
    else if (month == 8)
    mn = "August";
    else if (month == 9)
    mn = "September";
    else if (month == 10)
    mn = "October";
    else if (month == 11)
    mn = "November";
    else if (month == 12)
    mn = "December";

    printf("The day of the week is %s\n", mn[input - 1]);
    }
    Last edited by Mr.Roboto; September 29th, 2003 at 03:57 PM.
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    You have a few errors.
    1. You should declare mn as char *, not char.
    2. Since mn is an array, there's no reason to do comparisons like if (month == 1).
    3. In C, copying strings is done with strcpy. foo = "string" doesn't work, but strcpy(foo, "string"); does.
    4. You're referring to mn[input - 1]. input is undefined, you must mean mn[month - 1];

    With that said, here's your code with my changes in bold letters:
    Code:
    include <stdio.h> 
    
    int main( ) 
    
    { 
    char *mn[] = {"January", "February", "March", "April", "May", "June", "July", 
    "August", "September", "October", "November", "December"}; 
    int month; 
    
    printf("Please enter a number between 1 and 12:\n"); 
    scanf("%d", &month); 
    
    printf("The month is %s\n", mn[month - 1]); 
    return 0;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    14
    Rep Power
    0
    again i am a beginner so if I don't do comparisons with arrays what else could I do instead of using if statements?

    btw it compiled fine but i got some segmentation fault<coredump> when I actually ran it.

    HELP!@!@!@!@:confused:
    Last edited by Mr.Roboto; September 29th, 2003 at 08:18 PM.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    14
    Rep Power
    0

    Unhappy


    plz...............................:confused:
  16. #9
  17. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Doe this version cause problems?

    Code:
    #include <stdio.h> 
    
    int main( ){ 
        char *mn[] = {"January", "February", "March", "April", "May", "June", "July", 
            "August", "September", "October", "November", "December"}; 
        int month; 
    
        while (1){
            printf("Please enter a number between 1 and 12 (99 to exit):\n"); 
            scanf("%d", &month); 
    
            if (month == 99) break;
    
            if (month < 0 || month > 12){
                printf("Only enter a value between 1 and 12 inclusive!\n");
                fflush(stdin);
            }else{
                printf("The month is %s\n", mn[month - 1]); 
            }
        }
        return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo