这段代码1的错误是什么?

时间:2013-12-29 20:22:04

标签: c

我纠正了所有错误,但编译找到一个错误,我找不到

  #include <stdio.h>
  #include <conio.h>
  int main(void)

{

    float side,base,heigh;
    char a;
    printf("S-square\nT-traingle");
    scanf("%c",&a);
    if(a=='S'||a=='s')
    {

        printf("enter the value of side");
        scanf("%f",&side);
        printf("area=%f",side*side);
    }

    else if(a=='t'||a=='T')
    printf("enter the base,heigh");
    scanf("%f%f",&base,&heigh);
    printf("area=%f",0.5*heigh*base);

    else  
    printf("not valid choise");

}

此代码使用“if”循环查找traingle和squre的区域 但我无法找到错误在哪里

2 个答案:

答案 0 :(得分:2)

您错过了{}

的大括号else
 else if(a=='t'||a=='T')
{
     printf("enter the base,heigh");
     scanf("%f%f",&base,&heigh);
     printf("area=%f",0.5*heigh*base);
}  

没有大括号,编译器会将此代码段视为

else if(a=='t'||a=='T')
{
     printf("enter the base,heigh");
}
scanf("%f%f",&base,&heigh);
printf("area=%f",0.5*heigh*base); 

以及下一个else导致错误,因为它之前没有ifelse必须拥有其之前的if)。

答案 1 :(得分:2)

您错过了if声明不同部分的大括号:

else if(a=='t'||a=='T') {
    printf("enter the base,heigh");
    scanf("%f%f",&base,&heigh);
    printf("area=%f",0.5*heigh*base);
} else {
    printf("not valid choise");
}

虽然最后一对括号实际上并不是必需的,但总是使用它们以避免像你所拥有的问题一样被认为是好习惯。