| | |
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 |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





