What's wrong with my use of Stringstream here?

The program basically works as I want it to work - It grabs data from an input file, and then checks if the data is valid - The invalid data gets written to one text file, while the valid data then calculates a few things, and then writes it to another text file.

My problem is with the use of Stringstream - For my while loop, when I use something like, "while (!inFile.eof())", it processes the last line twice, so I thought of using stringstream, to process it line by line...I wrote it like so:

1
2
3
4
5
while (getline(inFile, line)){

istringstream ss(line);

while(ss >> numAdults >> numChildren >> mealType >> dayType >> depositAmount){


There is a combination of integers, chars and floats, so I thought that would be the correct way to utilize it.

The output in the billing_statement text file is this:

 Adults Children  Meal  Weekend  Deposit    Tax  Surcharge  Discount   Meal Cost
     27        3     D        Y    57.50  133.75     52.03     27.83      767.41
      4        0     S        N    25.00   15.66     52.03      1.31       85.87
    250       43     D        N   500.00 1280.82     52.03    249.05     6866.77
      5        0     D        Y   275.00   23.22      9.04      3.46      134.77

And the output in the error_file text file is this:


 Adults Children  Meal  Weekend  Deposit 
     10        0     R        Y       10
     14       -1     S        N       30
     20        3     D        Y      -10


Certain data is being combined, instead of being processed line by line. Would appreciate some insight.

Thanks!

here is my program INPUT:

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#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;
}
Last edited on
Topic archived. No new replies allowed.