Very new at this, need help with if/else

This is a discussion on Very new at this, need help with if/else within the C Programming forums, part of the General Programming Boards category; Hello, I'm just starting this whole c programming and I've looked at alot of tutorials and still can't figure out ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    11

    Question Very new at this, need help with if/else

    Hello, I'm just starting this whole c programming and I've looked at alot of tutorials and still can't figure out a way to get this basic program to work properly. It keeps saying I have an error somewhere. I tried combining all the conditions in one line as such:

    If (d=28; d=29; d=30; d=31);

    I've also tried it with or in there and most recently I tried using if/else if/else statements and that didn't work. This is what I currently have:
    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    int
    main(void)
    {
        printf("How many days in month?");
        int d = GetInt();
        if (d=28)
        {
        printf("Days in months were: %d\n", d);
        }
        if (d=29)
        {
        printf("Days in months were: %d\n", d);
        }
        else if (d=30)
        {
        printf("Days in months were: %d\n", d);
        }
        else if (d=31)
        {
        printf("Days in months were: %d\n", d);
        }
        else
        {
        printf("Invalid number of days\n");
        }
        return 0;
    }
    If someone could guide me in the right direction I would really appreciate it! Thanks!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,582
    You need to use "==" to compare; "=" does assignment.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Like this:
    Code:
    if(d>=28 && d<=31)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    11
    Hmm... so simple. I tried == when I tried listing them on the same line but never after I separated them and that worked. Thank you!

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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