Grouping calculations into functions

Hello all,

I am just starting to learn C++ and I need a hint on how to figure out how to take things out of int main() and grouping them into functions.

The code is completed
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
#include <iostream>
#include <iomanip>

using namespace std;



int main()
{
  int year, month, day;
  char c;
  bool validInput = false;
  while ((cin) && !validInput)
    {
      cout << "Enter a date in the form YYYY-MM-DD: " << flush;
      cin >> year >> c >> month >> c >> day;

      // Check to see if this is a valid date

      // The Gregorian calendar began On Oct 15, 1582. Earlier dates
      // are invalid.

      if (year < 1582)
	validInput= false;
      else if (year == 1582 && month <= 10 && day < 15)
	validInput = false;

      // Months must be in the range 1..12
      else if (month < 1 || month > 13)
	validInput = false;

      // Days must be in the range 1..K where K is the number of
      // days in that month.
      else
	{
	  int numberOfDaysInMonth = 0;
	  switch (month)
	    {
	    case 1:
	    case 3:
	    case 5:
	    case 7:
	    case 8:
	    case 10:
	    case 12:
	      numberOfDaysInMonth = 31;
	      break;

	    case 4:
	    case 6:
	    case 9:
	    case 11:
	      numberOfDaysInMonth = 30;
	      break;

	    case 2:
	      if (((year % 4 == 0) && (year % 100 != 0))
		  || (year % 400 == 0))
		numberOfDaysInMonth = 29;
	      else
		numberOfDaysInMonth = 28;
	    }

	  if (day < 1 || day > numberOfDaysInMonth)
	    validInput = false;
	  else
	    validInput = true;
	}

      if (!validInput)
	{
	  cout << "Sorry, that is not a valid date" << endl;
	  string garbage;
	  getline (cin, garbage); // discard the rest ofthe input line
	}
    }
  if (!cin)
    {
      cout << "Could not obtain valid input." << endl;
      return -1;
    }


  // Input is valid - compute the day number


  // Add up the number of days in all earlier months
  // of this year
  int sum = 0;
  for (int m = 1; m < month; ++m)
    {
      int monthLength = 0;
      switch (m)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
	  monthLength = 31;
	  break;

	case 4:
	case 6:
	case 9:
	case 11:
	  monthLength = 30;
	  break;

	case 2:
	  if (((year % 4 == 0) && (year % 100 != 0))
	      || (year % 400 == 0))
	    monthLength = 29;
	  else
	    monthLength = 28;
	}

      sum += monthLength;
    }

  // Then add the day number to that sum
  int dayNum = sum + day;

  cout << setw(2) << setfill('0') << month
       << "/" << setw(2) << setfill('0') << day << "/"
       << setw(4) << year;
  cout << " is day #" << dayNum << " of that year." << endl;

  return 0;
}


I just need to know what to do to take for example:

"A function to compute the day number within the year (returned as an int) given the year, month, and day (passed as int parameters)."

There are others but once I get a hint on how to do this I can figure the rest out. I been stuck on this for days and figure it out for the world LOL!

Thanks for any kind of help.
Thanks! It makes a bit more sense to me. So that means that my functions have to be before the int main () right?
Only if you don't want to prototype them.
Topic archived. No new replies allowed.