2017-11-25 59 views
1

看起来这可能是O'Reilly出版的Head First C,2012,Griffiths and Griffiths中的一个错字。在p。 165,有一个练习:本书错误?功能上的不必要的分号(Head First C example,Griffiths&Griffiths 2012)

“有一个新的计划帮助头等第一餐馆的服务员巴士桌子,代码会自动汇总一张账单,并为每件物品增加销售税,看看你是否能找出需要去的东西在每个空白处“

该错误似乎在第7行,float add_with_tax(float f);。定义函数时,分号不应该在这里。我对此是否正确?

#include <stdio.h> 

float total = 0.0; 
short count = 0; 
short tax_percent = 6; 

float add_with_tax(float f); // we're returning a small cash value, so it'll be a float 
{ 
    float tax_rate = 1 + tax_percent/100.0; 
    total = total + (f * tax_rate); 
    count = count + 1; 
    return total; 
} 


int main(){ 
    float val; 
    printf("Price of item: "); 
    while (scanf("%f", &val) == 1){ 
     printf("Total so far: %.2f\n", add_with_tax(val)); 
     printf("Price of item: "); 
    } 
    printf("\nFinal total: %.2f\n", total); 
    printf("Number of items: %hi\n", count); 
    return(0); 
} 
+3

是的,你是对的。该semiolon不应该在那里。 –

+0

有些拼写错过了编辑的情况并不罕见。许多非小说类书籍(特别是与计算机相关的教科书)可能有* errata *列表问题。您还应该尝试找到可能修复此类错误的更新版本。 –

+0

已确认错误:http://www.oreilly.com/catalog/errata.csp?isbn=0636920015482 –

回答

1

是的,这是一个语法错误。 float add_with_tax(float f);是一个有效的原型,所以实际的错误是在下一行,因为在文件范围内你不能有一个单独的块{ ... }