Thread: Very new at this, need help with if/else

  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
    4,183
    You need to use "==" to compare; "=" does assignment.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Like this:
    Code:
    if(d>=28 && d<=31)

  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