convert int month to string month

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 14731 Views - Last Post: 06 March 2013 - 05:43 AM Rate Topic: -----

#1 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

convert int month to string month

Posted 05 March 2013 - 01:18 PM

hi i have a program to sort out dates, i have been able to convert the date to integer but cant work out how to get it back to string how can i do that?


this is my month array and function to convert the given month to int
char* allMonths[]= {"January", "February", "March", "April",
                    "May", "June", "July", "August",
                    "September", "October", "November", "December"};

int monthToInt(char tempMonth[])
{
    for(int i = 0; i<12; i++)
    {
        if (tempMonth == allMonths[i])
        {
            return i;
        }
    }
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: convert int month to string month

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: convert int month to string month

Posted 05 March 2013 - 01:25 PM

First you can't use the comparison operator== with C-strings, you will need to use something like strcmp().

As for your actual question what have you tried?


Jim
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 5201
  • View blog
  • Posts: 13,946
  • Joined: 18-April 07

Re: convert int month to string month

Posted 05 March 2013 - 01:26 PM

Just use the index to access your array...

char* intToMonth(int index) {
	char* allMonths[]= {"January", "February", "March", "April",
                    "May", "June", "July", "August",
                    "September", "October", "November", "December"};
	return allMonths[index];
}



That should do the trick for you. Of course make sure it is in range of 0 through 11... you can do that with an if statement or use modulus to keep it in range.

I will leave that part to you. :)

This post has been edited by Martyr2: 05 March 2013 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#4 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: convert int month to string month

Posted 05 March 2013 - 01:40 PM

View PostMartyr2, on 05 March 2013 - 01:26 PM, said:

Just use the index to access your array...

char* intToMonth(int index) {
	char* allMonths[]= {"January", "February", "March", "April",
                    "May", "June", "July", "August",
                    "September", "October", "November", "December"};
	return allMonths[index];
}



That should do the trick for you. Of course make sure it is in range of 0 through 11... you can do that with an if statement or use modulus to keep it in range.

I will leave that part to you. :)/>



thanks that seems to print out dates just not the ones that get entered how do i call that correctly?
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 5201
  • View blog
  • Posts: 13,946
  • Joined: 18-April 07

Re: convert int month to string month

Posted 05 March 2013 - 02:01 PM

Uhhh well if they enter a date like 3/5/2013 (March 5th of 2013) then you would take off the 3, subtract 1 (since you based your array on zero index) and pass it to the function...

cout << "Month of the date you entered is: " << intToMonth(month - 1) << endl;



It is the opposite of your first function where you went from month to int, the function I showed is the int back to month.
Was This Post Helpful? 0
  • +
  • -

#6 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: convert int month to string month

Posted 05 March 2013 - 06:37 PM

thanks i understand that, by the looks of it the problem lies with my inttomonth function it doesnt seem to be converting the strings to int would you know why ?

ive tried this way
int monthToInt(char tempMonth[])
{
    for(int i = 0; i < 11; i++)
    {
        if (tempMonth == allMonths[i])
        {
            return i;
        }
    }
}



and this way
int monthToInt(char tempMonth[])
{
    for(int i = 0; i < 11; i++)
    {
        if (strcmp(tempMonth,allMonths[i]) == 0)
        {
            return i;
        }
    }
}



messed around with both but still value stays at 0 meaning every month printed is january
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: convert int month to string month

Posted 05 March 2013 - 06:45 PM

Maybe the month is not being found. Try returning -1 at the end of the function.
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: convert int month to string month

Posted 05 March 2013 - 06:48 PM

Can you post how you are calling the function? You might not be null terminating your input string.
Was This Post Helpful? 0
  • +
  • -

#9 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: convert int month to string month

Posted 06 March 2013 - 01:32 AM

using a for loop with temp variable

for(int i = 0; i < n; i++)
    {
        scanf("%s %d %d", & tempMonth[i], & date[i].day, & date[i].year);
        date[i].month = monthToInt(tempMonth);
    }


Was This Post Helpful? 0
  • +
  • -

#10 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: convert int month to string month

Posted 06 March 2013 - 02:36 AM

The problem is already done for you - to convert a month back to it's int, you simply check each of the strings in *allMonths[].

But in C, you don't use things like if(string1 == string2), instead you use if(strcmp(string1,string2) == 0)

Here's an example using your code:

#include <stdio.h>
#include <string.h>  //for strcmp()

int monthToInt(char tempMonth[],char *allMonths[]);

