Thread: Odd data entry error, reading an unsigned integer with scanf

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    197

    Odd data entry error, reading an unsigned integer with scanf

    Hi all, this is probably the first time I have ever actually asked a question on the board, so Ill give it my best.

    This is a simple homework assignment, and I have pretty much finished except for a weird bug that won't go away. The point of the assignment is dealing with how to store data in a much smaller fashion, what I am doing is taking input from the user, dealing with a students record of some sort. It stores the persons current age, grade, gender, and current gpa al in a 16bit integer (short on my system thats why I used it). Well every thing is fine except that the first value I read in (Age) is always a 0, no matter what I enter. The code and a sample run below

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define AGE_MASK 0xF000;
    #define GRADE_MASK 0x0F00;
    #define SEX_MASK 0x0080;
    #define GPA_MASK 0x007E;
    
    int main()
    {
        unsigned short studentinfo, age, grade, sex, gpa;
        unsigned char gender;
        printf("Please enter the students Age, Grade, Sex as 'M' or 'F',\n");
        printf("and GPA without the decimal point.\n");
        /*
        scanf(" %u %u %c %u", &age, &grade, &gender, &gpa);
        printf("%u %u %c %u\n", age, grade, gender, gpa);*/
    
        printf("AGE: ");
        scanf(" %u",&age);
        printf("Grade: ");
        scanf(" %u",&grade);
        printf("Sex: ");
        scanf(" %c",&gender);
        printf("GPA: ");
        scanf(" %u",&gpa);
        printf("%u %u %c %u\n", age, grade, gender, gpa);
        if( isalpha(gender) != 0)
        {
            gender = toupper(gender);
        }
        else
        {
            printf("Error: You need to enter a character for sex\n");
            exit(EXIT_FAILURE);
        }
    
        if( gender == 'M')
        {
            sex = 1;
        }
        else if( gender == 'F' )
        {
              sex = 0;
        }
        else
        {
                printf("Error: You need to enter M or F for the students sex\n");
                exit(EXIT_FAILURE);
        }
    
        age = age - 3;
    
        studentinfo = 0;
        studentinfo |= (age << 12) & AGE_MASK;
        studentinfo |= grade << 8 & GRADE_MASK;
        studentinfo |= (sex << 7) & SEX_MASK;
        studentinfo |= (gpa << 1)& GPA_MASK;
        printf("%u\n",studentinfo);
        printf("%u\n",age);
        exit(EXIT_SUCCESS);
    }
    Code:
    Please enter the students Age, Grade, Sex as 'M' or 'F',
    and GPA without the decimal point.
    AGE:     14
    Grade:  10
    Sex: F
    GPA: 35
    0 10 F 35
    55878
    65533

    If you have any ideas let me know, I just don't understand why its getting 0 every time it reads the first value.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    hello.c:21: warning: unsigned int format, different type arg (arg 2)
    hello.c:23: warning: unsigned int format, different type arg (arg 2)
    hello.c:27: warning: unsigned int format, different type arg (arg 2)

    My guess is you should use %hu for reading in unsigned shorts

    gcc -Wall prog.c
    saves the day yet again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by Salem
    hello.c:21: warning: unsigned int format, different type arg (arg 2)
    hello.c:23: warning: unsigned int format, different type arg (arg 2)
    hello.c:27: warning: unsigned int format, different type arg (arg 2)

    My guess is you should use %hu for reading in unsigned shorts

    gcc -Wall prog.c
    saves the day yet again.

    LOL, I should have thought of using -Wall, doh. need to read the gcc manual again.

    And thank you, that did fix the problem, I feel so stupid for the moment.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-04-2009, 02:03 PM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM