Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:什么';我在这里使用Stringstream有什么问题吗?_C++_Fstream_Stringstream - Fatal编程技术网

C++ C++;:什么';我在这里使用Stringstream有什么问题吗?

C++ C++;:什么';我在这里使用Stringstream有什么问题吗?,c++,fstream,stringstream,C++,Fstream,Stringstream,我昨天发布了这个程序(如果你昨天没有看到,我在下面重新发布了一个基本摘要),你们向我展示了布尔逻辑的错误,所以一切都按照我的要求运行,但是如果我使用while(infle)或while(!infle.eof())等,我会处理最后一行两次。因此,我想使用stringstream,这样我可以逐行处理输入 由于输入数据不仅仅是一种数据类型(它是int、int、char、char、float),因此我以以下方式使用了stringstream(如下代码所示): 这些是我试图在输入文件中以正确顺序读取的变量

我昨天发布了这个程序(如果你昨天没有看到,我在下面重新发布了一个基本摘要),你们向我展示了布尔逻辑的错误,所以一切都按照我的要求运行,但是如果我使用while(infle)或while(!infle.eof())等,我会处理最后一行两次。因此,我想使用stringstream,这样我可以逐行处理输入

由于输入数据不仅仅是一种数据类型(它是int、int、char、char、float),因此我以以下方式使用了stringstream(如下代码所示):

这些是我试图在输入文件中以正确顺序读取的变量。发生的情况是,我的账单\报表文本文件中有4行输出,错误\报告文本文件中有3行输出(输入文本文件中总共有13行)。似乎有些线路可能会合并。感谢您的帮助

谢谢

输入文件:

10.0新加坡元100.00

27三维Y 57.50

125 17 D N 0.00

40新南威尔士州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

50DY275.00

-3 10日Y 20.00

14-1新南威尔士州30.00

20三维Y-10.00

这里有一个链接到我昨天的帖子,它解释了我的程序,虽然我的具体问题不需要它

#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void getData(int&,int&,char&,char&,float&);
无效检查有效(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&);
河流充填;
流出流文件(“Billing_Statement.txt”);
流错误报告(“error\u Report.txt”);
//将税率和周末附加费声明为常量。
常量浮动税率=0.18;
const float weekendsource=.07;
int main()
{
bool valid=true;
浮动工资;
浮动总税;
浮动总附加费;
浮置;
int numAdults;
国际儿童;
字符类型;
字符日型;
浮动存款额;
弦线;

问题是您的
getData()
函数正在从
infle
读取输入:一行由
std::getline()
读取,下一行在
getData()
中读取。看起来,
getData()
的目的是读取嵌套
中实际读取的数据,而
。现在,
getData()
实际上是有害的。我会将代码更改为

  • 不要使用嵌套的
    while
    -循环,而是使用
    if
    -语句
  • 如果无法读取该行,即在
    if
    else
    分支中打印该行
  • 删除
    getData()
    的使用和可能的实现
  • 缩进代码以反映其结构
  • 当然,我只是
    '\n'
  • 不需要
    close()
    文件,因为各个流的析构函数无论如何都会这样做(只需要在处理错误时使用
    close()

  • 谢谢-我不敢相信我忘记了getData函数。删除它本身就解决了我的问题。我还担心我过度使用引用-如果是,请有人指出我何时/如果这样做?
    while (getline(inFile, line)){
    
    istringstream ss(line);
    
    while(ss >> numAdults >> numChildren >> mealType >> dayType >> depositAmount){
    
    #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;
    }