0

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;
}
4
  • Well, you'd have to allow for the user to input "exit", and it all starts by checking the return value of scanf Commented Feb 6, 2017 at 11:30
  • currently if the user inputs "exit" the program displays "no overlapping area" infinitely. Commented Feb 6, 2017 at 11:32
  • @KamiKaze: "fgets is deprecated": What?! No. You mean gets() right? Commented Feb 6, 2017 at 12:02
  • 1
    @xing and alk. You are right, comment removed Commented Feb 6, 2017 at 14:32

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm afraid i don't understand, are you able to add comments on my code? or if there is any other way.
There are many ways, this one is simple and easy. Why don't you understand? And what?
0
#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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.