![]() |
|
#1
|
|||
|
|||
Convert month name to to month numberi 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
|
||||
|
||||
Re: got a bit of prob hereIf 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
|
|||
|
|||
Re: got a bit of prob herehmm..so if user enter "jan" the compiler will recognise it as 1 rite, can teach me how i go along to implement it...
|
|
#4
|
||||
|
||||
Re: got a bit of prob hereNo. 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:
__________________
www.blake-foster.com |
|
#5
|
|||
|
|||
Re: got a bit of prob herehaha 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
|
||||
|
||||
Re: got a bit of prob hereWhy 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
|
|||
|
|||
Re: got a bit of prob herei think i get what u mean by it...how should i make it when i type "jun" it display 6?
|
|
#8
|
||||
|
||||
Re: got a bit of prob hereJust call the getMonth function (you'll need to finish implementing it
CPP / C++ / C Code:
If you don't want it to be case-sensitive, convert the input to lower case. __________________
www.blake-foster.com |
|
#9
|
|||
|
|||
Re: got a bit of prob hereCPP / C++ / C Code:
i try to do like this...i type jan it will return 1, but i type other it also return 1...why? |
|
#10
|
|||
|
|||
Re: got a bit of prob hereYour variable count only gets incremented when it is equal to the input. Maybe just return i when that happens instead:
CPP / C++ / C Code:
|
Recent GIDBlog
Problems with the Navy (Officers) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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 19: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 19:25 |
| vector prob | nattylife | C++ Forum | 1 | 15-Nov-2003 07:41 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The