I have a while loop in my main function which calls a user defined function which calls another user defined function which calls scanf to read a pair of double values. I want my program to end when a non-numeric is entered. I have my functions returning structures, and not 0/1. Is there a way to pass a false scanf return all the way through to break my while loop?
Can I declare a global variable and assign it to the scanf statement and then make an if statement about that variable in the main function?
Or will I have to change my functions to return int (0/1) and pass in my structures by reference?
This is probably a really bad explanation so here's my code for the parts involved in my question... The program is meant to read in a rectangle (by bottom left point and top right point) and a circle and say if their areas overlap. And I'm looking to put a break in the while loop in the main function...
typedef struct{double x,y;} point;
typedef struct{point a,b,c,d;} rectangle;
typedef struct{point o; double r;} circle;
point readPoint(){
point p;
scanf("%lf%lf", &p.x, &p.y); //type "quit"
return p;
}
rectangle readRectangle(){
rectangle r;
r.c=readPoint();
r.b=readPoint();
r.a.x=r.c.x;
r.a.y=r.b.y;
r.d.x=r.b.x;
r.d.y=r.c.y;
return r;
}
int main(void) {
int a=0;
rectangle r;
circle c;
while(1){
printf("Enter rectangle:");
r=readRectangle(); //break loop here if quit is entered
printf("Enter circle:");
c=readCircle();
a=overlap(r, c);
if (a==1)
printf("The rectangle and the circle overlap.\n\n");
else
printf("The rectangle and the circle do not overlap.\n\n");
}
printf("Program completed normally.");
return EXIT_SUCCESS;
}
point *readPoint(point *p), then check the return ofscanfandif (scanf("%lf%lf", &p->x, &p->y) < 2) return NULL; return p;and continue that logic back up the chain.