1

I have been given and assignment to read a file of data listed below:

Turn on the Bright Lights
Interpol
9.49
House of Jealous Lovers
The Rapture
1.29
Fever to Tell
Yeah Yeah Yeahs
6.99
Desperate Youth, Blood Thirsty Babes
TV on the Radio
8.91
The Fragile
Nine Inch Nails
12.49

Input this data into C++ and then display in a matrix and output total price of the cd's and how many cd's were sold. So far I can only get the first line to display properly and cannot figure out what I need to change to get the rest to display. Here is what I have written thus far. I have not started on the output code and feel as though I will not have an issue with that.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;

int main()
{
ifstream inputFile, processLine;
ofstream outputFile;
string title, band;
double price, total = 0;

inputFile.open("orders.txt");
while (!inputFile.eof())
{
    getline(inputFile, title);
    getline(inputFile, band);
    inputFile >> price;
    total += price;

    cout << "Welcome to Megan McCracken's Online Music Store" << endl;
    cout << "You have submitted the following order:" << endl;
    cout << "***************************************************************************" << endl;
    cout << "Title" << setw(46) << "Artist" << setw(24) << "Cost" << endl;

    cout << title << setw(28) << band << setw(22) << fixed << setprecision(2) << price << endl;
    cout << title << setw(30) << band<< setw(19) << fixed << setprecision(2) << price << endl;
    cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;
    cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;
    cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;

    cout << "--------------------------------------------------------------------------" << endl;
    cout << "Total Due:" << setw(75) << fixed << setprecision(2) << total << endl;
    cout << "==========================================================================" << endl;

}


    /* getline(inputFile, title);
    cout << left << title;
    getline(inputFile, band);
    cout << setw(23) << band;
    inputFile >> price;
    cout << setw(10) << fixed << setprecision(2) << price << endl; */






system("pause");
return 0;

}
Megan McCracken
  • 43
  • 1
  • 1
  • 6
  • [why eof in a loop condition is always wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – kfsone Jul 03 '16 at 00:12

1 Answers1

1

There are multiple problems with this code.

while (!inputFile.eof())

This is always a bug. See the linked article for more information.

But this is not the only problem.

getline(inputFile, band);
inputFile >> price;

Do not use both std::getline() and operator>> on the same input stream. operator>> has certain, narrowly-constrained, semantics when it comes to handling whitespace. Here, operator>> is not going to consume the trailing newline, so on the next loop iteration the first getline() is going to go off the rails.

Although there are some band-aid fixes that are often suggested, in this case, the simplest solution is to simply not use operator>>.

Use getline() a third time, to read the third line into a std::string. Then using it to construct an independent std::istringstream, and use operator>> with that. Problem solved.

After fixing that, there are some logical errors that need to be fixed. The header of the report should likely be displayed before the loop, and inside the loop the only thing that should be done is displaying a single line of the receipt, and adding up the totals, then displaying the total after the end of file was received.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • So you fix works wonderfully, however what is an option assuming we cannot use the std::istringstream. All we are allowed to use are the getline and inputFile >> functions. Is there a possible way to do that? – Megan McCracken Jul 03 '16 at 22:35
  • This would not be a reasonable assumption. `std::istringstream` is a part of the C++ library. It will exist, and work just fine, anywhere you will find `std::getline` and `operator>>`. – Sam Varshavchik Jul 03 '16 at 22:38