Extract values and store in new variable

This is a discussion on Extract values and store in new variable within the C Programming forums, part of the General Programming Boards category; My assignment is: Write a C program that accepts a month and day (for example, June 14) from the keyboard ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Extract values and store in new variable

    My assignment is:

    Write a C program that accepts a month and day (for example, June 14) from the keyboard as input. Store this information in one string called date. Call a function named separate() passing in the string date and the addresses of a tempmonth array and tempday integer. The separate() function should extract the two values from the passed string and store them into the passed variable addresses. Back in main, print the data in tempmonth and tempday.

    so far I have come up with

    Code:
    #include<stdio.h>
    int main()
    {
    #define DSIZE 15
        void separate(char [], char *, int *);
        char date[DSIZE];
        char tempmonth[10];
        int tempday;
        
    
        printf("\nPlease enter a date, (ex. January 28): ");
          gets(date);
        separate(date, tempmonth, &tempday);
    
        printf("Month: ");
        puts(tempmonth);
        printf("Day: %d",tempday);
    
        return 0;
    }
    
    void separate(char date[], char *tempmonth_addr, int *tempday_addr)
    {
        sscanf(date,"c%d",tempmonth_addr, tempday_addr);
    }
    the output value is a bunch of strange information,

    any help please

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Never use gets: http://cpwiki.sourceforge.net/Gets
    You are not passing the size of tempmonth, and so you cannot check for buffer overflow.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    (1) You need a percent sign in front of a format specifier like %c.
    (2) You don't want to use %c anyway, since %c will only read in a single character. To read in a group of characters, use %s.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    That was exactly what I need, Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Storing int and double type values under one variable name
    By Thanuja91 in forum C++ Programming
    Replies: 10
    Last Post: 10-30-2007, 05:15 AM
  3. Trying to store lots of values in a vector?
    By DARKGuy in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2007, 04:41 PM
  4. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  5. loop to store values
    By keendoc in forum C++ Programming
    Replies: 1
    Last Post: 10-28-2005, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21