| | |
Why isnt this working? printf doesnt print variable inside a if block? Works outside?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 8
Reputation:
Solved Threads: 0
Why isnt this working? printf doesnt print variable inside a if block? Works outside?
0
#1 Mar 5th, 2009
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!
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)
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; }
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
Re: Why isnt this working? printf doesnt print variable inside a if block? Works outside?
0
#2 Mar 5th, 2009
Why are you trying to read a double using "%d"? Use "%lf" instead.
•
•
•
•
scanf("%lf",&grade2);
•
•
Join Date: Feb 2009
Posts: 8
Reputation:
Solved Threads: 0
Re: Why isnt this working? printf doesnt print variable inside a if block? Works outside?
0
#3 Mar 5th, 2009
![]() |
Other Threads in the C++ Forum
- Previous Thread: Summit with C++
- Next Thread: Problem with linked lists using STL
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





