Code: Select all
#include <stdio.h>
struct {
int bar, baz, qux;
} foo;
main()
{
foo.bar = 0;
foo.baz = 0;
foo.qux = 0;
scanf("%d", &foo.bar);
printf("%d\n", foo.bar);
scanf("%d", &(foo).baz);
printf("%d\n", foo.baz);
scanf("%d", &(foo.qux));
printf("%d\n", foo.qux);
}
The problem looks to be with the scanf() and printf format strings; you are using it to fill and print chars, but you are using the formatting code for integers (%d). You will need to use %c instead. The lack of typechecking in these two functions, while occasionally useful, is a serious problem in using them. Here's a modified version of the code below, with a few changes to make it easier to work with:
Code: Select all
#include <stdio.h>
typedef struct{
char sec;
char min;
char hr;
int day;
} time;
time addtime(time t1,time t2)
{
time op;
op.min=0;
op.hr=0;
op.sec = t1.sec + t2.sec;
if(op.sec>59){
op.sec %= 60;
op.min++;
}
op.min += t1.min + t2.min;
if(op.min>59){
op.min %= 60;
op.hr++;
}
op.hr += t1.hr + t2.hr;
return op;
}
main()
{
time op,tim1,tim2;
/* initializing op */
op.hr = '0';
op.sec = '0';
op.min = '0';
printf("initial time: \n%c:%c:%c\n",op.hr,op.min,op.sec);
tim1.sec=40; tim1.min=15; tim1.hr=11;
fflush(stdin);
puts("Hour: ");
scanf("%c", &tim2.hr);
fflush(stdin);
puts("Minute: ");
fflush(stdin);
scanf("%c", &tim2.min);
fflush(stdin);
puts("Second: ");
scanf("%c", &tim2.sec);
printf("\nTime2= %c:%c:%c",tim2.hr,tim2.min,tim2.sec);
op=addtime(tim1,tim2);
printf("Corrected Time: \n%c:%c:%c",op.hr,op.min,op.sec);
getch();
}
This reads and prints the data correctly, though the addtime function still needs work, it seems. HTH.