user1912285
user1912285

Reputation: 112

Can not read unsigned char even if give "%hhu"

I have a problem in my codes, it does not work as expect when i read 3 unsigned char from keyboard, i already use fflush() to flush the buffer, but the 2nd variable can get value from keyboard. Here's my codes:

void main(){
    // input Date from keyboard
    unsigned char tmpDayOfWeek;
    unsigned char tmpDay;
    unsigned char tmpMonth;
    unsigned int tmpYear;
    printf("Please input your date:\n");
    printf("  Day of Week (0 for Sunday, 1 for Monday,...): "); 
    fflush(stdin);
    scanf("%hhu", &tmpDayOfWeek);
    printf("  Day (1..31): ");
    fflush(stdin);
    scanf("%hhu", &tmpDay);
    printf("  Month (1..12): ");
    fflush(stdin);
    scanf("%hhu", &tmpMonth);
    printf("  Year (1900..9999): ");
    fflush(stdin);
    scanf("%u", &tmpYear);
    printf(" dow = %d\n", tmpDayOfWeek);
    printf(" Day = %d\n", tmpDay);
    printf(" Month = %d\n", tmpMonth);
    printf(" Year = %d\n", tmpYear);

    fflush(stdin);
    cin.get();
    return;
}

When I run this code and input from keyboard 3, 4, 5 and 2000 the output is:

dow = 3 
Day = 0 
Month = 5 
Year = 2000

the value of tmpDay is 0, although i input 4 anyone plz help me, Thank, HoangVi P/S: I use VC++ 2005

Upvotes: 0

Views: 868

Answers (1)

Jochen Kalmbach
Jochen Kalmbach

Reputation: 3684

There is no format specifier which allows to parse an ´unsigned char´ as a "value"!

Please read the documentation at:

The "shortes" value to parse with scanf is short unsigned int => %hu

Upvotes: 1

Related Questions