getline is adding an extra space.
Oct 24, 2016 at 9:51pm UTC
EDIT: I've figured out the problem. But thank you for checking in!
Hello everyone! I'm just about finished writing a small program for my Computer Science class. The only issue I'm having is an extra space is being added after only the first line of a loop which throws off the formatting. I was wondering what I would have to do to get rid of that space. Here is the code:
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
void getInfo (ifstream& inFile, ofstream& outFile)
{
string farmName, item;
int numberOfItem, totalItems = 0;
double itmAmnt, totalPrice = 0.00;
while (!(getline(inFile, farmName, ',' )).eof())
{
inFile >> numberOfItem;
inFile >> item;
inFile >> itmAmnt;
cout << std::setw(25) << std::setfill(' ' ) << left << (farmName + ':' ) << numberOfItem
<< " " << item << " @ " << itmAmnt << " totaling $" << fixed << setprecision(2)
<< static_cast <double > (numberOfItem) * itmAmnt << "\n---" ;
outFile << std::setw(25) << std::setfill(' ' ) << left << farmName << ":" << numberOfItem
<< " " << item << " @ " << itmAmnt << " totaling $" << fixed << setprecision(2)
<< static_cast <double > (numberOfItem) * itmAmnt << "\n---" ;
totalItems += numberOfItem;
totalPrice += static_cast <double > (numberOfItem) * itmAmnt;
}
cout << "\n\n" << totalItems << " items for a grand total of $" << totalPrice << endl;
outFile << "\n\n" << totalItems << " items for a grand total of $" << totalPrice << endl;
}
And the output is the following:
Collins Farm: 43900 tomatoes @ 0.67 totaling $29413.00
---
Bart Smith Farms: 34910 cassavas @ 0.99 totaling $34560.90
---
Allen Farms: 117 coconuts @ 0.54 totaling $63.18
---
.
.
.
Any help would be appreciated! Thank you!
Last edited on Oct 24, 2016 at 10:03pm UTC
Oct 24, 2016 at 10:06pm UTC
Are you sure the output is not more like?
Collins Farm: ...
---Bart Smith Farms: ...
Last edited on Oct 24, 2016 at 10:07pm UTC
Oct 24, 2016 at 10:11pm UTC
After I added inFile.ignore();
that's what it did but then I just added a \n to "\n---"
, making it "\n---\n"
and it worked.
Topic archived. No new replies allowed.