Compiler errors:
pythagoras.c: In function ‘main’:
pythagoras.c:22:4: fejl: ‘else’ without a previous ‘if’
pythagoras.c:29:5: fejl: ‘else’ without a previous ‘if’
The code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c;
double secondside1, secondside2, secondside3;
char Scanres;
printf("Hvilken side vil du finde? a, b eller c?\n");
Scanres = scanf("%c", &Scanres);
secondside1 = a * a;
secondside2 = b * b;
secondside3 = c * c;
if(Scanres = c);
printf("Indtast værdierne af a og b\n");
scanf("%lf%lf", &a, &b);
c = sqrt(secondside1 + secondside2);
printf("c er %f", c);
else if(Scanres = b);
printf("Indtast værdierne af a og c\n");
scanf("%lf%lf", &a, &c);
b = sqrt(secondside3 - secondside2);
printf("b er %f\n", b);
else(Scanres = a);
printf("Indtast værdierne af b og c\n");
scanf("%lf%lf", &b, &c);
a = sqrt(secondside3 - secondside1);
printf("a er %lf", a);
return 0;
}
The comparison operator in C is
==. With a single=you are doing assignment.Also, having a semicolon after the
if()makes it execute an empty statement, rather than the code path you thought.Note that indentation is free in C, it carries no meaning so the compiler won’t warn you if it seems non-sensical.
Further, this:
doesn’t make a lot of sense. The
scanf()function will write the character it read intoScanres, but then you will overwrite that value by storing the return value ofscanf()(which is the number of conversions it made) into the same variable.