The basics of programming in c++ for beginners

A task. Calculate the number of days between dates.

The task is simple: Find the number of days between two dates. Provide account leap years.

To practice the two options:

  1. One of the boundary dates described only a year. That is, the start date is entered in full (for example 25.12.2015), and the second only a year (for example 2016). It counts the days before 01.01.2016
  2. Both the date of full – describes day month year.

Here is an example of a, partially crucial first simplified version:

Here the function DaysCount() It receives in the first three parameters of the start date (year, month, day) and the last parameter boundary (year, 1-th day of January,)

Solve any possible way, even though the cycle with something else. But the decision of the second embodiment does not show :) Let it be homework. Good luck!

Questions to ask in the comments

40 thoughts on “A task. Calculate the number of days between dates.

  1. I barely pouserdstvoval and wrote everything with protections, and other input… Could you evaluate any comments, and my solution to this problem:
    #include “stdafx.h”
    #include
    #include

    int main()
    {
    setlocale (LC_ALL,”rus”);
    using std::cout;
    using std::cin;

    Double daus_date = 0;
    double month_date = 0;
    double year_date = 0;

    long long daus_date_int = 0;
    long long month_date_int = 0;
    long long year_date_int = 0;

    //——————————————————————————————————————————-
    //The first date
    //——————————————————————————————————————————-
    bool choice = false;
    while (choice == false)
    {
    cout <> year_date;
    (long long) year_date_int = year_date;
    if (year_date_int >= 0)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    choice = false;
    }
    }
    system("cls");
    choice = false;
    while (choice == false)
    {
    cout <> month_date;
    (long long) month_date_int = month_date;
    if (month_date_int == 1 || month_date_int == 2 || month_date_int == 3 || month_date_int == 4 || month_date_int == 5 || month_date_int == 6 || month_date_int == 7 || month_date_int == 8 || month_date_int == 9|| month_date_int == 10 || month_date_int == 11 || month_date_int == 12)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    choice = false;
    }
    }
    system("cls");
    choice = false;
    while (choice == false)
    {
    cout <> daus_date;
    (long long) daus_date_int = daus_date;
    switch (month_date_int)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    {
    if (daus_date_int >= 1 && daus_date_int <= 31)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    switch (month_date_int)
    {
    case 1:
    cout << "В январе 31 day!\n\n";
    break;
    case 3:
    cout << "В марте 31 day!\n\n";
    break;
    case 5:
    cout << "В мае 31 day!\n\n";
    break;
    case 7:
    cout << "В июле 31 day!\n\n";
    break;
    case 8:
    cout << "В августе 31 day!\n\n";
    break;
    case 10:
    cout << "В октябре 31 day!\n\n";
    break;
    case 12:
    cout <= 1 && daus_date_int <= 30)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    switch (month_date_int)
    {
    case 4:
    cout << "В апреле 30 days!\n\n";
    break;
    case 6:
    cout << "В июне 30 days!\n\n";
    break;
    case 9:
    cout << "В сентябре 30 days!\n\n";
    break;
    case 11:
    cout << "В ноябре 30 days!\n\n";
    break;
    }
    choice = false;
    }
    break;
    }

    case 2:
    {
    if (year_date_int % 4 == 0)
    {
    if (daus_date_int <= 29)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    cout << "В високосном году, in February 29 дней\n\n";
    choice = false;
    }
    }
    else
    {
    if (daus_date_int <= 28)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    cout << "В феврале 28 дней\n\n";
    choice = false;
    }
    }
    break;
    }
    }

    }
    system("cls");

    long long one_daus_date = 0;
    long long one_month_date = 0;
    long long one_year_date = 0;

    one_daus_date = daus_date_int;
    one_month_date = month_date_int;
    one_year_date = year_date_int;

    //——————————————————————————————————————————-
    //The second date
    //——————————————————————————————————————————-
    choice = false;
    while (choice == false)
    {
    cout <> year_date;
    (long long) year_date_int = year_date;
    if (year_date_int >= 0)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    choice = false;
    }
    }
    system("cls");
    choice = false;
    while (choice == false)
    {
    cout <> month_date;
    (long long) month_date_int = month_date;
    if (month_date_int == 1 || month_date_int == 2 || month_date_int == 3 || month_date_int == 4 || month_date_int == 5 || month_date_int == 6 || month_date_int == 7 || month_date_int == 8 || month_date_int == 9|| month_date_int == 10 || month_date_int == 11 || month_date_int == 12)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    choice = false;
    }
    }
    system("cls");
    choice = false;
    while (choice == false)
    {
    cout <> daus_date;
    (long long) daus_date_int = daus_date;
    switch (month_date_int)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    {
    if (daus_date_int >= 1 && daus_date_int <= 31)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    switch (month_date_int)
    {
    case 1:
    cout << "В январе 31 day!\n\n";
    break;
    case 3:
    cout << "В марте 31 day!\n\n";
    break;
    case 5:
    cout << "В мае 31 day!\n\n";
    break;
    case 7:
    cout << "В июле 31 day!\n\n";
    break;
    case 8:
    cout << "В августе 31 day!\n\n";
    break;
    case 10:
    cout << "В октябре 31 day!\n\n";
    break;
    case 12:
    cout <= 1 && daus_date_int <= 30)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    switch (month_date_int)
    {
    case 4:
    cout << "В апреле 30 days!\n\n";
    break;
    case 6:
    cout << "В июне 30 days!\n\n";
    break;
    case 9:
    cout << "В сентябре 30 days!\n\n";
    break;
    case 11:
    cout << "В ноябре 30 days!\n\n";
    break;
    }
    choice = false;
    }
    break;
    }

    case 2:
    {
    if (year_date_int % 4 == 0)
    {
    if (daus_date_int <= 29)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    cout << "В високосном году, in February 29 дней\n\n";
    choice = false;
    }
    }
    else
    {
    if (daus_date_int <= 28)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустивое значение!\n";
    cout << "В феврале 28 дней\n\n";
    choice = false;
    }
    }
    break;
    }
    }

    }
    system("cls");

    long long two_daus_date = 0;
    long long two_month_date = 0;
    long long two_year_date = 0;

    two_daus_date = daus_date_int;
    two_month_date = month_date_int;
    two_year_date = year_date_int;

    //——————————————————————————————————————————-
    //Score in the first days of the date of
    //——————————————————————————————————————————-
    double counter_daus_one_date = 0;
    for (long long counter = 0; counter < one_year_date; counter++)
    {
    if (counter % 4 == 0 && counter != 0)
    counter_daus_one_date += 366;
    else
    counter_daus_one_date += 365;
    }
    for (short counter = 0; counter <= one_month_date; counter++)
    {
    switch (counter)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    counter_daus_one_date += 31;
    break;
    case 4:case 6 :case 9: case 11:
    counter_daus_one_date += 30;
    break;
    case 2:
    if (one_year_date % 4 == 0)
    counter_daus_one_date += 29;
    else
    counter_daus_one_date += 28;
    break;
    }
    }
    counter_daus_one_date += one_daus_date;

    //——————————————————————————————————————————-
    //Score days for the second date
    //——————————————————————————————————————————-

    double counter_daus_two_date = 0;
    for (long long counter = 0; counter < two_year_date; counter++)
    {
    if (counter % 4 == 0 && counter != 0)
    counter_daus_two_date += 366;
    else
    counter_daus_two_date += 365;
    }
    for (short counter = 0; counter <= two_year_date; counter++)
    {
    switch (counter)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    counter_daus_two_date += 31;
    break;
    case 4:case 6 :case 9: case 11:
    counter_daus_two_date += 30;
    break;
    case 2:
    if (two_year_date % 4 == 0)
    counter_daus_two_date += 29;
    else
    counter_daus_two_date += 28;
    break;
    }
    }
    counter_daus_two_date += two_daus_date;
    //——————————————————————————————————————————-
    //Account number of days between dates
    //——————————————————————————————————————————-

    cout << "Первая дата: ";
    if (one_daus_date < 10)
    cout << '0' << one_daus_date << '.';
    else
    cout << one_daus_date << '.';
    if (one_month_date < 10)
    cout << '0' << one_month_date << '.';
    else
    cout << one_month_date << '.';
    cout << one_year_date << '\n';

    cout << "Вторая дата: ";
    if (two_daus_date < 10)
    cout << '0' << two_daus_date << '.';
    else
    cout << two_daus_date << '.';
    if (two_month_date < 10)
    cout << '0' << two_month_date << '.';
    else
    cout << two_month_date << '.';
    cout << two_year_date << '\n';

    if (counter_daus_two_date < counter_daus_one_date)
    cout << "Количество дней между датами: " << counter_daus_one_date – counter_daus_two_date < counter_daus_one_date)
    cout << "Количество дней между датами: " << counter_daus_two_date – counter_daus_one_date << '\n';
    if (counter_daus_two_date == counter_daus_one_date)
    cout << "Вы ввели две одинаковые даты!" << '\n';
    _getch();
    return 0;
    }

    1. excuse me, I do not know how this code issue in the style of C ++ (both decorated in a frame your decision).

      1. Necessary to frame your code tegami code:
        [code]
        … your code here …
        [/code]
        Only instead of square brackets need to write a corner (insignia “less” and “more”).

      2. And like this (without formatting) recorded by your example one, naturally, even watch will not…

        With the same success all your code can be written in a single line at all (you can check, that while it is still up and running).

  2. Here is a cultural version of my code:

    #include "stdafx.h"
    #include
    #include

    int main()
    {
    setlocale (LC_ALL,"rus");
    using std::cout;
    using std::cin;

    double daus_date = 0;
    double month_date = 0;
    double year_date = 0;

    long long daus_date_int = 0;
    long long month_date_int = 0;
    long long year_date_int = 0;

    //-------------------------------------------------------------------------------------------------------------------------------
    //Первая дата
    //-------------------------------------------------------------------------------------------------------------------------------
    bool choice = false;
    while (choice == false)
    {
    cout <> year_date;
    (long long) year_date_int = year_date;
    if (year_date_int >= 0)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    choice = false;
    }
    }
    system("cls");
    choice = false;
    while (choice == false)
    {
    cout <> month_date;
    (long long) month_date_int = month_date;
    if (month_date_int == 1 || month_date_int == 2 || month_date_int == 3 || month_date_int == 4 || month_date_int == 5 || month_date_int == 6 || month_date_int == 7 || month_date_int == 8 || month_date_int == 9|| month_date_int == 10 || month_date_int == 11 || month_date_int == 12)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    choice = false;
    }
    }
    system("cls");
    choice = false;
    while (choice == false)
    {
    cout <> daus_date;
    (long long) daus_date_int = daus_date;
    switch (month_date_int)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    {
    if (daus_date_int >= 1 && daus_date_int <= 31)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    switch (month_date_int)
    {
    case 1:
    cout << "В январе 31 день!\n\n";
    break;
    case 3:
    cout << "В марте 31 день!\n\n";
    break;
    case 5:
    cout << "В мае 31 день!\n\n";
    break;
    case 7:
    cout << "В июле 31 день!\n\n";
    break;
    case 8:
    cout << "В августе 31 день!\n\n";
    break;
    case 10:
    cout << "В октябре 31 день!\n\n";
    break;
    case 12:
    cout <= 1 && daus_date_int <= 30)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    switch (month_date_int)
    {
    case 4:
    cout << "В апреле 30 дней!\n\n";
    break;
    case 6:
    cout << "В июне 30 дней!\n\n";
    break;
    case 9:
    cout << "В сентябре 30 дней!\n\n";
    break;
    case 11:
    cout << "В ноябре 30 дней!\n\n";
    break;
    }
    choice = false;
    }
    break;
    }

    case 2:
    {
    if (year_date_int % 4 == 0)
    {
    if (daus_date_int <= 29)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    cout << "В високосном году, в феврале 29 дней\n\n";
    choice = false;
    }
    }
    else
    {
    if (daus_date_int <= 28)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    cout << "В феврале 28 дней\n\n";
    choice = false;
    }
    }
    break;
    }
    }

    }
    system("cls");

    long long one_daus_date = 0;
    long long one_month_date = 0;
    long long one_year_date = 0;

    one_daus_date = daus_date_int;
    one_month_date = month_date_int;
    one_year_date = year_date_int;

    //-------------------------------------------------------------------------------------------------------------------------------
    //Вторая дата
    //-------------------------------------------------------------------------------------------------------------------------------
    choice = false;
    while (choice == false)
    {
    cout <> year_date;
    (long long) year_date_int = year_date;
    if (year_date_int >= 0)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    choice = false;
    }
    }
    system("cls");
    choice = false;
    while (choice == false)
    {
    cout <> month_date;
    (long long) month_date_int = month_date;
    if (month_date_int == 1 || month_date_int == 2 || month_date_int == 3 || month_date_int == 4 || month_date_int == 5 || month_date_int == 6 || month_date_int == 7 || month_date_int == 8 || month_date_int == 9|| month_date_int == 10 || month_date_int == 11 || month_date_int == 12)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    choice = false;
    }
    }
    system("cls");
    choice = false;
    while (choice == false)
    {
    cout <> daus_date;
    (long long) daus_date_int = daus_date;
    switch (month_date_int)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    {
    if (daus_date_int >= 1 && daus_date_int <= 31)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    switch (month_date_int)
    {
    case 1:
    cout << "В январе 31 день!\n\n";
    break;
    case 3:
    cout << "В марте 31 день!\n\n";
    break;
    case 5:
    cout << "В мае 31 день!\n\n";
    break;
    case 7:
    cout << "В июле 31 день!\n\n";
    break;
    case 8:
    cout << "В августе 31 день!\n\n";
    break;
    case 10:
    cout << "В октябре 31 день!\n\n";
    break;
    case 12:
    cout <= 1 && daus_date_int <= 30)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    switch (month_date_int)
    {
    case 4:
    cout << "В апреле 30 дней!\n\n";
    break;
    case 6:
    cout << "В июне 30 дней!\n\n";
    break;
    case 9:
    cout << "В сентябре 30 дней!\n\n";
    break;
    case 11:
    cout << "В ноябре 30 дней!\n\n";
    break;
    }
    choice = false;
    }
    break;
    }

    case 2:
    {
    if (year_date_int % 4 == 0)
    {
    if (daus_date_int <= 29)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    cout << "В високосном году, в феврале 29 дней\n\n";
    choice = false;
    }
    }
    else
    {
    if (daus_date_int <= 28)
    choice = true;
    else
    {
    cout << "\nВы ввели недопустимое значение!\n";
    cout << "В феврале 28 дней\n\n";
    choice = false;
    }
    }
    break;
    }
    }

    }
    system("cls");

    long long two_daus_date = 0;
    long long two_month_date = 0;
    long long two_year_date = 0;

    two_daus_date = daus_date_int;
    two_month_date = month_date_int;
    two_year_date = year_date_int;

    //-------------------------------------------------------------------------------------------------------------------------------
    //Счёт дней в первой дате
    //-------------------------------------------------------------------------------------------------------------------------------
    double counter_daus_one_date = 0;
    for (long long counter = 0; counter < one_year_date; ++counter)
    {
    if (counter % 4 == 0 && counter != 0)
    counter_daus_one_date += 366;
    else
    counter_daus_one_date += 365;
    }
    for (short counter = 0; counter < one_month_date; ++counter)
    {
    switch (counter)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    counter_daus_one_date += 31;
    break;
    case 4:case 6 :case 9: case 11:
    counter_daus_one_date += 30;
    break;
    case 2:
    if (one_year_date % 4 == 0)
    counter_daus_one_date += 29;
    else
    counter_daus_one_date += 28;
    break;
    }
    }
    counter_daus_one_date += one_daus_date;
    cout << counter_daus_one_date << " counter_one\n";

    //-------------------------------------------------------------------------------------------------------------------------------
    //Счёт дней во второй дате
    //-------------------------------------------------------------------------------------------------------------------------------

    double counter_daus_two_date = 0;
    for (long long counter = 0; counter < two_year_date; ++counter)
    {
    if (counter % 4 == 0 && counter != 0)
    counter_daus_two_date += 366;
    else
    counter_daus_two_date += 365;
    }
    for (short counter = 0; counter < two_month_date; ++counter)
    {
    switch (counter)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    counter_daus_two_date += 31;
    break;
    case 4:case 6 :case 9: case 11:
    counter_daus_two_date += 30;
    break;
    case 2:
    if (two_year_date % 4 == 0)
    counter_daus_two_date += 29;
    else
    counter_daus_two_date += 28;
    break;
    }
    }
    counter_daus_two_date += two_daus_date;
    cout << counter_daus_two_date << " counter_two\n";
    //-------------------------------------------------------------------------------------------------------------------------------
    //Счёт количества дней между датами
    //-------------------------------------------------------------------------------------------------------------------------------

    cout << "Первая дата: ";
    if (one_daus_date < 10)
    cout << '0' << one_daus_date << '.';
    else
    cout << one_daus_date << '.';
    if (one_month_date < 10)
    cout << '0' << one_month_date << '.';
    else
    cout << one_month_date << '.';
    cout << one_year_date << '\n';

    cout << "Вторая дата: ";
    if (two_daus_date < 10)
    cout << '0' << two_daus_date << '.';
    else
    cout << two_daus_date << '.';
    if (two_month_date < 10)
    cout << '0' << two_month_date << '.';
    else
    cout << two_month_date << '.';
    cout << two_year_date << '\n';

    if (counter_daus_two_date < counter_daus_one_date)
    cout << "Количество дней между датами: " << counter_daus_one_date - counter_daus_two_date < counter_daus_one_date)
    cout << "Количество дней между датами: " << counter_daus_two_date - counter_daus_one_date << '\n';
    if (counter_daus_two_date == counter_daus_one_date)
    cout << "Вы ввели две одинаковые даты!" << '\n';
    _getch();
    return 0;
    }

  3. #include
    using namespace std;

    int main()
    {
    int the,my,god;
    int den_1,mes_1,god_1;

    cout<<"vvedite pervuy datu"<<endl;
    cout<<"vvedite den"<>the;
    cout<<"vvedite mesyas"<>my;
    cout<<"vvedite god"<>god;
    cout<<"————————-"<<endl;
    cout<<"vvedite tekushuy datu"<<endl;
    cout<<"vvedite den"<>den_1;
    cout<<"vvedite mesyas"<>mes_1;
    cout<<"vvedite god"<>god_1;
    double k=0;
    int kol_d=0;
    int kol_m=0;
    int kol_g=0;
    int sum_m=0;
    int sum_d=0;

    if(mes_1 == 1)
    k=31;
    else if(mes_1 == 2)
    k=28;
    else if (mes_1 == 3)
    k=31;
    else if (mes_1 == 4)
    k=30;
    else if (mes_1 == 5)
    k=31;
    else if (mes_1 == 6)
    k=30;
    else if (mes_1 == 7)
    k=31;
    else if (mes_1 == 8)
    k=31;
    else if (mes_1 == 9)
    k=30;
    else if (mes_1 == 10)
    k=31;
    else if (mes_1 == 11)
    k=30;
    else if(mes_1 == 12)
    k=31;

    kol_d=k-den;//We consider how many days until the completion of the previous one month the entered date
    kol_m = 12-mes;//how many months before the end of the year
    kol_g = god_1-(god+1);
    sum_d=kol_d+den_1;
    sum_m=kol_m+mes_1;

    cout<<"dney"<<sum_d<<endl;
    cout<<"mes"<<sum_m<<endl;
    cout<<"godov"<<kol_g<<endl;

    return 0;
    }


  4. #include "stdafx.h"
    #include
    #include

    using namespace std;

    int main()
    {
    setlocale (LC_ALL, "rus");

    int day, month, year, finDay,finMonth,finYear, stData,finData, leapYear,monthDay = 0;
    int monthArray[12]{ 31,28,31,30,31,30,31,31,30,31,30,31 };
    while (true)
    {
    cout <> day >> month >> year;

    if (year < 45 || monthArray[month] 12)
    cout << "Ввод нверный, повторите ввод!" << endl;
    else
    break;
    }

    if (!(year % 4) && month < 3) leapYear = year / 4-1;
    else leapYear = year / 4;

    for (int i = 0; i < month; i++)
    monthDay += monthArray[i];

    stData = year * 365 + leapYear + monthDay + day;
    monthDay = 0;

    while (true)
    {
    cout <> finDay >> finMonth >> finYear;

    for (int i = 0; i < finMonth; i++)
    monthDay += monthArray[i];

    if (!(finYear % 4) && month < 3) leapYear = finYear / 4-1;
    else leapYear = finYear / 4;

    finData = finYear * 365 + leapYear + monthDay + finDay;

    if (finData <= stData)
    cout << "Конечная дата не может быть раньше начальной\n";
    else
    break;
    }

    cout << "\n Между датами " << finData - stData << " дней\n";

    _getch();

    return 0;
    }

  5. Is it too zamudrili code? It can all be a lot easier to do with classes. It eventually OOP language and not some sort of BASIC.

    1. OOP is not a panacea and, more often, not easy code, but it makes it more understandable and orderly. A much easier task can be done using the library API C or C ++ languages. But for beginners useful to show how algorithmically the calculations, you can use ready-made tools.

    2. The prevalence of panacea, and most not easy code, but it makes it more understandable and orderly. Due to what really could be simplified code, so it is to use the API libraries C or C ++ to work with dates. But for beginners useful demonstration of computing algorithms, than using ready-made tools.

  6. Here is my solution. Of course, there is no any protection and badly written code, tk. no normal naming and comments, but their task it copes.

  7. Never! … Never use when working with your calendar “manual” representation of dates, calendar (blunt witted). For example the, what:
    – 2016 – it is a leap year, 366 days…
    – but!: 1900 (or 1800 etc.) – this is not leap years, 365 days, exceptions…
    – but!: 2000 – again leap year, an exception to the previous rule…

    To work with the calendar has a library : struct tm data type, time_t … mktime function(), localtime() … etc.

Leave a Reply

Your email address will not be published. Required fields are marked *