我昨天发布了这个程序(如果您昨天没有看到它,我在下面重新发布了一个基本摘要),你们告诉我我的布尔逻辑有什么问题,所以一切都按我的意愿运行to,但如果我使用while(inFile)或while(!inFile.eof())等等,我正在处理最后一行两次。因此,我想使用stringstream,以便我可以处理输入逐行。
由于输入数据不仅仅是一种数据类型(它是int,int,char,char,float),我还是按照以下方式使用stringstream(如下面的代码所示):
while (getline(inFile, line)){
istringstream ss(line);
while(ss >> numAdults >> numChildren >> mealType >> dayType >> depositAmount){
这些是我正在尝试在输入文件中以正确顺序读取的变量。会发生的是,我的billing_statement文本文件中有4行输出,error_report文本文件中有3行输出(输入文本文件中总共有13行)。似乎有些线路可能会合并在一起。感谢这里的帮助。
感谢。
输入文件:
10 0 S Y 100.00
27 3 D Y 57.50
125 17 D N 0.00
4 0 S N 25.00
0 25 S Y 23.75
250 43 D N 500.00
0 0 D N 0.0
10 0 R Y 10.00
17 3 D R 15.00
5 0 D Y 275.00
-3 10 D Y 20.00
14 -1 S N 30.00
20 3 D Y -10.00
这是昨天我的帖子的链接,它解释了我的程序,虽然我的具体问题不应该需要...
C++: Mock Catering Company Billing Program - Not able to ouput bad data to error file
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
void getData(int &, int &, char &, char &, float &);
void checkValid(int &, int &, char &, char &, float &, bool &);
void calcData(int, int, char, char, float, float &, float &, float &, float &);
void sendData(int, int, char, char, float, float &, float &, float &, float &);
ifstream inFile;
ofstream outFile("Billing_Statement.txt");
ofstream error_Report("Error_Report.txt");
//Declare the tax rate and weekend surcharge as constants.
const float taxRate = 0.18;
const float weekendSurcharge = .07;
int main()
{
bool valid = true;
float mealCost;
float totalTax;
float totalSurcharge;
float discountAmount;
int numAdults;
int numChildren;
char mealType;
char dayType;
float depositAmount;
string line;
cout << "\nThis program will calculate data for a catering company " << endl;
outFile << " Adults " << "Children " << "Meal " << " Weekend " << setw(9) << "Deposit "
<< setw(6) << "Tax" << setw(11) << "Surcharge" << setw(10) << "Discount" << setw(12) <<
"Meal Cost" << endl;
error_Report << " Adults " << "Children " << "Meal " << " Weekend " << setw(9) <<
"Deposit " << endl;
inFile.open("file.txt");
if (!inFile) {
cout << "\nError: File could not be opened. ";
//exit(1);
}
while (getline(inFile, line)){
istringstream ss(line);
while(ss >> numAdults >> numChildren >> mealType >> dayType >> depositAmount){
getData(numAdults, numChildren, mealType, dayType, depositAmount);
checkValid(numAdults, numChildren, mealType, dayType, depositAmount, valid);
if (valid == true)
{
calcData(numAdults, numChildren, mealType, dayType, depositAmount, totalTax,
totalSurcharge, discountAmount, mealCost);
sendData(numAdults, numChildren, mealType, dayType, depositAmount, mealCost,
totalTax, totalSurcharge, discountAmount);
}}
}
cout << "\nA copy of this has created for your convenience in the file,
Billing_Statement.txt" << endl;
inFile.close();
outFile.close();
error_Report.close();
return 0;
}
void getData(int & numAdults, int & numChildren, char & mealType, char & dayType, float
& depositAmount)
{
inFile >> numAdults >> numChildren >> mealType >> dayType >> depositAmount;
}
void checkValid(int & numAdults, int & numChildren, char & mealType, char & dayType,
float & depositAmount, bool & valid)
{
if (numAdults < 0 || numChildren < 0)
valid = false;
else if (!(mealType == 'D' || mealType == 'S'))
valid = false;
else if (!(dayType == 'Y' || dayType == 'N'))
valid = false;
else if (depositAmount < 0)
valid = false;
else
valid = true;
if (valid == false) {
error_Report << setw(7) << numAdults << setw(9) << numChildren << setw(6) <<
mealType << setw(9) << dayType << setw(9) << right << depositAmount << setw(8) << endl;
}
}
void calcData(int numAdults, int numChildren, char mealType, char dayType, float
depositAmount, float & totalTax, float & totalSurcharge, float & discountAmount, float &
mealCost)
{
if (mealType == 'S') {
mealCost = ((numAdults * 21.75) + (numChildren * (21.75 * .60)));
totalTax = mealCost * taxRate;
mealCost += taxRate;
if (dayType == 'Y') {
totalSurcharge = mealCost * weekendSurcharge;
mealCost += totalSurcharge;
}}
else {
mealCost = ((numAdults * 25.80) + (numChildren * (25.80 * .60)));
totalTax = mealCost * taxRate;
mealCost += taxRate;
if (dayType == 'Y') {
totalSurcharge = mealCost * weekendSurcharge;
mealCost += totalSurcharge;
}
}
if (mealCost < 100) {
discountAmount = .015 * mealCost;
mealCost -= discountAmount;
}
else if (mealCost >= 100 && mealCost < 400) {
discountAmount = .025 * mealCost;
mealCost -= discountAmount;
}
else if (mealCost >= 400) {
discountAmount = .035 * mealCost;
mealCost -= discountAmount;
}
}
void sendData(int numAdults, int numChildren, char mealType, char dayType, float
depositAmount, float &mealCost, float &totalTax, float &totalSurcharge, float
&discountAmount)
{
outFile << fixed << showpoint << setprecision(2);
outFile << setw(7) << numAdults << setw(9) << numChildren << setw(6) << mealType <<
setw(9) << dayType << setw(9) << right << depositAmount << setw(8) << totalTax <<
setw(10) << totalSurcharge << setw(10) << right << discountAmount << setw(12) << right
<< mealCost << endl;
}
答案 0 :(得分:1)
问题是,您的getData()
函数正在从inFile
读取输入:std::getline()
读取一行,getData()
读取下一行。似乎getData()
旨在读取嵌套while
中实际读取的数据。就像现在一样,getData()
实际上是有害的。我会将代码更改为
while
- 循环,而是使用if
语句。if
的{{1}} - 分支中打印。else
。std::endl
但getData()
。'\n'
个文件,因为无论如何,各个流的析构函数都会这样做(只需要使用close()
就可以处理错误了。)