Help - For loop not working properly

Hello

My for loop is not adding up the numbers together but picking up single value of the array for the julian date program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//Write a program that converts a date (dd mm) to julian format.
//The julian format is the day of the year the ‘dd mm’ date represents,
//e.g. the julian format of ’27 03’ (i.e. the 27th of March) is 31 + 28 + 27 , i.e. 31 days in January plus 28 days in February plus 27,
//which gives a julian date of 86. Use the following definition for the number of days in each month:
//int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
//You can ignore leap years.
//The program must ask for 2 dates – read the day and month of each date separately.
// It must then convert both dates to their julian format, display them, and then display the number of days difference between the two dates.
//The program must check which is the smaller date before subtracting – the user may enter the dates in any order.
//Verify that the dates entered are legitimate dates, using the assert function and the above defined array, e.g. 31 6 is not a valid date.


#include <iostream>
#include<cassert>
//asks for input  of 2 dates and converts them to julian format
int FirstSumOfDays( int d1, int m1)
{
    using namespace std;

    int i=0, date1=0, sumOfJulianDays1=0  ;
    int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31} ;
    for( i = 0; i <= (m1-1); i++);
{
    sumOfJulianDays1 += daysPerMonth[i];
}
    date1 = d1 + sumOfJulianDays1;
    cout<<" First Date entered ("<<d1<<" "<<m1<<") in Julian Date format is "<<date1<<endl;
    return(date1);
}
int SecondSumOfDays( int d2, int m2)
{
    using namespace std;
    int i, date2 = 0, sumOfJulianDays2 = 0 ;
    int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    for (i=0 ; i < (m2-1) ; i++);
    {
        sumOfJulianDays2 += daysPerMonth[i];

    }

    date2 = d2 + sumOfJulianDays2;
    cout<<" Second Date entered ("<<d2<<"  "<<m2<<") in Julian Date format is "<<date2<<endl;
    return(date2);
}
int checkdate( int &date1, int &date2)
{
    using namespace std;
    int dateDifference, largerDate, smallerDate;
       {

         if (date1 > date2)
     {
    largerDate = date1;
    smallerDate = date2;
    dateDifference=largerDate-smallerDate;
    cout<<" the Difference between the dates " <<largerDate<<" and " <<smallerDate<<" is "<<dateDifference<<endl;
    }
    else if(date2 > date1)
   {
    largerDate = date2;
    smallerDate = date1;
    dateDifference=largerDate-smallerDate;
    cout<<" the Difference between the dates " <<largerDate<<" and " <<smallerDate<<" is "<<dateDifference<<endl;
    }
    else if((date1 == date2) && (date2 == date1))
    {
    cout<<" No difference between the dates, they are equal"<<endl;
    }

         }
     return(dateDifference);
     }


using namespace std;
int main()
{
int day1,day2,cdate1=0,cdate2=0,month1,month2,dateDiff =0 ;
cout<<"Enter Day for the first date : "<<'\t';
cin>>day1;
assert ((day1 >= 1) && (day1 <=31));


cout<<"Enter Month for the first date :"<<'\t';
cin>>month1;
assert ((month1 >=1) && (month1 <=12));


FirstSumOfDays( day1, month1);

cout<<"Enter Day for the second date : "<<'\t';
cin>>day2;
assert ((day2 >= 1) && (day2 <=31));


cout<<"Enter Month for the second date : "<<'\t';
cin>>month2;
assert ((month2 >=1) && (month2 <=12));

SecondSumOfDays(day2,month2);

cout<<"Calculating the date difference.... " <<endl;
cdate1 = FirstSumOfDays(day1,month1);
cdate2 = SecondSumOfDays(day2, month2);
dateDiff = checkdate(cdate1,cdate2);
return 0;
}

Last edited on
Line 9, 22: The semicolon terminates the for loop. Get rid of it.

Other questionable code:
FirstSumOfDays() and SecondSumOfDays() are identical except for one word in the cout.
These should be combined into a single function and the cout moved out of the function.

Line 6,19,34,60: If you're going to use using namespace std;, specify it only once after your includes. Better, don't specify it at all.

Line 36: Why the two comparisons? It makes no difference which order you compare the values. not equal is not equal.

Line 65,69,75,79: Your asserts are bogus. Consider if day1 is entered as 32. day1 > 1 is going to be true. The second comparison is not going to be executed. What you want is an AND condition.
 
assert (day1 >= 1 && day1 <=31);

Thank you for your prompt response.
Can you suggest how i can incorporate a single function without mixing up the calculations.

Its now running.

thank you very much.
You could always make it so the functions cout is
std::cout << "Date entered ("<<d2<<" "<<m2<<") in Julian Date format is "<<date2<<endl;
And just print " First " or " Second " outside of the function, thus getting the same output.
thank you .
Topic archived. No new replies allowed.