Using SSCANF input string output two variables

This is a discussion on Using SSCANF input string output two variables within the C Programming forums, part of the General Programming Boards category; Needed some help getting this program to work correctly. The object of the program is to have a user input ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Using SSCANF input string output two variables

    Needed some help getting this program to work correctly. The object of the program is to have a user input a string for a date and then call a function to extract that date into two variables (month, day) and reprint the information. Not quite sure what I am doing wrong but it compiles and when I enter a date it comes back with the date is 4, 4254512. Any help would be greatly appreciated.

    I think the problem is probably in the function in the way of I am having trouble trying to figure out how to input a string and send two variables back to the main program. I am sure it is a simple solution that I cant figure out.
    Thanks
    Kevin



    #include <stdio.h>

    void seperate(char[]);
    char date[6];
    char month;
    int day;

    int main()
    {
    month = 0;
    day = 0;

    printf("Please input a month and date (i.e. JUN 10, MAY 22)\n\n");
    gets(date);
    seperate(date);
    printf("\n\nThe date you entered is %c, %d",&month, &day);
    return 0;
    }



    void seperate(month, day)

    {

    sscanf(date, "%c %d",&month,&day);

    return;
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Use code tags!!!

    void seperate(month, day)
    Nope!

    void seperate( char *input )
    Yep, for passing into the function. Now, why don't you think about returning it, and see what you come up with.

    Please read this before posting more code - << !! Posting Code? Read this First !! >>

    also, gets() is bad, fgets() is better. There are LOADS of sample snippets on how to use it, so search the forums if you're unsure how to use it.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Code tags fgets

    Made the recommended changes now it says fgets has too few arguments to call and won't compile. Thanks for your help here is the updated code.


    Code:
    #include <stdio.h>
    void seperate(char[]);
    char date[6];
    char month;
    int day;
    
    int main()
    {
    month = 0;
    day = 0;
    
    printf("Please input a month and date (i.e. JUN 10, MAY 22)\n\n");
    fgets(date);
    seperate(date[]);
    printf("\n\nThe date you entered is %c, %d",&month, &day);
    return 0;
    }
    
    
    
    void seperate(char *date)
    
    {
    
    sscanf(date, "%c %d",&month,&day);
    printf("\n\nThe date you entered is %c, %d",&month, &day);
    return;
    }


    I think I need a cprogramming for dummies book.
    Last edited by Ken Fitlike; 10-02-2006 at 12:47 PM. Reason: added code tags

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    USE CODE TAGS!!!!!

    fgets(date, *You choose the size, it's a number*, stdin);

    if memory serves. The problem with gets is that it doesn't have a limit to it's size - buffer overflows

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    This code seems to be working with the exception of my output being only the first letter of the input month. For example Jun 10 comes out via printf as J, 10. I believe the problem is in sscanf somewhere but it is hard to figure it out. Thanks for everyone's help.


    PHP Code:

    #include <stdio.h>
    void seperate(char[]);
    char date[7];
    char month;
    int day;

    int main()
    {
    month 0;
    day 0;

    printf("Please input a month and date (i.e. JUN 10, MAY 22)\n\n");
    gets(date);
    seperate(date);
    printf("\n\nThe date you entered is %c, %d",monthday);
    return 
    0;
    }



    void seperate(char *date)

    {

    sscanf(date"%4c %3d",&month, &day);
    printf("\n\nThe date you entered is %c, %d",monthday);
    return;


  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Aside from your horrible use of gets, and your painfully tiny buffer, month is only declared as one single character. How can you expect to store 4 characters in it?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    how do I fix

    I am extremely limited in my knowledge and trying to learn the best I can but what is wrong with the use of my gets statement and how do I fix the single character limit for the month variable. Thanks for your time. I understand the buffer limit problem in that I limited it to 7 but since I wanted the date in the APR 04 type format I didnt see a need in any larger buffer.

    Thanks for all your help,
    Kevin

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How can you not know how to fix the problem of storing the month? How big is the month you're trying to store? Make your month an array that big, plus one more, for the null character (assuming you're going to treat it as a string).

    As to your first buffer size, assume your users are stupid, because most are. As such, what happens when they type in "January" instead of "JAN"? It breaks. You run past the end of your buffer, because you didn't read the FAQ on why gets should never be used.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    And if I dont treat month as an array

    What are my options if I would rather not have month as an array??

    Thanks

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have any options. You're telling it that it must be, because of the way you're taking your input. Read it in number form if you don't want a string. This will of course change the way you have to input it. But if you're using characters; specificially more than one, then you need an array.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 05:41 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 05:02 PM

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