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;

}

Recommended Answers

All 2 Replies

Why are you trying to read a double using "%d"? Use "%lf" instead.

scanf("%lf",&grade2);

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.