Hi, I'm new here and also new in programming.

I have a home assigment to make a c++ program which calculates the difference between two dates in days. (present - dateOfBirth).

I don't think I'm allowed to use functions already implemented in c++, but my own.

I've found many examples on the web and here some but not exactly what I need.

I've came across this (the comments in the code are my concerns) I don't understand some simple things, also can the daysFrom1900 function be simplified? where is the 1900 limitation?

using namespace std;

int daysInMonth (int month);
int leapYear (int year);
int daysFrom1900 (int year, int month, int day, int year2, int month2, int day2);

int main()
{

    int year, year2, month, month2, day, day2;
    
    cout << "Enter a year: ";
    
    cin >> year >> month >> day >> year2 >> month2 >> day2;
    
    int counter = daysFrom1900(year, month, day, year2, month2, day2);
    
    cout << counter;
    
    system("pause");
    return 0;
}



int leapYear (int year)
{
  int a = 365;

  if( year % 4 == 0 ){
      if(year % 100 == 0 && year % 400 != 0){
              FALSE;
      }
      TRUE;
  }
 FALSE;

} // Why year%100 and year % 400?? also "a" is never used.


int daysInMonth (int month)
{

    int days;
    switch (month){
           case 1 : days = 31; break;
           case 2 : /*leapYear(year)? days = 29 :*/ days = 28; break;
           case 3 : days = 31; break;
           case 4 : days = 30; break;
           case 5 : days = 31; break;
           case 6 : days = 30; break;
           case 7 : days = 31; break;
           case 8 : days = 31; break;
           case 9 : days = 30; break;
           case 10 : days = 31; break;
           case 11 : days = 30; break;
           case 12 : days = 31; break;
       }
    return days;
} // can I use if statement in case 2? I mean: if(leapyear(year)) days=29; else days=28;




int daysFrom1900 (int year, int month, int day, int year2, int month2, int day2)
{
    
    int y, m, d, y2, m2, d2;
    
    y = year;
    y2 = year2;
    m = month;
    m2 = month2;
    d = day;
    d2 = day2;
    int counter = 0;
    
    
    
         if (y == y2)
              for ( int a = 1; a <= m2; a++)
                  if (a == m2){
                     for (int b = 1; b <= d2; b++)
                         q++; 
                         }//who is "q" ?
                  else {
                     for (int b = 1; b <= daysInMonth(a); b++)
                         counter++;
                         }
                      
         else
             for (int c = y; c <= y2; c++){
    
                 if (c == y){
                    for ( int a = m; a <= 12; a++)
                         for (int b = d; b <= daysInMonth(a); b++)
                            counter++;
                             }
                 else if (y == y2){
                      for ( int a = 1; a <= m2; a++)
                           if (a == m2){
                                for (int b = 1; b <= d2; b++)
                                      counter++;
                                       }
                            else 
                                for (int b = 1; b <= daysInMonth(a); b++)
                                      counter++;
                                        }
                                        
                 else if (c != y){
                      for ( int a = 1; a <= 12; a++)
                           for (int b = 1; b <= daysInMonth(a); b++)
                               counter++;
                               }
             }
                     
        return counter - 1;
      
}//didn't understood much :(

I also found some code but with classes. I'm supposed to learn about classes in a week or two.
I just want simple c++ code, beginner style.

Can somebody please clarify these things for me? Thanks in advance.

I'm not sure I agree with finding code on the internet and trying to use it.

The way for you to understand it is to write your own code. If you run into problems while trying to do your own homework, please let us know and we'll kindly try to help you.

So this is not your code? If this is something you got off the internet, I would not use it. There are better examples.

In particular, this function is either poorly named or takes too many parameters:

int daysFrom1900 (int year, int month, int day, int year2, int month2, int day2)

Why would "daysFrom1900" take TWO dates, not one? You are either calculating the number of days between two dates or you are calculating the number of days since 1900, but not both.

I don't think I'm allowed to use functions already implemented in c++, but my own.

So that means you can't use stuff like "struct tm"? Fair enough.


This function:

// can I use if statement in case 2? I mean: if(leapyear(year)) days=29; else days=28;

Yes you can, and it doesn't have to be all on one line.

Lines 70 - 75 are completely unnecessary, though harmless I suppose. you don't have to create new variables. You already have them passed to the function. Use them.

[edit]

I don't think I'm allowed to use functions already implemented in c++, but my own.

Actually, the more I look at this, the more confused I get. What does this mean?
[/edit]

Edited 5 Years Ago by VernonDozier: n/a

Thanks for the answers(help).

I've give up on this task, I've chosen another problem for which I wrote the code, and it works!

Generate the first prime number after a number "n" inserted from the keyboard.

The code is on the school computer but here it is w/o the prime function. Here goes if it makes any importance or it is of use for anybody.

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{ int n,m;
  cout<<"insert n:"; cin>>n;
   m=n+1;
   while(m!=0)
    { 
      if(prime(m)==1) 
         {
          cout<<"the first prime number greater than n is:"<<m;
          break;
          }/*don't know for sure if I put break with the if or with the while... i think is has the same effect either way */
     m++;
    }
   _getch();
  return 0;
}

Sry when I said I couldn't use c++ implemented functions I was referring to : _strtime,_strdate, ctime and other. I don't know for sure if they are functions. => no time manipulation operations library.

You gave up? The process is simple. Each year has 365 days. Every year divisible by 4 (Leap Year) has 366. Every month has a set number of days (except month 2 in LY). A simple matter of addition.
1) How many years*365
2) Add in # of leap years
3) Get number of days from birthday to end of year 1 and subtract
4) Get number of days from beginning of this year to today and add

For 1&2 a simple FOR loop with an IF for leap year
For 3 and 4 two more FOR loops

Edited 5 Years Ago by WaltP: n/a

This article has been dead for over six months. Start a new discussion instead.