2011-03-23 116 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