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.