getting months right

I have been writing a calendar program and the next stage is to ammend the zeller function so that the user can enter the month in conventional format. for example the user can enter 1 or january.
anyone have any ideas please?
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
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <iomanip>

void output_month(int start, int total) { 

cout << " S M T W T F S" << endl;
cout << setw(5 * start ) << "";

int i;
for (i = 1; i <= total; i++) {

cout << setw(5) << i; 
if ((i + start) % 7 == 0) 
{ 
cout << endl;
}

}

cout << endl;
return;
}


void zeller(int day,int month,int year)


{
int result;

if (month < 3) 
{
month += 12;
--year;
}

result = day + (13 * month - 27)/5 + year + year/4
- year/100 + year/400;

#ifdef MONDAY_IS_0
result += 6;
#endif // MONDAY_IS_0



cout<<"daycode ="<< (result % 7);


}



void main() 
{ 

int i = 0;
int j = 0;
int day, month, year;

do {
cout << "Input start: ";
cin >> i;
} 
while ((i < 0) || (i > 6));

do {
cout << "Input number of days: ";
cin >> j;
} 
while ((j > 31) || (j < 28));

//output_month(i, j);//call
cout<< "enter day";
cin>>day;
cout<<"enter month";
cin>>month;
cout<< "enter year";
cin>>year;
zeller(day,month,year);

system("PAUSE");
}

what kind of a error are you getting?
no errors the program works fine so far but i dont know how to do the next stage
i need to know what i have to do so that the user can enter the month number or the month in conventional format eg. january,february etc.
but right now the user can only enter it in number format,
any idea of what i can do to let me enter the actual name?
easiest route for a beginner would be calling the "isalpha()" or "isdigit()" functions, if then statement to determine which of two Zeller functions to use. The only difference between them being one works with number input, the other works with a string input.

There are other ways, but this seems to be the easiest. Just ask questions if any of that didn't make sense
No.

There should only be one Zeller function. The Zeller function only works with integers.

The trick is to properly input the month. What you need is a function that gets the month from the user. The function should recognize whether the user input a number or a string like "April" or "apr" which must be turned into a number.

Here is an example of how to convert a month name to a number (scroll down to the bottom):
http://www.cplusplus.com/forum/general/11460/#msg54095

To determine whether or not the input value is a number or not, you can simply try converting one way first, and the other way second. If both fail then the user input something invalid.
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
const char* month_name_abbreviations[] = ...;

// Returns the month as a number in [1, 12].
// If the input was not valid, returns zero.
int get_month()
  {
  int month;

  cout << "Enter the month you were born> ";

  if (cin >> month)
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );  // Get rid of the ENTER key
  else
    {
    // The input of an integer failed.
    // Try to read it as a month name (just like in the other thread).
    cin.clear();

    string s;
    getline( cin, s );
    s += "---";
    s.erase( 3 );

    transform( ... );

    month = find( ... ) - month_name_abbreviations + 1;
    }

  // If the user's input is not valid, return zero.
  if ((0 > month) || (month > 12))
    return 0;

  // Otherwise return the proper month in [1, 12].
  return month;
  }

Hope this helps.

[edit] BTW, the month-name-to-integer trick is pretty slick. Your teacher will know that you didn't make it up yourself. Best to own up to getting help on the internet. [/edit]
Last edited on
Topic archived. No new replies allowed.