So I'm working on a Zeller's Rule c program, trying to break a 4 digit int that represents the year of a specific date into 2 different int variables containing the first 2 digits and last 2 digits of said year.
void main()
{
int day, month, year;
.
.
.
printf("Enter a Year: ");
scanf("%i",&year);
.
.
.
int data, i;
int split[3];
for(i=3 ; i>=0 ; i--) //Problem is in the loop
{
data = year % 10;
split[i] = data;
year /= 10;
printf("Data%i: %i, should be %i\n", i, split[i], data);
}
}
The above code outputs: (int year = 1234)
Data3: 0, should be 4
Data2: 0, should be 0
Data1: 0, should be 0
Data0: 0, should be 0
However, if i modify the loop labeled as the problem above as:
int data, i;
int split[3];
for(i=3 ; i>=0 ; i--) //Data is all there, correctly
{ //Problem arises when I try to store to my array
data = year % 10;
year /= 10;
printf("Data%i: should be %i\n", i, data);
}
output of above code changes: (int year = 1234)
Data3: should be 4
Data2: should be 3
Data1: should be 2
Data0: should be 1
I am completely lost as to why the 开发者_开发问答program 0's out everything as soon as I try to put the data that IS THERE into my array. Been giving me a headache for hours as I obviously have no idea what the problem is.
You have 3 cells in your spit array (int split[3]
), but you assign 4 times into it:
for(i=3 ; i>=0 ; i--)
change it to int split[4]
.
精彩评论