0

I have a small problem. I'm curious to see why this happens. If you run this code and enter the data, I put in a debug line. Inside the 'if (grade2>grade) block, the DEBUG printf's don't show the data correctly.

Outside the if block, I print pretty much the same line. It shows it correctly on that line.

Anyone know why? I'm learning a lot about c as I go. Thanks!

int main(void)
{
		char fi='a';
        char li='a';
        double grade=0;
        int z=0;
        int t=4;
		printf("Entered %d students in Class \n",t);
        for (z=0;z<t;z++)
        {
                printf("Enter student %d's data. Ex:BM100 (Bob Marley has a 100) :: ",(z+1));
                char fi2='a';
                char li2='a';
                double grade2=0;
                scanf("%c%c",&fi2,&li2);
                //printf("\n");
                scanf("%d",&grade2);
				getchar();
                if (grade2>grade)
                {
                        grade=grade2;
                        fi=fi2;
                        li=li2;
						printf("DEBUG-CHANGE! ADDED %d BY '%c' '%c' as highest grade!\n",grade,fi,li);
						printf("DEBUG-CHANGE2! ADDED %d BY %c %c as highest grade!\n",grade2,fi2,li2);
                }

				printf("DEBUG-STUDENT %d DATA:: NAME:%c%c	GRADE:%d",z,fi2,li2,grade2);
  }

		return 0;

}
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by crewxp
0

thanks kbs. I looked at what you suggested and saw that I had mis-understood how C takes in datatypes. I fixed all the misrepresentations in my code and it works fine now.

Thanks :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.