C计算每平方英尺成本计划

时间:2013-11-21 05:05:39

标签: c++ function compiler-construction

目前我在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;
}

3 个答案:

答案 0 :(得分: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);

所以你正在学习函数,你应该再看看如何使函数返回值。这是基础,但在上面的代码中遗漏了。

答案 1 :(得分:0)

许多错误:

1。     cin&gt;&gt;长度,宽度,costpersqft;

AFAIK不允许这样做。

应该是

cin >> length >> width >> costpersqft;

您的installedPrice和totalPrice函数也应该根据原型返回一些浮点值。

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);
}

什么是安装?它对此功能不可见。

答案 2 :(得分:0)

我看到一些编译错误。

get data函数将3个int引用作为参数,但是你发送的是2个int和一个float。

在InstalledPrice函数内部,永远不会声明安装变量。