2011-03-23 199 views
0

我无法编译,这个程序给我错误。我已经花了3天的时间。我不明白这些编译错误

C:\南澳\ COS1511 \ TEST.CPP
[警告在函数'漂浮calcAllowedPerChild(浮点)':

错误C:\南澳\ COS1511 \ TEST.CPP:26个
无效操作数类型float()(float)const float为二进制operator<

#include <iostream> 
using namespace std; 

const float maxPerUnit = 20000.00; 
//minPerChild include a standard gift consisting of a bath towel and facecloft 
const float minPerChild = 100.00; 
const float maxPerChild = 180.00; 

//Depending on the amount the child may also get one or more of the fallowing: 
const float TOOTHBRUSH = 29.95; 
const float HIGHLIGHTERS = 25.99; 
const float CRAYONS = 17.95; 
const float NOTEBOOK = 12.95; 
const float PEN = 9.99; 

//You must add the function calcAllowebPerchild() here 

float calcAllowedPerChild (float nrChildren) 

{ 

float allowedPerChild = maxPerUnit/nrChildren; 

if (allowedPerChild > maxPerChild || calcAllowedPerChild < minPerChild) 

    return maxPerChild; 
    else 
    return minPerChild; 

} 

int main() 
{ 

    float amtGift; //Allowed amount per child 
    float amtSpendPerChild = 0.00; //actual amount spend per child 
    float totalSpend = 0.00; //total spend for one orphanage 
    float totalAll = 0.00; //total spend for all 4 orphanages 
    int nrChildren;  //number of chldren per orphanage 

    cout << "Enter the number of children: " << endl; 
    cin >> nrChildren; 
    amtGift = calcAllowedPerChild(nrChildren); 
    cout.setf(ios::fixed); 
    cout.precision(2); 
    cout << endl << " Allowable amount per child :R" << amtGift; 
    cout << endl << endl; 

    return 0; 
} 
+3

需要'家庭作业'标签? – 2011-03-23 22:23:49

+1

这条线上有一个分号'float calcAllowedPerChild(float nrChildren);'你可能不需要。 – ChrisF 2011-03-23 22:25:27

回答

1
float calcAllowedPerChild (float nrChildren); 
              //^: Error - reomove it. 

函数原型以;而不是函数定义结尾。在if语句条件也看一看 -

if (calcAllowedPerChild > maxPerChild || calcAllowedPerChild < minPerChild) 

既然是一门功课,我会给你在接下来的错误提示。

提示:每个陈述应该以;结尾。现在检查你的calcAllowerPerChild函数。

if (allowedPerChild > maxPerChild || calcAllowedPerChild < minPerChild) 
            //^^^^^^^^^^^^^^^^^ Error: Probably you 
            // meant allowedPerChild. Change it. 
+0

请帮忙,现在是给三个错误: 错误C:\ COS1511 \ test.cpp:26 预计','或';'之前的 “if” 错误C:\ COS1511 \ TEST.CPP:29 预期之前的 “else” 错误C基本表达式:\南澳\ COS1511 \ TEST.CPP:29 预期';”在“其他”之前 – queos 2011-03-23 23:02:30

+0

@queos - 检查给出的提示。 – Mahesh 2011-03-23 23:10:41

+0

thaks我认为我现在正在创建它是一个错误剩余:错误C:\ unisa \ COS1511 \ test.cpp:26 类型float()(float)'和'const float'的无效操作数到二进制'operator < ' – queos 2011-03-23 23:17:03

5

你有一个;在您的calcAllowedPerChild函数结束时。

void foo() { 
    ... code ... 
} 

或:

void foo() 
{ 
    ... code ... 
} 

取决于你的编码风格像

功能应该看看。

编辑 - 您还需要更正拼写(maxPerChald),并宣布calcAllowedPerChild

float calcAllowedPerChild = maxPerUnit/nrChildren; 

以及改变或者您的calcAllowedPerChild功能或您calcAllowedPerChild变量的名称。你不能让他们都有相同的名字。

+0

我已删除;在calcAllowedPerChild函数的结尾,但仍然给出两个错误 – queos 2011-03-23 22:28:12

+0

发布您的新错误 – 2011-03-23 22:30:27

+0

我添加到我的答案为您的其他错误。 – 2011-03-23 22:51:00

1

不应该有这个函数的结尾;

float calcAllowedPerChild (float nrChildren); 

修复了,看看会发生什么。

1

我建议检查你的分号 - 确保函数声明看起来不像原型。

+0

感谢工作 – queos 2011-03-23 23:24:50

0

这条线:

float calcAllowedPerChild (float nrChildren); 

不应该在最后的分号。它应该是:

float calcAllowedPerChild (float nrChildren) 
0

恩,是的,你有一个额外的';'在你的函数定义中:

float calcAllowedPerChild (float nrChildren); 

只要在行尾有分号就可以了。这就是编译器试图告诉你什么时候说你在该行上有一个额外的分号。

0

删除float calcAllowedPerChild (float nrChildren);末尾的分号; 函数定义不以分号结尾。

0

这是你的代码中的错字吗?

const float maxPerChald = 180.00; 

此行之后删除分号:

float calcAllowedPerChild (float nrChildren); 
+0

谢谢我认为我是geting它,但现在给出了三个错误 – queos 2011-03-23 23:02:13

+0

谢谢,现在正在工作 – queos 2011-03-23 23:27:14

1

有许多与calcAllowedPerChild问题 - 它应该是:

float calcAllowedPerChild (float nrChildren) 
{  
    float allowedPerChild = maxPerUnit/nrChildren; 

    if (allowedPerChild > maxPerChild || allowedPerChild < minPerChild) 
     return maxPerChild; 
    else 
     return minPerChild; 
} 

你也有一个错字这里:

const float maxPerChald = 180.00; 

它应该是:

const float maxPerChild = 180.00; 
+0

谢谢,现在我已经改变它,因为你说但仍然给三个错误是: – queos 2011-03-23 23:06:12

+0

请帮助,现在给出三个错误: 错误C:\ COS1511 \ test.cpp:26 预计','或';'在“if”之前 错误C:\ COS1511 \ test.cpp:29 预期的“else”之前的主表达式 错误C:\ unisa \ COS1511 \ test。cpp:29 预计';'在“else”之前 – queos 2011-03-23 23:10:29

+0

@queos:你应该复制并粘贴上面的代码 - 你的代码中仍然有一个错误 - 将if(allowedPerChild> maxPerChild || calcAllowedPerChild maxPerChild || allowedPerChild 2011-03-23 23:22:05