943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 880
  • C++ RSS
Mar 5th, 2009
0

Why isnt this working? printf doesnt print variable inside a if block? Works outside?

Expand Post »
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!
C++ Syntax (Toggle Plain Text)
  1. int main(void)
  2. {
  3. char fi='a';
  4. char li='a';
  5. double grade=0;
  6. int z=0;
  7. int t=4;
  8. printf("Entered %d students in Class \n",t);
  9. for (z=0;z<t;z++)
  10. {
  11. printf("Enter student %d's data. Ex:BM100 (Bob Marley has a 100) :: ",(z+1));
  12. char fi2='a';
  13. char li2='a';
  14. double grade2=0;
  15. scanf("%c%c",&fi2,&li2);
  16. //printf("\n");
  17. scanf("%d",&grade2);
  18. getchar();
  19. if (grade2>grade)
  20. {
  21. grade=grade2;
  22. fi=fi2;
  23. li=li2;
  24. printf("DEBUG-CHANGE! ADDED %d BY '%c' '%c' as highest grade!\n",grade,fi,li);
  25. printf("DEBUG-CHANGE2! ADDED %d BY %c %c as highest grade!\n",grade2,fi2,li2);
  26. }
  27.  
  28. printf("DEBUG-STUDENT %d DATA:: NAME:%c%c GRADE:%d",z,fi2,li2,grade2);
  29. }
  30.  
  31. return 0;
  32.  
  33. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crewxp is offline Offline
8 posts
since Feb 2009
Mar 5th, 2009
0

Re: Why isnt this working? printf doesnt print variable inside a if block? Works outside?

Why are you trying to read a double using "%d"? Use "%lf" instead.
Quote ...
scanf("%lf",&grade2);
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 5th, 2009
0

Re: Why isnt this working? printf doesnt print variable inside a if block? Works outside?

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crewxp is offline Offline
8 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Summit with C++
Next Thread in C++ Forum Timeline: Problem with linked lists using STL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC