Help with a bool statement

This is a discussion on Help with a bool statement within the C++ Programming forums, part of the General Programming Boards category; Hey, first time poster, in fact I registered for this site to ask this question, as selfish as that is. ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Help with a bool statement

    Hey, first time poster, in fact I registered for this site to ask this question, as selfish as that is.

    I'm taking my first programming class, a C++ class. I've had pretty much no problems so far, except for this one right here. It could be just that I'm missing something simple and stupid, or it could be that I'm off base entirely, I don't know. The problem I think lies in the fact that I missed the last 2 classes, and since 2 classes is an entire week, it's set me behind. I've skimmed over the chapter we're on in our text and we're not even covering this in the chapter, so I think it may be something the professor covered in class that I missed.

    Anyway, onto the problem. This is a 2 part assignment. The first part was to make a program that took a date in mm/dd/year format and find the # that day is in the calendar, making sure to add a day to Feb. in the case of a leap year. This first program did not include error checking for if a date is invalid (i.e: Feb 30, 2000).

    That's what the 2nd part of the assignment is. To add a function that checks to make sure the date entered is valid, and "Allow only dates between January 1, 1800 and December 31, 2100, inclusive, to be entered".

    This is my code for it:
    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    bool leap(int), isInRangeAndValidDate(int, int, int);  //declare functions and variables
    int dayNumber(int);
    int year, day, month, feb, total;           //global, so dayNumber may use them
    
    
    int main()                                                        //begin main
    {
        cout << "This program will tell you the day number for a certian date\n\n";
        cout << "What month? (in number form) ";
          cin  >> month;
        cout << "What day? ";
          cin  >> day;
        cout << "What year? (4 digits) ";
          cin  >> year;                                       //ask for user input
        
        system("cls");                                          //clearscreen
    
    if (isInRangeAndValidDate(month, day, year) == true)  //check if valid date
        cout << "The day number for " << month << "/" << day << "/" << year 
             << " is " << (dayNumber(total) + (day));        
                                          //output the daynumber and month/day/year
    
    else                                               //if invalid, returns error
      cout << "Not a valid date";
    
    
        cout << "\n\n\nDavid \nProg L4Pt1DW\n10/21/10\n\n";
                                                                 //prog ID
        system("pause");
        return 0;
    }                                                           //end main    
    
    
    
    
    bool leap(int year)                                         //bool for leap year
    {
         return (year % 400==0)||(year % 4==0 && year % 100 != 0);
    }                                                              //end leap(year)
    
    
    bool isInRangeAndValidDate(int month, int day, int year)   
    {
      if (leap(year) == true)
        return ((month <= 12 && month >= 1) && (((month == 1||3||5||7||8||10||12 && day <= 31 && day >=1) 
            || (month == 4||6||9||11 && day <= 30 && day >=1) || (month == 2 && day <= 29 >= 1))) && (year <= 2100 && year >= 1800));
            
      else if (leap(year) == false);
        return ((month <= 12 && month >= 1) && (((month == 1||3||5||7||8||10||12 && day <= 31 && day >=1)
            || (month == 4||6||9||11 && day <= 30 && day >=1) || (month == 2 && day <= 28 >= 1))) && (year <= 2100 && year >= 1800));  
    //checks if the date is valid, making sure the right days are in the right month                             
    }                                 //end isInRangeAndValidDate(month, day, year)
    
    
    int dayNumber(int total)                                   //dayNumber start
    {
        if (leap(year) == true)
           feb = 29;
        else
           feb = 28;
                            //if bool leap(year) is true, feb is 29, if not it's 28
        switch(month)
        {                                               //begin switch for month
            case 1:
              return 0;
            case 2:
              return 31;
            case 3:
              return feb+31;
            case 4:
              return feb+62;
            case 5:
              return feb+92;
            case 6:
              return feb+123;
            case 7:
              return feb+153;
            case 8:
              return feb+184;                       //adds the days in the month up
            case 9: 
              return feb+215;
            case 10:
              return feb+245;
            case 11:
              return feb+276;
            case 12:
              return feb+306;                       //end switch statment for the month
        }                                                       
              
    }                                                            //end dayNumber

    I bolded the part of the code that I'm having problems with. I realize it's probably pretty sloppy and inefficient code since I'm pretty new to programming.

    No matter what I try, I can't get the bolded part to work how it's supposed to. I don't even know if you can have a boolean statement with multiple (arguments?). All I can get the code to do is check if the month is 1-12, the day is 1-28/29/30/31, and the year is 1800-2100. I can't get it to account for the leap day added in February.

    By moving stuff around (mainly by switching the ||s to &&s) I can get it to account for the leap day, saying that Feb 29 is valid in 2000, but invalid in 1999, but when I can get it to do that it returns the "invalid" statement for things like Jan 31, 2000. I just can't get both aspects of what it's supposed to do to work at the same time.

    I apologize if this question is possibly too simple to deserve posting, but I'm kinda desperate since it's due online in an hour lol
    Last edited by Moth; 11-11-2010 at 09:05 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    I ended up adding
    Code:
      if (leap(year) == false && month == 2)
        return ((month == 2 && day <= 28 >= 1) && (year <= 2100 && year >= 1800));
    to it in case anyone is curious. I would have liked to get it to do what I wanted to without adding more things, but I just couldn't make it happen

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    In your if statement you have:
    Code:
    month == 1||3||5||7||8||10||12 && day <= 31 && day >=1)
    In C/C++ you do not compound and/or statements this way.

    They must be
    Code:
    if(month == 1 || month == 3 || month == 7)
    The same with:

    Code:
     if (leap(year) == false && month == 2)
        return ((month == 2 && day <= 28 >= 1) && (year <= 2100 && year >= 1800));
    It should be:

    Code:
     if (leap(year) == false && month == 2)
        return ((month == 2 && day <= 28 && day >= 1) && (year <= 2100 && year >= 1800));
    Jim

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    In your if statement you have:
    Code:
    month == 1||3||5||7||8||10||12 && day <= 31 && day >=1)
    In C/C++ you do not compound and/or statements this way.

    They must be
    Code:
    if(month == 1 || month == 3 || month == 7)
    The same with:

    Code:
     if (leap(year) == false && month == 2)
        return ((month == 2 && day <= 28 >= 1) && (year <= 2100 && year >= 1800));
    It should be:

    Code:
     if (leap(year) == false && month == 2)
        return ((month == 2 && day <= 28 && day >= 1) && (year <= 2100 && year >= 1800));
    Jim

    Ah, thank you!

    The bottom one (where I left out "day") was just a typo, but I didn't know you had to do the same with multiple ors. Looking back now, I don't know why I thought that since it's essentially the same thing as ands, but oh well.

    Thanks a ton!

    Also, sorry for quoting a reply that ended up being longer than my post.

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    There is a reason you're having trouble with this: Because you're making it incredibly complex. Look at how much code is repeated in this function. That's the first sign you're doing something wrong.

    What you should try here (and what you should try with any complex problem) is to break it into smaller pieces. For example:

    Code:
    bool isInRangeAndValidDate(int month, int day, int year)
    {
        bool validYear = year >=1800 && year <=2100;
        bool validMonth = month>=1 && month <=12;
        ...
        return validYear && validMonth && validDay;
    }
    Also, don't compare a boolean to a boolean literal. You should write:
    Code:
    if (leap(year))
    {
    and not
    Code:
    if (leap(year)==true)
    {
    Since all of the comparison operators evaluate to another boolean! It's completely pointless.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  4. Can someone help me with this console app please?
    By Marcos in forum C++ Programming
    Replies: 4
    Last Post: 07-26-2003, 07:04 PM
  5. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 07:43 PM

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