0

I've got my Functions set up and Running, when i run the programm it just skips forward to the End, if i press Enter it asks me to enter values but calculates everything as 0.00.

Im quiet certain its either within my "struct SEGMENT calculate_segment_v1(struct SEGMENT s)" Function or the Function where one should enter the Values.

Any help appreciated!

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

double atan(double x);

struct POINT {
    double x;
    double y;
};

struct SEGMENT {
    struct POINT p1;
    struct POINT p2;
    double length;
    double angle;
};

double distance_of_points(struct POINT p1, struct POINT p2)
{
    double length;
    length = sqrt(pow((p1.x - p2.x), 2) + pow((p1.y - p2.y), 2));
    return length;
};

double angle_x(struct POINT p1, struct POINT p2)
{
    double angle;
    double berechnung;
    berechnung=(pow((p1.x - p2.x), 2)/(pow((p1.y - p2.y), 2)));
    angle = atan(berechnung);
    return angle;
}

void print_segment(struct SEGMENT s)
{
    printf("\n--> Die Strecke verbindet die Punkte"
           " (%.2f,%.2f) und (%.2f,%.2f);\n"
           "--> Sie hat eine Laenge von %.4f;\n"
           "--> Der Winkel betraegt: %.2f Grad.\n\n",
           s.p1.x, s.p1.y, s.p2.x, s.p2.y, s.length, s.angle);
}

struct SEGMENT read_segment_v1()
{
    struct SEGMENT s;
    printf(">> Bitte Koordinaten x,y des ersten"
           " Punktes der Strecke eingeben: ");
    scanf ("%lf%lf", &s.p1.x, &s.p1.y);
    printf(">> Bitte Koordinaten x,y des zweiten"
           " Punktes der Strecke eingeben: ");
    scanf ("%lf%lf", &s.p2.x, &s.p2.y);
    return s;
}

struct SEGMENT calculate_segment_v1(struct SEGMENT s)
{
    struct POINT X={s.p1.x, s.p1.y}, Y={s.p2.x ,s.p2.y};
    s.length = distance_of_points(X, Y);
    s.angle = angle_x(X, Y);
}

int main(void)
{
    struct SEGMENT S;
    char c;
    double l;

    while (1) {
        printf("Bitte # eingeben fuer Programmende: ");
        fflush(stdin);
        scanf ("%c", &c);

        if (c=='#') break;

        S = read_segment_v1();
        S = calculate_segment_v1(S);
        print_segment(S);
    }

    printf("Ende des Programms\n");
    return 0;
}
fbynite
  • 2,651
  • 1
  • 21
  • 25
Oof
  • 9
  • 2
  • 1
    `fflush(stdin);` = wrong; the standard only defines behavior for flushing *out* streams. And `"%c"` should be `" %c"` due to the remaining whitespace in the input stream after your read-segment input logic. [See this question for reasons why](https://stackoverflow.com/questions/20306659/the-program-doesnt-stop-on-scanfc-ch-line-why). – WhozCraig Nov 28 '18 at 18:07
  • The problem of the question author has nothing to do with stream flushing, however. There is a simple bug in the code. – KT. Nov 28 '18 at 18:15

1 Answers1

-1

There is a bug in your code. Add

return s;

to the end of your calculate_segment_v1 function to fix it.

PS: I can't help but digress to note that the code you have posted is rather bad in a number of respects besides this particular bug. If this is something that you have written while learning to code, please consider changing the learning path you have been taking so far.

KT.
  • 10,815
  • 4
  • 47
  • 71
  • Why not make specific examples of what could be improved in his code instead of bashing him for trying to learn - poor style on your part. – fbynite Nov 28 '18 at 18:19
  • Unnecessary flush of the stdin. Unnecessary re-creation of data structures inside the calculate_segment function. Unnecessary atan declaration. Unreasonable "functional" style of passing itself by value and returning. Lack of easy to read logical structure. Ugly code formatting (fixed by an edit to the question now). It is hard to point out all the flaws - the best would be to rewrite the code completely, but this is not what the author asked and this would not fix anything, if the problem stems from the current learning path. Hence I suggested to use a proper textbook or a course. – KT. Nov 28 '18 at 18:32
  • thanks for the help, im learning this in university, you could tell me where to improve tho :) – Oof Nov 28 '18 at 18:48
  • re-write 50 lines of code, lol - that's a mighty fine high-horse you're on. – fbynite Nov 28 '18 at 18:48
  • @Oof, then just find an experienced friend or a professor to go over this code with you. – KT. Nov 28 '18 at 18:55
  • @fbynite Not sure what horses have to do with this. Rewriting 50 lines of code takes time, solves no one's problem without detailed in-person explanations, and won't even be noted by anyone in this downvoted answer to a pseudo-duplicate question. If you disagree, feel free to contribute your own answer, of course! – KT. Nov 28 '18 at 18:58
  • idk why you'd be flaming my code if you won't tell me whats wrong thats just bs – Oof Nov 28 '18 at 19:09
  • @Oof please, read the second comment to this answer for a tentative list of things, that are wrong with the code. I do not "flame your code", whatever it means. I simply suggested you consider sitting down with someone more experienced to go over it with you. I do not mean to offend, this is a sincere advice which, I am convinced, would help you more than simply fixing the bug with the `return` statement. – KT. Nov 28 '18 at 19:13
  • @Oof Some of the criticisms is based on things that may not have been covered yet in class (assuming that I understood correctly). Usually structs are passed as pointers (reducing the number of variables that are used). Also structs can be assigned to each other instead of assigning each element. I would suggest that compiling with warnings enabled (i.e. -Wall with gcc) would be beneficial because it would have caught the missing return statement. In practice you also need to check the return values of the stdio functions (scanf) to ensure they are working correctly / as expected. – fdk1342 Nov 28 '18 at 23:25