Thread: Help with if else statements - new to programming

  1. #1
    Registered User
    Join Date
    Mar 2014
    Location
    Whangarei, Northland, New Zealand
    Posts
    4

    Help with if else statements - new to programming

    Hi I just starting learning C and have an assigment to hand in tomorrow which is supposed to tell you the next date after the day entered i.e todays date is 19/3/2014 tomorrows date should be 20/3/2014

    I have sorted it so it calculates the leap year exceptions for february and if the last day is the 31st it should start a new month and that the last day of the year the next day will be the first day of the new year.

    The problem I'm having is where there are only 30 days in the month - i must be doing something wrong with the else if's but can't figure out what - here is the code so far

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    int day, month, year, nextday, nextmonth;
    bool leapyear;
    int main() {
        printf ("Enter a date in the form day/month/year: ");
        scanf ("%d/%d/%d", &day, &month, &year);
        nextday = day + 1;
        leapyear = false;
        if (year % 4 == 0) {
            leapyear = true;
        }
        if (year % 100 == 0) {
            leapyear = false;
        }
        if (year % 400 == 0) {
            leapyear = true;
        }
        if (leapyear) {
            if ((day == 28) && (month == 2)) {
                if (leapyear) {
                    nextday = day + 1;
                }
            } 
        } else if ((day == 28) && (month == 2)) {
            nextday = 1;
            month = month + 1;
        } else if ((day == 31) && (month == 12)) {
            nextday = 1;
            month = 1;
            year = year + 1;
        } else if ((day < 31) && (month == 1 || 3 || 5 || 7 || 8 || 10 )) {
            nextday = day + 1;
            month = month;
        } else if ((day == 31) && (month == 1 || 3 || 5 || 7 || 8 || 10 )) {
            nextday = 1;
            month = month + 1;
        }    else if ((day < 30) && (month == 4 || 6 || 9 || 11))  {
            nextday = day + 1;
            month = month; 
        }  else if ((day == 30) && (month == 4|| 6 || 9 || 11)) {
            nextday = 1;
            month = month + 1;
        }  else if ((day < 31) && (month == 12)) {
            nextday = day + 1;
            month = month;
        }
        printf ("%d/%d/%d", nextday, month, year);
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    (month == 1 || 3 || 5 || 7 || 8 || 10 ))
    You can't do it that way. Look in your book, do you ever see a compound conditional that looks like that? You need to do a direct comparison of the variable with each number.

    Code:
    month == 1 || month == 3 || ...

  3. #3
    Registered User
    Join Date
    Mar 2014
    Location
    Whangarei, Northland, New Zealand
    Posts
    4
    Ok will try rewriting. Also I don't have a book.

  4. #4
    Registered User
    Join Date
    Mar 2014
    Location
    Whangarei, Northland, New Zealand
    Posts
    4
    I've been only learning for 3 weeks

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Quote Originally Posted by LarissaM View Post
    Ok will try rewriting. Also I don't have a book.
    Worry not! Google is your book now...

  6. #6
    Registered User
    Join Date
    Mar 2014
    Location
    Whangarei, Northland, New Zealand
    Posts
    4
    Thanks :-) all sorted now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. So if-statements are bad now?
    By MutantJohn in forum C Programming
    Replies: 22
    Last Post: 06-02-2013, 01:08 AM
  2. Little help - If statements
    By mrben2k9 in forum C Programming
    Replies: 1
    Last Post: 11-09-2009, 06:41 PM
  3. Help in C with 'if' statements
    By jamestgarner in forum C Programming
    Replies: 5
    Last Post: 09-07-2006, 01:41 AM
  4. Which one of these if() statements is best?...
    By MathFan in forum C++ Programming
    Replies: 11
    Last Post: 05-07-2005, 02:19 AM
  5. If statements :P
    By Pyroteh in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2005, 01:37 PM