int main(void) {

   char* allMonths[]= {"January", "February", "March", "April",
      "May", "June", "July", "August", 
      "September", "October", "November", "December"};
   
   char tempMonth[10]; //was 9, thanks for noticing JackofAllTrades!
   int i;

   for(i=0;i<12;i++) {
      printf("%s is number %d\n",allMonths[i],i+1);
   }
   printf("\n\n");
   while(tempMonth[0] != '-1') {
      printf("Enter a month to see it's number [-1 to Quit]: ");
      fflush(stdout);
      scanf("%s",tempMonth);
      
      i=monthToInt(tempMonth,allMonths);
      if(i>=0)
         printf("That months number is: %d\n",i+1);
   
   }  
   return 0;
}

int monthToInt(char tempMonth[], char *allMonths[])
{
    for(int i = 0; i<12; i++)
    {
        if (strcmp(tempMonth,allMonths[i])==0)
        {
            return i;
        }
    }
    return 0;
}


This post has been edited by Adak: 06 March 2013 - 04:49 AM

Was This Post Helpful? 0
  • +
  • -

#11 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: convert int month to string month

Posted 06 March 2013 - 03:41 AM

its the beginning finding the int value of the month ive used strcmp,

the input should be
8
January 1 01
January 1 00
July 17 12
September 10 12
July 1 00
August 25 06
May 27 08
October 1 03

output
January 1 00
July 1 00
January 1 01
October 1 03
August 25 06
May 27 08
July 17 12
September 10 12

but am getting
January 1 0
January 1 0
February 1 1
January 1 3
January 25 6
January 27 8
January 10 12
January 17 12
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: convert int month to string month

Posted 06 March 2013 - 03:56 AM

char tempMonth[9];


That is not enough space to hold "September".

And what you've provided for code is nothing like what you've provided for expected input/output.

EDIT: Never mind, that was Adak's code. My bad, it's early.

Give us your full code.
Was This Post Helpful? 0
  • +
  • -

#13 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: convert int month to string month

Posted 06 March 2013 - 04:09 AM

View PostJackOfAllTrades, on 06 March 2013 - 03:56 AM, said:

char tempMonth[9];


That is not enough space to hold "September".

And what you've provided for code is nothing like what you've provided for expected input/output.

EDIT: Never mind, that was Adak's code. My bad, it's early.

Give us your full code.


haha no worries heres the program in full

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct dates
{
    int day;
    int month;
    int year;
};

char* allMonths[]= {"January", "February", "March", "April",
                        "May", "June", "July", "August",
                    "September", "October", "November", "December"};


int monthToInt(char tempMonth[])
{
    for(int i = 0; i < 12; i++)
    {
        if (strcmp(tempMonth, allMonths[i]))
        {
            return i;
        }
    }
}

char* monthToString(int index)
{
    return allMonths[index];
}

int cmp(const void* p1, const void* p2)
{
    if  (((struct dates*)p1)->year < ((struct dates*)p2)->year)
    {
        return -1;
    }
    else if(((struct dates*)p1)->year > ((struct dates*)p2)->year)
    {
        return 1;
    }
    
    if  (((struct dates*)p1)->month < ((struct dates*)p2)->month)
    {
        return -1;
    }
    else if(((struct dates*)p1)->month > ((struct dates*)p2)->month)
    {
        return 1;
    }
    
    if  (((struct dates*)p1)->day < ((struct dates*)p2)->day)
    {
        return -1;
    }
    else if(((struct dates*)p1)->day > ((struct dates*)p2)->day)
    {
        return 1;
    }
    
    else return 0;
}

int main() {
    
    int n;
    char tempMonth[100];
    
    scanf("%d", & n);

    struct dates *date;
    date = (struct dates*)malloc(sizeof(struct dates) *n);

    for(int i = 0; i < n; i++)
    {
        scanf("%s %d %d", & tempMonth[i], & date[i].day, & date[i].year);
        date[i].month = monthToInt(tempMonth);
    }
 
    qsort(date, n, sizeof(struct dates), cmp);
    
    for(int i = 0; i < n; i++)
    {
        printf("%s %d %d\n", monthToString(date[i].month), date[i].day, date[i].year);
    }
}


Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: convert int month to string month

Posted 06 March 2013 - 04:21 AM

Holds one, single C string.
char tempMonth[100];


To use it, you do not do this:
scanf("%s %d %d", & tempMonth[i], & date[i].day, & date[i].year);


you do this:
scanf("%s %d %d", tempMonth, & date[i].day, & date[i].year);


The name of an array is a pointer to the first element in the array.

If you're compiling this as C, you should not be casting the return value of malloc here:
date = (struct dates*)malloc(sizeof(struct dates) *n);


It's unnecessary and can hide other issues.
Was This Post Helpful? 0
  • +
  • -

#15 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: convert int month to string month

Posted 06 March 2013 - 05:00 AM

output added another February to the list (N)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2