我需要一些认真的帮助才能运行它。我想我用 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";
}