Home > Software design >  Why isn't my integer printing out properly in C
Why isn't my integer printing out properly in C

Time:01-19

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?

CodePudding user response:

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 u\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 u\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 u\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
  • Related