2013-11-21 24 views
-2

目前我在C++類中,並且正在學習函數。我們的任務是創建一個程序來計算總成本,包括每個房間的價格(每平方英尺的價格),勞動力和用戶輸入數量給出的每個房間的稅額。這是我的程序,我無法編譯,我不知道爲什麼。感謝任何幫助我的人,我非常感謝。C計算每平方英尺程序的成本

#include <iostream> 
#include <iomanip> 
using namespace std; 

void getdata (int&, int&, int&); 
float InstalledPrice (int , int, float); 
float totalprice (float); 
void printdata (int , int, float); 



int main() 
{ 

    int length, width, count, x; 
    float installation, costpersqfoot, price; 

    cout << "Enter the amount of rooms.\n"; 
    cin >> x; 

    for(count = x; count != 0; count --) 
    { 
     getdata (length, width, costpersqfoot); 
     InstalledPrice (length, width, costpersqfoot); 
     totalprice (installation); 
     printdata (length, width, price); 

    } 

} 

void getdata(int & length, int & width, float & costpersqft) 
{ 
    cin >> length, width, costpersqft; 
} 

float InstalledPrice (int length, int width, float costpersqfoot) 
{ 
    const float LABOR_COST = 0.35; 
    float sqfoot; 
    sqfoot = length * width; 
    installation = (costpersqfoot * sqfoot) + (LABOR_COST * sqfoot); 
} 

float totalprice(float installation) 
{ 
    const float TAX_RATE = 0.05; 
    float price; 
    price = (installation * TAX_RATE) + installation; 
} 

void printdata(int length, int width, float price) 
{ 
    cout << length << " " << width << " " << price << endl; 
} 
+2

編譯器告訴你什麼是錯誤的?那將是一個開始猜測問題出在哪裏的好地方......雖然在那個時候,你可能不必猜測...... :) – cHao

回答

0

許多錯誤:

1. CIN >>長度,寬度,costpersqft;

AFAIK這是不允許的。

應該是

cin >> length >> width >> costpersqft; 

而且你installedPrice和totalPrice函數應返回一些浮點值按thier原型。

float InstalledPrice (int length, int width, float costpersqfoot) 
{ 
    const float LABOR_COST = 0.35; 
    float sqfoot; 
    sqfoot = length * width; 
    installation = (costpersqfoot * sqfoot) + (LABOR_COST * sqfoot); 
} 

什麼是安裝?這個功能不可見。

0

我看到一對夫婦編譯錯誤。

獲取數據函數將3個int引用作爲參數,但是您發送的是2個整數和一個浮點數。

Inside InstalledPrice函數中,從不聲明安裝變量。

1

一些更多的錯誤

float totalprice(float installation) 
{ 
    const float TAX_RATE = 0.05; 
    float price; 
    price = (installation * TAX_RATE) + installation; 
} 

出於某種原因,初學者學習功能時,往往顯得錯過return概念。上述應

float totalprice(float installation) 
{ 
    const float TAX_RATE = 0.05; 
    return (installation * TAX_RATE) + installation; 
} 

如果你的函數返回一個值(即,如果它不是一個void功能),那麼你必須使用return做到這一點。 InstalledPrice同樣的問題。

現在當你使用這些函數時,你必須捕獲返回的值。所以main應該是這樣的

getdata (length, width, costpersqfoot); 
    installation = InstalledPrice (length, width, costpersqfoot); 
    price = totalprice (installation); 
    printdata (length, width, price); 

所以,你正在學習的功能,你一定要再來看看如何使功能返回值。這是基本的,但是從上面的代碼中遺漏了。