-3

I am trying to just write something that takes a month and date and prints it back out. I have written the following code:

int main(void){
    char month[] = {};
    int day;
    printf("Please enter the month and day of you date. i.e January 01\n\n");
    scanf("%s,%d", month, &day);
    printf("Month is %s and the day is %d\n", month, day);
    return 0;
} 

When I input a date like December 22, I get the following print out: Month is December and date is 1. The day value is stuck printing as 1. Why isn't my day integer updating and is instead just staying stuck at 1?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Scott Roan
  • 17
  • 3
  • 1
    When you say `"%s,%d"`, scanf is expecting you to type a word, and a comma, and a number. So try typing "December,22", and if that's not what you want, change the scanf format to `%s %d`. – Steve Summit Jan 18 '23 at 19:18
  • 2
    To catch this sort of error, you can look at `scanf`'s return value. In this case, if `scanf` doesn't return 2, that means it didn't read everything you asked. So you could say `if(scanf("%s,%d", month, &day) != 2) { printf("input error!\n"); exit(1); }`. – Steve Summit Jan 18 '23 at 19:20
  • 3
    `month` is a zero-length array. – dbush Jan 18 '23 at 19:22
  • 3
    Question: ```char month[] = {};``` <---- What does this definition mean? – Harith Jan 18 '23 at 19:28
  • 1
    @SteveSummit More like try typing "December ,22" (notice the space.) Of course all is UB with the rump `month[]`. – chux - Reinstate Monica Jan 18 '23 at 19:33
  • Scott Roan, what was the return value of `scanf()`? What did you expect? – chux - Reinstate Monica Jan 18 '23 at 19:34
  • Scott Roan, How large a string do you think that can be stored in `month`? – chux - Reinstate Monica Jan 18 '23 at 19:36
  • @SteveSummit was correct. The ',' was what was causing the problem. Very new to C so didn't realize that was how it worked. Thanks! – Scott Roan Jan 18 '23 at 19:37
  • No, it doesn't work. Your code invokes undefined behaviour. – Harith Jan 18 '23 at 19:41
  • 1
    @ScottRoan: As hinted by some of the comments above, the `,` was not the only problem in your code. You must ensure that the array `month` is large enough to store the word. Writing `char month[] = {};` is equivalent to writing `char month[0] = {};`, so the array has a length of `0`, which means that it does not have enough space to store anything. – Andreas Wenzel Jan 18 '23 at 19:51
  • @AndreasWenzel oh ok! the program i wrote ran without error using month[] = {} but I'll make sure to update it and make sure I don't use an empty length any more! Thanks! – Scott Roan Jan 18 '23 at 21:30
  • 1
    @ScottRoan: When your program invokes undefined behavior (for example due to writing to an empty array), then anything may happen. This means that your program could crash or misbehave in some other way, but also means that your program could appear to work as intended. However, even if your program works as intended, you cannot rely on this happening, because even making a slight unrelated change to your program could cause your program to stop working as intended. Therefore, you should always avoid undefined behavior. – Andreas Wenzel Jan 18 '23 at 23:40

1 Answers1

1

This declaration

char month[] = {};

is invalid in C and C++.

At least you should write for example

char month[10];

In the prompt the format of the input date is shown without a comma

printf("Please enter the month and day of you date. i.e January 01\n\n");

But in the call of scanf

scanf("%s,%d", month, &day);

there is present a comma.

The program can look for example the following way

#include <stdio.h>

int main( void )
{
    char month[10];
    unsigned int day;

    printf( "Please enter the month and day of you date. i.e January 01\n\n" );

    if (scanf( "%9s %u", month, &day ) == 2)
    {
        printf( "Month is %s and the day is %02u\n", month, day );
    }
}

The program output might look like

Please enter the month and day of you date. i.e January 01

December 22
Month is December and the day is 22

If you want to include a comma in the input string then the program can look the following way

#included <stdio.h>

int main( void )
{
    char month[10];
    unsigned int day;

    printf( "Please enter the month and day of you date. i.e January, 01\n\n" );

    if (scanf( "%9[^,], %u", month, &day ) == 2)
    {
        printf( "Month is %s and the day is %02u\n", month, day );
    }
}

The program output might look like

Please enter the month and day of you date. i.e January, 01

January, 01
Month is January and the day is 01

Another approach is to use function fgets instead of scanf as for example

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

int main( void )
{
    char date[14];

    printf( "Please enter the month and day of you date. i.e January, 01\n\n" );

    int success = fgets( date, sizeof( date ), stdin ) != NULL;

    if (success)
    {
        const char *p = strchr( date, ',' );

        if (success = p != NULL)
        {
            char *endptr;

            unsigned int day = strtoul( p + 1, &endptr, 10 );

            if ( success = endptr != p + 1 )
            {
                printf( "Month is %.*s and the day is %02u\n", 
                    ( int )( p - date ), date, day );
            }
        }
    }
}

The program output might look like

Please enter the month and day of you date. i.e January, 01

January, 01
Month is January and the day is 01
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    If you're going to declare `char month[10]` make sure to use a width modifier with the `%s` format specifier in `scanf`. – Chris Jan 18 '23 at 19:54