Execution priority question

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Execution priority question

 
0
  #1
Jun 6th, 2009
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

  1. #include <stdio.h>
  2. #include<string>
  3.  
  4. int IsValidAge(int age, char *er){
  5. int val = (age>=18 && age <= 100);
  6. if(!val) {
  7. strcpy(er, "Invalid age for driving, enter values between 18 and 100: ");
  8. }
  9. else{ er[0] = 0; }
  10. return val;
  11. }
  12. int IsValidMark(int mark, char *er){
  13. int val = (mark>=0 && mark <= 100);
  14. if(!val) {
  15. strcpy(er, "Invalid Mark, (0<=mark<=100): ");
  16. }
  17. else{
  18. er[0] = 0;
  19. }
  20. return val;
  21. }
  22.  
  23. int getInt( int (*IsValid)(int, char *)){
  24. int num;
  25. int ret;
  26. int valid = 1;
  27. char newline;
  28. char ErrMes[121];
  29. do{
  30. fflush(stdin);
  31. ret = scanf("%d%c", &num,&newline);
  32. if(IsValid != NULL){
  33. valid = (*IsValid)(num,ErrMes);
  34. }
  35. }while(!(valid && ret == 2 && newline == '\n') && printf(ErrMes));
  36. return num;
  37. }
  38.  
  39. int main(void){
  40. int age;
  41. int mark;
  42. printf("Enter Your Age: ");
  43. age = getInt(IsValidAge);
  44. printf("Enter your driving test mark: ");
  45. mark = getInt(IsValidMark);
  46. printf("you are %d years old and got %d in test\n", age, mark);
  47. return 0;
  48. }
Last edited by atman; Jun 6th, 2009 at 9:45 pm. Reason: forgot
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Execution priority question

 
0
  #2
Jun 6th, 2009
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 :
  1. printf("Enter Your Age: "); // Executes 1st
  2. age = getInt(IsValidAge); // Executes 2nd
  3. printf("Enter your driving test mark: "); //Executes 3rd
  4. 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);
}
This is the call by function pointers.
Last edited by csurfer; Jun 6th, 2009 at 9:58 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Execution priority question

 
0
  #3
Jun 6th, 2009
Moved to C forum because it is a C program, not c++.

>>fflush(stdin);
Non-standard use of fflush(). There is no standard way to flush the input stream, and fflush() is only defined to flush output streams to the file on disk (e.g. your hard drive).
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Execution priority question

 
0
  #4
Jun 7th, 2009
> 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
  1. printf("Enter Your Age: ");
  2. fflush( stdout );
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC