GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Nov-2008, 12:33
minwei86 minwei86 is offline
New Member
 
Join Date: Oct 2008
Posts: 13
minwei86 is an unknown quantity at this point

Convert month name to to month number


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.
  #2  
Old 10-Nov-2008, 12:38
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 376
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: got a bit of prob here


If you just want the compiler to recognize that jan = 1, feb = 2, etc, you can use macros, like this:

#define jan 1
#define feb 2

etc.
__________________
www.blake-foster.com
  #3  
Old 10-Nov-2008, 12:53
minwei86 minwei86 is offline
New Member
 
Join Date: Oct 2008
Posts: 13
minwei86 is an unknown quantity at this point

Re: got a bit of prob here


hmm..so if user enter "jan" the compiler will recognise it as 1 rite, can teach me how i go along to implement it...
  #4  
Old 10-Nov-2008, 13:03
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 376
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: got a bit of prob here


No. Now you're talking about converting user input from a string to an integer at runtime. That's different from making the compiler recognize that jan = 1, feb = 2, etc. If you want to do it at runtime, the easiest thing to do would probably by to store the string equivalents of the month names in an array, and search the array for a match.

This should get you started:

CPP / C++ / C Code:
#include <string>

using namespace std;

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

int getMonth(const string& abbrev)
{
  // loop through the array, and find the month that matches abbrev
}
__________________
www.blake-foster.com
  #5  
Old 10-Nov-2008, 13:09
minwei86 minwei86 is offline
New Member
 
Join Date: Oct 2008
Posts: 13
minwei86 is an unknown quantity at this point

Re: got a bit of prob here


haha i tried that before...i get stuck while get started...cos i create a array of number from 1 to 12 like int mth[] = {1,2,3,4,5,6,7,8,9,10,11,12}; i intend to use pointer to point jan to 1 , something like that...but doesnt work well as pointer cant be use to point string to int..have to be same data type
  #6  
Old 10-Nov-2008, 13:15
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 376
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: got a bit of prob here


Why are you putting the numbers from 1 to 12 in an array? You want to search the array for the name of a month, not the number. Then add 1 to the index into the array to get the number of the month.

If the user enters "jun", for example, when you search through the array you find the string "jun" at months[5] and so you return 6.
__________________
www.blake-foster.com
  #7  
Old 10-Nov-2008, 13:18
minwei86 minwei86 is offline
New Member
 
Join Date: Oct 2008
Posts: 13
minwei86 is an unknown quantity at this point

Re: got a bit of prob here


i think i get what u mean by it...how should i make it when i type "jun" it display 6?
  #8  
Old 10-Nov-2008, 13:24
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 376
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: got a bit of prob here


Just call the getMonth function (you'll need to finish implementing it )

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

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

int getMonth(const string& abbrev)
{
  // loop through the array and find the entry that matches abbrev
}

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

If you don't want it to be case-sensitive, convert the input to lower case.
__________________
www.blake-foster.com
  #9  
Old 10-Nov-2008, 14:31
minwei86 minwei86 is offline
New Member
 
Join Date: Oct 2008
Posts: 13
minwei86 is an unknown quantity at this point

Re: got a bit of prob here


CPP / C++ / C Code:
#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);
}

i try to do like this...i type jan it will return 1, but i type other it also return 1...why?
  #10  
Old 10-Nov-2008, 14:59
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: got a bit of prob here


Your variable count only gets incremented when it is equal to the input. Maybe just return i when that happens instead:

CPP / C++ / C Code:
int getMonth(string& month)
{
  for(int i = 0; i<12; i++)
  {
      if(month == months[i]) return i; // or return i+1, depending on what you want
  }
  return 0;
}
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ASCII art display prob. suvirj C++ Forum 2 01-Oct-2006 17:17
Install prob. with MS Visual C++ 2003 on XP. PeteGallo MS Visual C++ / MFC Forum 0 10-Feb-2006 20:28
Need help/advise on HDD prob [Ci] Luigi Computer Hardware Forum 9 28-Aug-2004 14:52
writing into file prob belludandy C Programming Language 0 15-Nov-2003 20:25
vector prob nattylife C++ Forum 1 15-Nov-2003 08:41

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:15.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.