0

我需要一些认真的帮助才能运行它。我想我用 inputFile 搞乱了循环。程序编译并且 .txt 文件位于正确的目录中。txt 是一个替换用户输入的测试文件。该文件在三个单独的行中包含三个数字:1、115 和 10。这是我的代码:

//Headers
//Headers
#include<iostream>
#include<fstream>
#include<cmath>
#include<cstdlib>
#include<iomanip>
using
namespace std;
void PaintJobEstimator(double gallonprice, double wallspace)
{
    double numBucket;
    double hours;
    double bucketCost;
    double laborCharges;
    double totalCost;
    {
    //calculates number of buckets of paint (gallons) needed
    numBucket=1/115*wallspace;
    //calculates paint cost
    bucketCost=gallonprice*numBucket;
    //calculates labor hour
    hours=8/115*wallspace;
    //calculates labor charges
    laborCharges=hours*18;
    //calculates total cost
    totalCost=bucketCost+laborCharges;
    //Console output
    {
        cout << "The number of Gallons of paint required:\t" << numBucket << endl;
        cout << "The hours of labor required:\t" << hours << " hrs" << endl;
        cout << "The labor charges:\t$" << laborCharges << endl;
        cout << "The cost of the paint:\t$" << bucketCost << endl;
        cout << "The total cost of the paint job:\t$" << totalCost << endl;
    }

    }

}

int main ()
{
    int rooms=0; //number of rooms
    double wallspace=0; //wall space measured in square meters
    double gallonprice=0; //Price per gallon
    cout << "=========================================================\n";
    cout << "___________________Paint Job Estimator___________________\n";
    cout << "_________________________________________________________\n";
    //by Jeff Youngblood
    cout << endl;
    ifstream inputFile;
    //open the file
    inputFile.open("17.txt");
    if (inputFile.is_open())
    {
        if (rooms>=1) //validates rooms
        {
            inputFile >> rooms;
        }
        for (int roomNum=1;roomNum<=rooms;roomNum++)
        {
            if (wallspace>1)//validates and inputs wallspace
            {
                inputFile >> wallspace;
            }
        }
        //end loop
        while (gallonprice>10) //validates price per gallon
        {
            inputFile >> gallonprice;
        }
        PaintJobEstimator(gallonprice,wallspace);
        system ("pause");
    }
    else
        cout <<"Error reading file '17.txt', please check your directory.\n";
}
4

2 回答 2

2

这个序列

    double wallspace = 0;
    //...
    if (wallspace>1) //validates and inputs wallspace
    {
        inputFile >> wallspace;
    }

不起作用,因为 if 语句中的条件始终为假。当您到达那里时,wallspace的值为零,因此您永远不会尝试输入新值。

与 for 类似gallonprice,您永远不会输入值,因为初始条件为 false,因此永远不会进入循环。

于 2012-11-15T10:14:02.687 回答
1

您读取的所有文件都是有条件的,期望值大于零,但是由于您init的房间、墙壁空间和加仑价格为零,因此永远不会读取文件。PaintJobEstimator因此用 0,0 调用。尝试删除条件并从那里开始工作。

于 2012-11-15T10:25:21.647 回答