2013-04-11 105 views
2

我是一名尝试使用VS 2012 Ultimate学习C编程的初学者。错误:预期“;”

我刚刚学会了如何制作一个“摄氏到华氏”转换器,所以我决定进一步把它放到“球体积”计算器中。这是我键入:

#include <stdio.h> 
#include <string.h> 

char line[100]; /*Line input by the user*/ 
char line_2[100]; /*Second line input by the user*/ 
float radius; /*Value of the radius input by the user*/ 
float pi; /*Value of the pi input by the user*/ 
float volume /*Result of the calculation*/ 
float result_volume; /*Result of the second calculation*/ 
float result_volume2; /*Result of the third calculation*/ 
float result_volume3; /*Result of the fourth calculation*/ 

int main() 
{ 
    printf("Please write the value of the radius: "); 
    fgets(line,sizeof(line),stdin); 
    sscanf(line,"%f",&radius); 
    printf("Please write the value of the Pi "); 
    fgets(line_2,sizeof(line_2),stdin); 
    sscanf(line_2,"%f",&pi); 
    volume = radius*radius*radius; 
    result_volume = volume *pi; 
    result_volume2 = result_volume*4; 
    result_volume3 = result_volume2/3; 
    printf("When the radius is %f, and the Pi, %f, then the volume of the sphere is: 
       %f\n", radius, pi, result_volume3); 
    return (0); 
} 

,当我尝试编译此,我不断收到错误:

Error: Expected a ";" on float result_volume. 

在哪里我犯这样的错误,我怎么能解决这个问题?请帮忙!

回答

4

在你展示的代码中,;之后

float volume /*Result of the calculation*/ 

缺少它应该是:

float volume; /*Result of the calculation*/ 

注:当你得到这样的错误,通常情况下,你应该看之前的行发生错误的行。在你的情况下,这是前一行。

会发生什么情况是,编译器仅在下一行到达;时才会看到问题。在那里,它意识到整个两条线不会做出单一的命令,并且应该在某处发生切割。但它无法告诉哪里。所以,它会在发现错误时标记错误,而不是实际发生的位置。

但是,该领域已经有了重大改进,例如使用Clang,MAC OS X IDE Xcode能够在需要的地方准确地提示;

0
float volume /*Result of the calculation*/ 
float result_volume; /*Result of the second calculation*/ 

float volume

float volume; /*Result of the calculation*/ 
float result_volume; /*Result of the second calculation*/ 
后忘了一个分号( ;