![](/img/trans.png)
[英]My program saying it does not have enough memory to allocate I tried everything but still wont work please give any help :c
[英]If and else statements wont work in this small program, help please :)
编译器错误:
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’
编码:
#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;
}
if
语句的语法为:
if (expr) statement;
statement
可能是一个指令块,因此您必须添加方括号。
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
{
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;
}
else
不需要表达式。 ==
是比较运算符,不是=
。 a
, b
和c
未初始化。 scanf
返回值是成功匹配和分配的输入项目数,而不是读取值。 您应该阅读基础。
C中的比较运算符为==
。 仅用一个=
您就可以进行分配。
另外,在if()
之后加上分号会使它执行一个空语句,而不是您认为的代码路径。
请注意,缩进在C语言中是免费的,它没有任何意义,因此,如果缩进看起来毫无意义,则编译器不会警告您。
此外,这:
Scanres = scanf("%c", &Scanres);
没有任何意义。 scanf()
函数会将其读取的字符写入Scanres
,但是您将通过将scanf()
的返回值(即其进行的转换数)存储到同一变量中来覆盖该值。
嘿,删除半冒号
if(Scanres = c);
并使用像
if ( statement)
{
code
}
else if ( statement )
{
code
}
else
{
code
}
这应该解决。 请确保您正确输入定义为“ a”,“ b”和“ c”的变量
#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c;
a = 10 ;
b = 12 ;
c = 15 ;
// Here you must make it that the user inputs the 3 variables ; I have entered them manually first
// Use scanf or fgets to input the 3 variables. And there you go !!
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 if(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;
}
您的if / else语法全都没用,您需要使用大括号{}
来标识与每个条件相关联的块。 此外,使用==
运算符测试是否相等:
if (Scanres == c) { /* Start the block for this case */
/* ... */
} else if (Scanres == b) { /* Here's how you do an "else if" block. */
/* ... */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.