Reputation: 11
I need my code to terminate when a word such as "exit" is typed into the first data input. I do not know that proper function or method to achieve this. My data is correct just need to terminate with a word command. Please any advice or tips with explanation would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
double xminA, xmaxA, yminA, ymaxA, xminB, xmaxB, yminB, ymaxB;
double xminC, xmaxC, yminC, ymaxC;
char end;
while(1){
printf("Rectangle A:");
scanf("%lf%lf%lf%lf", &xminA, &yminA, &xmaxA, &ymaxA);
printf("Rectangle B:");
scanf("%lf%lf%lf%lf", &xminB, &yminB, &xmaxB, &ymaxB);
if(xminA>xmaxB || xminB>xmaxA || yminA>ymaxB || yminB>ymaxA){
printf("No overlapping area\n");
}
else if(xminA<=xmaxB || xminB<=xmaxA || yminA<=ymaxB || yminB<=ymaxA){
xminC= fmax(xminA,xminB);
xmaxC= fmin(xmaxA, xmaxB);
yminC= fmax(yminA, yminB);
ymaxC= fmin(ymaxA, ymaxB);
printf("Overlap rectangle: (%lf,%lf) (%lf,%lf)\n", xminC, yminC, xmaxC, ymaxC);
}
}
return EXIT_SUCCESS;
}
Upvotes: 0
Views: 163
Reputation: 11
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
double t, xminA, xmaxA, yminA, ymaxA, xminB, xmaxB, yminB, ymaxB;
double xminC, xmaxC, yminC, ymaxC;
while(1){
printf("Rectangle A:");
if (0 == scanf("%lf", &t)){ //<--this if() statement causes the function to terminate at a non-numeric
printf("Program completed normally");
break;
}
t=xminA;
scanf("%lf%lf%lf",&yminA, &xmaxA, &ymaxA);
printf("Rectangle B:");
scanf("%lf%lf%lf%lf", &xminB, &yminB, &xmaxB, &ymaxB);
if(xminA>xmaxB || xminB>xmaxA || yminA>ymaxB || yminB>ymaxA){
printf("No overlapping area\n");
}
else if(xminA<=xmaxB || xminB<=xmaxA || yminA<=ymaxB || yminB<=ymaxA){
xminC= fmax(xminA,xminB);
xmaxC= fmin(xmaxA, xmaxB);
yminC= fmax(yminA, yminB);
ymaxC= fmin(ymaxA, ymaxB);
printf("Overlap rectangle: (%lf,%lf) (%lf,%lf)\n", xminC, yminC, xmaxC, ymaxC);
}
}
return EXIT_SUCCESS;
}
Upvotes: 0
Reputation: 53006
To achieve this you need to acknowledge that all the input is text, then you can use fgets()
to read a whole line of input, check whether it is exit\n
and then if it's not proceed to convert the input into the numbers that you want, checking if they were successfully converted and taking action in case they weren't.
Example
char line[256];
if (fgets(line, sizeof line, stdin) == NULL)
return -1; // Something bad happened
if (strcmp(line, "exit\n") == 0)
return 0; // The user typed `exit'
if (sscanf(line, "%lf%lf%lf%lf &xminA, &yminA, &xmaxA, &ymaxA) == 4) {
// Got the first rectangle, now do the same for the second
// rectangle
}
So as you see, this should be a function so that you allow the user to type exit
after the first rectangle input succeeds.
This could be further refined, to a level where you make your user very comfortable and happy.
Also, since you don't check the return value of scanf()
your program is unable to tell whether the input was correct (the expected 4 floating point numbers) or if it failed, scanf()
returns a value and you can understand it's meaning if you read some documentation.
Upvotes: 2