Escape with infinite loop char()

Asked 2 years ago, Updated 2 years ago, 27 views

// Enter code here
#include <stdio.h>
#include <math.h>

double dist(a, b, c, d) {
    double result;
    result = sqrt((double)(a - c)*(a - c) + (double)(b - d)*(b - d));
    return result;
}

int main(void) {
    char ent;
    int a, b, c, d;
    double distance;

    printf ("Enter the coordinates of the first point:");
    scanf("%d %d", &a, &b);
    printf ("Enter the coordinates of the second point:");
    scanf("%d %d", &c, &d);
    distance = dist(a, b, c, d);
    printf ("The distance between the two points is %."6lf.", distance);}

I squeezed it out like this...In this state "Do you want to do it again? Print out "" If you press y, do it again, and if you press n, the program will be displayed I'm going to end it, what should I do? I tried to implement it by using while(1) repeated sentence, break, and continue, but I keep getting errors...

c

2022-09-22 19:57

1 Answers

#include <stdio.h>
#include <math.h>

double dist(a, b, c, d) {
        double result;
        result = sqrt((double)(a - c)*(a - c) + (double)(b - d)*(b - d));
        return result;
}

int main(void) {
        char ent, yesno;
        int a, b, c, d;
        double distance; 

        do {
                printf ("Enter the coordinates of the first point:");
                scanf("%d %d", &a, &b);
                printf ("Enter the coordinates of the second point:");
                scanf("%d %d", &c, &d);
                distance = dist(a, b, c, d);
                printf ("The distance between the two points is %."6lf.\n", distance);
                printf ("Do you want to do it again? %c", getc(stdin)); 
        } } while(getc(stdin) == 'y');
}


2022-09-22 19:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.