| | |
Execution priority question
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 48
Reputation:
Solved Threads: 0
Hello.,
I'm very green in c++, and at school we just had a first lesson on pointers to functions, and I'm going over teachers code using debugger and dont understrand why the line age = getInt(IsValidAge); executes before printf, i thought C++ compiler executes lines sequentially. Any help would be greatly appreciated.
Thank you.
ps. The breakpoint sets to printf line but nothing goes on the screen, only after going into the getInt function it prints the line...
Regards,
andre
I'm very green in c++, and at school we just had a first lesson on pointers to functions, and I'm going over teachers code using debugger and dont understrand why the line age = getInt(IsValidAge); executes before printf, i thought C++ compiler executes lines sequentially. Any help would be greatly appreciated.
Thank you.
ps. The breakpoint sets to printf line but nothing goes on the screen, only after going into the getInt function it prints the line...
Regards,
andre
C Syntax (Toggle Plain Text)
#include <stdio.h> #include<string> int IsValidAge(int age, char *er){ int val = (age>=18 && age <= 100); if(!val) { strcpy(er, "Invalid age for driving, enter values between 18 and 100: "); } else{ er[0] = 0; } return val; } int IsValidMark(int mark, char *er){ int val = (mark>=0 && mark <= 100); if(!val) { strcpy(er, "Invalid Mark, (0<=mark<=100): "); } else{ er[0] = 0; } return val; } int getInt( int (*IsValid)(int, char *)){ int num; int ret; int valid = 1; char newline; char ErrMes[121]; do{ fflush(stdin); ret = scanf("%d%c", &num,&newline); if(IsValid != NULL){ valid = (*IsValid)(num,ErrMes); } }while(!(valid && ret == 2 && newline == '\n') && printf(ErrMes)); return num; } int main(void){ int age; int mark; printf("Enter Your Age: "); age = getInt(IsValidAge); printf("Enter your driving test mark: "); mark = getInt(IsValidMark); printf("you are %d years old and got %d in test\n", age, mark); return 0; }
Last edited by atman; Jun 6th, 2009 at 9:45 pm. Reason: forgot
Your code doesn't involve any c++ concepts. It is completely a c code I should say.And I think you are getting confused the order of execution is :
And this code involves a more subtle concept called function pointers.Read this.
The code getInt is called first which in turn executes the function IsValidAge and IsValidMark in lines :
This is the call by function pointers.
c Syntax (Toggle Plain Text)
printf("Enter Your Age: "); // Executes 1st age = getInt(IsValidAge); // Executes 2nd printf("Enter your driving test mark: "); //Executes 3rd mark = getInt(IsValidMark); //Executes 4th
And this code involves a more subtle concept called function pointers.Read this.
The code getInt is called first which in turn executes the function IsValidAge and IsValidMark in lines :
•
•
•
•
if(IsValid != NULL)
{
valid = (*IsValid)(num,ErrMes);
}
Last edited by csurfer; Jun 6th, 2009 at 9:58 pm.
I Surf in "C"....
> why the line age = getInt(IsValidAge); executes before printf
Standard out is buffered. The printf happened, in the sense that the resulting string was stored in the standard out buffer maintained by the library. But what didn't happen was that buffer being sent to the outside world so you could see it.
If you want to guarantee you'll see it, then you need to do this
Note that this is correct use of fflush().
Your use of fflush(stdin) (which is NOT an output stream) is broken.
In either language, use fgets() for C or cin.getline() for C++, I would suggest you read a WHOLE line into memory, then parse that line from memory. In your most immediate case, this would mean using sscanf().
Oh, and as already mentioned, decide whether you want to be a C OR C++ programmer. Crazy mixes of c/c++ is not an option.
Standard out is buffered. The printf happened, in the sense that the resulting string was stored in the standard out buffer maintained by the library. But what didn't happen was that buffer being sent to the outside world so you could see it.
If you want to guarantee you'll see it, then you need to do this
C Syntax (Toggle Plain Text)
printf("Enter Your Age: "); fflush( stdout );
Your use of fflush(stdin) (which is NOT an output stream) is broken.
In either language, use fgets() for C or cin.getline() for C++, I would suggest you read a WHOLE line into memory, then parse that line from memory. In your most immediate case, this would mean using sscanf().
Oh, and as already mentioned, decide whether you want to be a C OR C++ programmer. Crazy mixes of c/c++ is not an option.
![]() |
Similar Threads
- a simple question for priority queue? (C++)
- Out-of-order execution and semaphores question (Assembly)
- Printing Debug Message in Visual Studio C++ 2005 (C++)
- Very Very Urgent...Need Code for Calculating Execution Time For Jsp Page (JSP)
- Preventing Execution of standalone EXEs from Flash Drive (Network Security)
- String literial question (PHP)
- Traveling Salesman Problem ... question about Triangle Inequality (Computer Science)
- How to prevent statement execution when importing modules? (Python)
- I would kill for this, just a little bit (C)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the C Forum
- Previous Thread: Getting wrong answer while returning address (but right if I return value)
- Next Thread: Scrabble in C
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






