date problem

i got this

weekday = (day+ 2*mth + 3*(mth+1)/5 + year + year/4 - year/100 + year/400 + 1) % 7;

but the whole thing will only work out when day,mth and year are integer..

but i wan the user to enter the mth in string format like "jan","feb" and etc.

how to gonna make it like jan = 1, feb = 2 so that when user enter jan, the compiler will know is 1 and do the formulae for me?

can someone guide me along??

all help is much appreciated.
You have to compare the userinput with the names of the months. I would suggest using a string array that holds all the names of months, and then use a loop with counter i to check every element of that array, and if array[i]==userinput returns true i+1 is the month.
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
#include <iostream>
#include <string>

using namespace std;

string months [12] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};

int getMonth(string& month)
{
    int count = 0;

  for(int i = 0; i<12; i++)
  {
      if(month == months[i])
      count++;
  }
  return count;
}

int main()
{
  string month;
  cout<<"Enter a month: ";
  cin>>month;
  cout<<getMonth(month);
}


this is the progress so far, but the result only 1 or 0, i not sure why
1
2
if (month==months[i])
count++;


If the month is equal to months[i], the program updates the variable count by one. If the user inputs a correct name, this will happen one time, so it would return 1. This is what you need:

1
2
if (month == months[i])
count=i+1; //+1 since i starts at 0 
Last edited on
haha ya...thank for your help
Topic archived. No new replies allowed.