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
#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;
}