Need Help with Date Program

No longer need help
Last edited on
Hi,

You haven't added up the numbers of days in the previous months :+)

Can you make use of arrays? This would be much easier.

Btw, I really, really hate constructs like this, and feel like strangling the person who taught that:

if (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N')

They are ugly, error prone, non scalable, UGLY, bad, awful, nasty - did I mention UGLY?

You can halve your logic by making use of the toupper function, then you only need to test for 'Y' or 'N'

Also, set a bool Quit variable, and loop on that.

Good LUck !!
By using arrays, I meant have arrays for the month names (you have this) and the days in month. Then you referer to them with a variable as the subscript:


mont = Months[m5 -1];

do something similar for the days in month array

Also, try to have meaningful names for variables, I don't know why everyone gets into their head to abbreviate the heck out of everything. As you go further in your coding, you will often find yourself making variable names longer.

Good variable names can make the code read like telling a story of what is happening, here is a somewhat comical thing, but there is no reason why we all can't make our code read in this storytelling fashion:

http://www.cplusplus.com/forum/lounge/176684/#msg872881
First Im in cs 1428 Which is a beginner class to coding so i have no idea what a toupper function is hahah. And secondly I dont exactly know what you mean by using arrays to add everything up. Lastly I see you saw that my coding has a problem with the looping. What do you think I should do to fix it. What do you mean using a bool quit? And how exactly would I add all the days up. Something like month 1 + month 2 + user input of what day? Like if they put march 5 it would be like January + February + 5?
Last edited on
so i have no idea what a toupper function is hahah


Google and Wiki are your best friends :+) ALso the tutorial, reference and article at the top left of this page.

http://www.cplusplus.com/reference/cctype/toupper/



I dont exactly know what you mean by using arrays to add everything up.


Have an array with the the number days in a months:

1
2
3
 
std::string MonthNames[] = {"Invalid", "January", "February", "March" /* etc */ }
unsigned int DaysInMonth[] = {0,31,28,30 /* etc */}; // the first one is zero because that's an invalid month 


If we are in November what code would you write to add up all the days from Jan to October? Hint: use a for loop and variables, don't do "month 1 + month 2" or "January + February +". The variable operates on the array subscript.

What do you mean using a bool quit?


1
2
3
4
5
6
7
bool Quit = false;

while (!Quit) {

   // user wants to quit
   Quit = true;
}


And how exactly would I add all the days up.


Well, how would you ? The concept I mean. If you had pen & paper, how would you do it? I am trying to get you to think how to do it with a for loop, with 1 line of code in the loop.
Last edited on
Honestly I have no idea how to do any of that. Im not understanding what the bool quit is even going to be used for. I kind of understand the array I dont know why I need invalid or 0. And lastly I dont know how to add up all the days in a for loop. Sorry If Im being difficult. Ive literally been in coding for like 6 weeks so things are hard for me to understand.
Im not understanding what the bool quit is even going to be used for


In your code:
char choice; // Choice is if user wants to continue

I kind of understand the array I dont know why I need invalid or 0.




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
std::string MonthNames[] = {"Invalid", "January", "February", "March" /* etc */ }
unsigned int DaysInMonth[] = {0,31,28,30 /* etc */}; // the first one is zero because that's an invalid month 

unsigned int MonthNum = 2;
std::cout << "Second Month is " << MonthNames[MonthNum] << "\n";
std::cout <<  "Days in Second Month is " << DaysInMonth[MonthNum] << "\n";

MonthNum = 0;
std::cout << MonthNames[MonthNum] << "\n";

unsigned int NumDaysInMonth = DaysInMonth[MonthNum];
if (NumDaysInMonth == 0){
    std::cout << "Month Number error " << "\n";
}
std::cout << DaysInMonth[MonthNum] << "\n";

unsigned int MyArray[] = {1,2,3,4,5,6,7,8,9,10}

unsigned int Sum = 0;
unsigned int Size = 10;
// think about how to do the following if it is only some of the months
for (unsigned int Counter = 0; Counter < Size; ++Counter) {
      Sum += MyArray[Counter];
}

std::cout << "Sum is " << Sum << "\n";

I didnt see that you replied and I already figured out the code on how to add all the days up. Its very long but it gets the job done so I am content. I dont know how I am going to figure out the leap year however. If you could help me with that that would be wonderful. I already Have a function that finds out if the year is a leap year or not but if you could help me figure out how to implement that into my program I would be very grateful.

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <string>

using namespace std;
bool month(int m);
bool day (int d);
bool year (int y);
bool leapyear(int y);
int da(int m);
int total(int b);
string name (int m5);
int main()
{
    int m5;        // The input for what month it is
    int d5;        // The input for what day it is
    int y5;       // The input for what year it is
    char choice;  // Choice is if user wants to continue
    cout << "This C++ Program prints the day number of the year,"
         << " given the date\nin the form of month, then day, then year"
         << endl;
    do
    {
    cout << "Enter date in the form : month day year : \n\nEnter Month  :";
    cin  >> m5;
    if (month(m5)== false)
    {
        cout << m5 <<" is an invalid month number" << endl;
        cout << "\n\nTry another date Y/N --->  ";
        cin  >> choice;

    }
    cout << "Enter Day : ";
    cin  >>  d5;
     if (day(d5)== false)
    {
        cout << d5 <<" is an invalid day number" << endl;
        cout << "\n\nTry another date Y/N --->  ";
        cin  >> choice;
    }
    cout << "Enter Year  : ";
    cin  >> y5;
    if (year(y5)== false)
    {
        cout << y5 <<" is an invalid year number" << endl;
        cout << "\n\nTry another date Y/N --->  ";
        cin  >> choice;
    }
    if (leapyear(y5)== true)
    {
        cout << "\n" << y5 << " is a leap year" << endl;
    }
    cout <<"\n" << name(m5) <<" - " << d5 << " - " << y5<< endl;
    cout << "Is day number " << total(m5)+d5 << " of the year " << y5 << endl;
    if (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N')
        {
        cout << "Invalid choice - enter y/n";
        cin  >> choice;
        }
    }
    while (choice != 'n' && choice != 'N');

            cout << "\nProgram is Terminated";
            cout << "\nTaylor Burcham - November - 9th - 2015" << endl;

    return 0;
}
bool month(int m5)
{
   if (m5 < 1 || m5 > 12)
    return false;
   else
    return true;
}
bool day (int d5)
{
   if (d5 < 1 || d5 > 31)
    return false;
   else
    return true;
}
bool year (int y5)
{
   if (y5 > 0 && y5 <= 2015)
    return true;
   else
    return false;
}
bool leapyear( int y5 )
{
	if ( (y5 % 4 == 0 && y5 % 100 != 0) || ( y5 % 400 == 0))
		return true;
	else
		return false;
}
int da(int m5)
   {
       int days;
        if (m5 == 1 || m5 == 3 || m5 == 5 || m5 == 7
            || m5 == 8 || m5 == 10 || m5 == 12)
        days = 31;
        else if (m5 == 2)
        days = 28;
        else if
            (m5 == 4 || m5 == 6 || m5 == 9|| m5 ==11)
        days = 30;
        days != m5;
        return days;

   }
string name(int m5)
    {
        string mont;
        string Months[]={"January","February","March","April","May",
        "June","July","August","September","October","November","December"};
        if (m5 == 1)
        mont = Months[0];
        else if (m5 == 2)
        mont = Months[1];
        if (m5 == 3)
        mont = Months[2];
        if (m5 == 4)
        mont = Months[3];
        if (m5 == 5)
        mont = Months[4];
        if (m5 == 6)
        mont = Months[5];
        if (m5 == 7)
        mont = Months[6];
        if (m5 == 8)
        mont = Months[7];
        if (m5 == 9)
        mont = Months[8];
        if (m5 == 10)
        mont = Months[9];
        if (m5 == 11)
        mont = Months[10];
        if (m5 == 12)
        mont = Months[11];
        return mont;
    }
int total(int m5)
{
    int sum;
    if (m5 ==1)
        sum = 0;
    else if (m5 ==2)
        sum = 31;
    else if (m5 ==3)
        sum = 31+28;
    else if (m5 ==4)
        sum = 31+28+31;
    else if (m5 ==5)
        sum = 31+28+31+30;
    else if (m5 ==6)
        sum = 31+28+31+30+31;
    else if (m5 ==7)
        sum = 31+28+31+30+31+30;
    else if (m5 ==8)
        sum = 31+28+31+30+31+30+31;
    else if (m5 ==9)
        sum = 31+28+31+30+31+30+31+31;
    else if (m5 ==10)
        sum = 31+28+31+30+31+30+31+31+30;
    else if (m5 ==11)
        sum = 31+28+31+30+31+30+31+31+30+31;
    else if (m5 ==12)
        sum = 31+28+31+30+31+30+31+31+30+31+30;
    return sum;
}
Its very long but it gets the job done so I am content.


To be brutally honest, that is a lackadaisical attitude. I am sorry if you find that offensive, I mean well (honestly) - but I am trying to give you an old fashioned shoulder-shunt in the direction of doing it properly. I gave you a pretty big hint in my last post - so I suggest you read that carefully.

Your current code is exactly NOT how to do it, and seems to miss the point of why we have computers.

What if you had 1 million items of data - would you write 100,000 LOC to add them up?

So you have been learning for 6 weeks now? That should be plenty enough time to learn the concepts to do this assignment.

All your topics seem to start with: "This is my assignment and I have no idea how to do it."

but if you could help me figure out how to implement that into my program I would be very grateful.


Really? Why should I bother - if you just seem to ignore what I am saying to you - and instead do the opposite?

So stop lolly-gagging about, get all your brain cells into 1 sock :+) - and THINK

sometimes it's hard to tell if someone has no idea, or they just want to be annoying on purpose
Topic archived. No new replies allowed.