Thread: Need help as to why the program is not working

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    16

    Question Need help as to why the program is not working

    Hello I have a date program that takes the date from the user and then prints the date and number of days in the year. The output is supposed to look like this if 3/7/1999 is entered

    07-March-1999 is day number 66 in 1999

    obviously this is not how it is working so any help would be nice.

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    
    using namespace std;
    
    
    void getDate (int &, int &, int &);
    bool validDate (int, int, int);
    bool isLeapYear (int);
    void printDate (int, int, int);
    int dayNumber (int, int, int);
    
    
    int main ()
    {
       int day;
       int month;
       int year;
       int totalDays;
       char again;
       bool valid;
       cout << "This program will take a date that you enter and it will determine if the year was" << endl;
       cout << "a leap year and how many days into the year you are." << endl;
       
       do
        {
          getDate(day, month, year);
          
          valid = validDate(day, month, year);
          
          if (valid == 1)
           {
             
             printDate(day, month, year);
             
             totalDays = dayNumber(day, month, year);
             
             cout << "is day number" << " " << totalDays << "in" << " " << year << endl;
           }
          else
           {  
             cout << "Error you did not follow the proper format" << endl;
         
           }
          
          cout << "Do you wish to repate the program? Enter Y or N" << endl;
          cin >> again;
       
        } while (again == 'Y' || again == 'y');
       
       return 0;  
          
    }
    
    
    void getDate (int& day, int& month, int& year)
    {
       char second;
       char fourth;
       cout << "Please enter a date in the format of m/d/yyyy" << endl;
       cin >> month >> second >> day >>fourth >> year;
       
       month = month;
       day = day;
       year = year;
    }
    
    
    bool validDate (int day, int month, int year)
    {
      int numberOfDays;
      bool status;
      bool leap;
      
      if (year >= 1 || year <= 3000)
           if (month >= 1 || month <= 12)
                  switch(month)
                   {
                      case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
                             numberOfDays = 31;
                             if (day == numberOfDays)
                          {
                                    status = true;
                              }
                         else
                             {
                                  status = false;
                              }
                         break;
                      case 2 :
                             leap = isLeapYear(year);
                             if (leap == 1)
                          {
                                    numberOfDays = 29;
                                 if (day == numberOfDays)
                             {
                                        status = true;
                             }
                                 else
                             {
                                        status = false;
                             }
                          }        
                             else
                          {
                                    numberOfDays = 28;
                                 if (day == numberOfDays)
                             {
                                        status = true;
                             }
                                 else
                             {
                                        status = false;
                             }
                           }
                                break;
                      default:
                             numberOfDays = 30;
                             if (day == numberOfDays)
                          {
                                    status = true;
                          }
                             else
                          {
                                    status = false;
                          }
                        
                     }    
     return status;      
    }
    
    
    bool isLeapYear (int year)
    {
          bool leapYear;
          
          if (year % 4 != 0)
              leapYear = false;    
          else if (year % 100 == 0) 
                    if (year % 400 == 0 )
                           leapYear = true;
          else 
                leapYear = true;
          
          return leapYear;
    }
    
    
    void printDate (int day, int month, int year)
    {
          string monthOfYear;
          
          switch (month)
          {   
             case 1 :
                monthOfYear = "January";
             break;
             
             case 2 :
                monthOfYear = "February";
             break;
             
             case 3 :
                monthOfYear = "March";
             break;
             
             case 4 :
                monthOfYear = "April";
             break;
             
             case 5 :
                monthOfYear = "May";
             break;
             
             case 6 :
                monthOfYear = "June";
             break;
             
             case 7 :
                monthOfYear = "July";
             break;
             
             case 8 :
                monthOfYear = "August";
             break;
             
             case 9 :
                monthOfYear = "September";
             break;
             
             case 10 :
                monthOfYear = "October";
             break;
             
             case 11:
                monthOfYear = "November";
             break;
             
             default:
                monthOfYear = "December";
           }
             if (day >= 1 || day <= 9)
               std::cout << setfill('0')
                         << setw(1) << day << "-" << monthOfYear << "-" << year;
             else
                cout << day << "-" << monthOfYear << "-" << year;
          
          
    }
    
    
    int dayNumber (int day, int month, int year)
    {
          int numberOfDays;
          int totalDays;
          bool leap;
    
    
             leap = isLeapYear (year);
     if (leap == 1)
      {
       validDate(day, month, year);
           for (int x = 1; x < month - 1; x++)
               switch (month)
               {
                   case 1:
                       numberOfDays = 31;
                       totalDays = numberOfDays;
                   break;
                
                   case 2 :
                       numberOfDays = 29;
                       totalDays = totalDays + numberOfDays;
                   break;
        
                   case 3 :
                       numberOfDays = 31;
                       totalDays = totalDays + numberOfDays;
                   break;
                
                   case 4 :
                       numberOfDays = 30;
                       totalDays = totalDays + numberOfDays;
                   break;
    
    
                   case 5 :
                       numberOfDays = 31;
                       totalDays = totalDays + numberOfDays;
                   break;
    
    
                   case 6 :
                       numberOfDays = 30;
                       totalDays = totalDays + numberOfDays;
                   break;
            
                   case 7 :
                       numberOfDays = 31;
                       totalDays = totalDays + numberOfDays;
                   break;
            
                   case 8 :
                       numberOfDays = 31;
                       totalDays = totalDays + numberOfDays;
                   break;
    
    
                   case 9 :
                       numberOfDays = 30;
                       totalDays = totalDays + numberOfDays;
                   break;
    
    
                   case 10 :
                       numberOfDays = 31;
                       totalDays = totalDays + numberOfDays;
                   break;
    
    
                   case 11 :
                       numberOfDays = 30;
                       totalDays = totalDays + numberOfDays;
                   break;
    
    
                   case 12 :
                       numberOfDays = 31;
                       totalDays = totalDays + numberOfDays;
                   break;
    
    
                   default:
                       totalDays = day;
         }
      }
     else
      {
         for (int x = 1; x < month - 1; x++)
            switch (month)
            {
              case 1:
                    numberOfDays = 31;
                    totalDays = numberOfDays;
                break;
                
                case 2 :
                    numberOfDays = 28;
                    totalDays = totalDays + numberOfDays;
                break;
        
                case 3 :
                    numberOfDays = 31;
                    totalDays = totalDays + numberOfDays;
                break;
                
                case 4 :
                    numberOfDays = 30;
                    totalDays = totalDays + numberOfDays;
                break;
    
    
                case 5 :
                    numberOfDays = 31;
                    totalDays = totalDays + numberOfDays;
                break;
    
    
                case 6 :
                    numberOfDays = 30;
                    totalDays = totalDays + numberOfDays;
                break;
            
                case 7 :
                    numberOfDays = 31;
                    totalDays = totalDays + numberOfDays;
                break;
            
                case 8 :
                    numberOfDays = 31;
                    totalDays = totalDays + numberOfDays;
                break;
    
    
                case 9 :
                    numberOfDays = 30;
                    totalDays = totalDays + numberOfDays;
                break;
    
    
                case 10 :
                    numberOfDays = 31;
                    totalDays = totalDays + numberOfDays;
                break;
    
    
                case 11 :
                    numberOfDays = 30;
                    totalDays = totalDays + numberOfDays;
                break;
    
    
                case 12 :
                    numberOfDays = 31;
                    totalDays = totalDays + numberOfDays;
                break;
    
    
                default:
                    totalDays = day;
        }
      }
        return totalDays;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>void getDate (int &, int &, int &);
    Prefer using variable names in declarations to make your code more readable and self-documenting:
    void getDate(int& day, int& month, int& year);
    bool validDate(int day, int month, int year);
    bool isLeapYear(int year);
    std::string printDate(int day, int month, int year);
    int dayNumber(int day, int month, int year);
    int GetDaysInMonth(int Year, int Month);

    >>void printDate (int, int, int);
    Prefer to keep all the output in the same function because it makes it easier to control the formatting. For example, does function printDate print necessary whitespace or not? And if you don't want the whitespace, what then? It's hard to follow things like formatting and whitespace if you break the output into multiple functions. So suggestion: make the function return a string with an appropriately formatted date. Make main print the date.

    >>bool valid;
    Prefer to declare variables near first use.

    >>if (valid == 1)
    Don't compare booleans to integers. If something is true, you can compare it to true/false, e.g.
    if (valid == true)
    or
    if (valid == false)
    but in contexts where you are evaluating if something is true (e.g. if statements), it's not necessary to include the "== true" part because that's what you're implicitly comparing anyway, so you can shorten it to:
    if (valid)
    There's also a "not" operator to test for false, so:
    if (!valid)

    >>valid = validDate(day, month, year);
    Since you aren't using the result "valid" anywhere but the first if statement, you can just substitute it directly into the if statement.

    Following this advice, you end up with:
    Code:
    int main()
    {
    	cout << "This program will take a date that you enter and it will determine if the year was" << endl;
    	cout << "a leap year and how many days into the year you are." << endl;
    
    	int year, month, day;
    	char again;
    
    	do
    	{
    		cout << "Please enter a date in the format of yyyy-mm-dd" << endl;
    		getDate(day, month, year);
    
    		if (validDate(day, month, year))
    		{
    			auto Date = printDate(day, month, year);
    			auto totalDays = dayNumber(day, month, year);
    			cout << Date << " is day number " << " " << totalDays << " in " << " " << year << endl;
    		}
    		else
    			cout << "Error you did not follow the proper format" << endl;
    
    		cout << "Do you wish to repate the program? Enter Y or N" << endl;
    		cin >> again;
    
    	}
    	while (again == 'Y' || again == 'y');
    	return 0;
    }
    (Btw, "auto" just says, hey compiler, substitute this for the real type for me automatically.)

    >>cin >> month >> second >> day >>fourth >> year;
    First off, you can directly read into the actual parameters eliminating the temporaries. Secondly, you only need one temp variable to "eat" the slashes, so:
    Code:
    void getDate(int& day, int& month, int& year)
    {
    	char tmp;
    	cin >> year >> tmp >> month >> tmp >> day;
    }
    >>if (year >= 1 || year <= 3000)
    Prefer to test for negativity so that you return early in the function. This eliminates the need for long nested indentation, so:
    if (year < 1 || year > 3000) return false;
    The condition is also wrong. What happens if I enter year = 4000?

    >>if (day == numberOfDays)
    What happens if I enter some day that is not == the number of days in the month? In case of march, if I enter 7, what happens?

    In IsLeapYear, what happens if the year is not divisible by 4, but is divisible by 100 but not divisible by 400? You didn't specify what happens! Btw, that situation is impossible because if year is divisible by 100, it is also divisible by 4, so if the first if statement is false, then the second else if statement will never be true.
    You can also directly return boolean expressions, so the function can be reduced to:

    Code:
    bool isLeapYear(int year)
    {
    	return ((year % 4) == 0);
    }
    In printDate, it is not necessary to distinguish between the cases since setw mandates that the width of the outputted integer shall be 2. If the integers are less than 2 characters, it will automatically be padded. Therefore, the function can be simplified to (assuming you would return the date, not print it):

    Code:
    std::string printDate(int day, int month, int year)
    {
    	string monthOfYear;
    
    	switch (month)
    	{
    		case 1: monthOfYear = "January"; break;
    		case 2: monthOfYear = "February"; break;
    		case 3: monthOfYear = "March"; break;
    		case 4: monthOfYear = "April"; break;
    		case 5: monthOfYear = "May"; break;
    		case 6: monthOfYear = "June"; break;
    		case 7: monthOfYear = "July"; break;
    		case 8: monthOfYear = "August"; break;
    		case 9: monthOfYear = "September"; break;
    		case 10: monthOfYear = "October"; break;
    		case 11: monthOfYear = "November"; break;
    		case 12: monthOfYear = "December"; break;
    		default: assert(false);
    	}
    
    	std::stringstream str;
    	str << setw(2) << setfill('0') << day << "-" << monthOfYear << "-" << year;
    	return str.str();
    }
    dayNumber is overly complicated. I suggest you simplify it. Consider that to find the day from the start of the year, you should sum up the days of the previous months + the day in the current month. This is actually an overlap with ValidDate since it also checks how many days a month has, so I suggest you break out that and create a function that takes a year and a month and returns the number of days in that month. Then use that function in DayNumber,
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    16
    Thank you for the help. Some of the issues that you had with the program stem from the way the instructed wanted everything and the defined parameters of the assignment. I am at least happy to know that though I did have some issues with stuff it seemed for the most part that I had what I needed. Thank you again for the help and advice I will make sure to try to keep those all in mind as I continue to program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program isn't working. Help me please!
    By QuinnSF in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2014, 02:28 AM
  2. program not working.....
    By Babita Naagar in forum C Programming
    Replies: 12
    Last Post: 07-03-2012, 01:26 AM
  3. program not working
    By rohit83.ken in forum C Programming
    Replies: 2
    Last Post: 10-20-2007, 06:45 AM
  4. help with working program.
    By AdamanC in forum C Programming
    Replies: 3
    Last Post: 08-28-2004, 05:46 PM