0

I creating a program that can output the title, artist, and price of songs inside a orders.txt file.

I'm having a hard time assigning variables to the contents and outputting them to the console successfully.

An example of the order list is:

Undead
Hollywood Undead
4.50
Paradise Lost
Hollywood Undead
3.00
Hello
Adele
5.00
Out Of Control
Hoobastank
6.00

I'm currently using a while loop to output all the contents. I know I need to use getline for separate variables for title and artist. I also know need to make the price int.

Unfortunately, when I debug the code, the loop doesn't end or the formatting is off. I'm using iomanip with setw().

PLEASE HELP! I've tried making price a string and then converting it into an int.

Here is my source code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{

ifstream inputFile;
inputFile.open("Orders.txt");
int number, price, totalNum = 0;
double total;
string title, artist;

cout << left << setw(36) << "Title";
cout << setw(22) << "Artist" << left << "Price" << endl;
while (!inputFile.eof())
{
    getline(inputFile, title);
    getline(inputFile, artist);
    inputFile >> price;
    cout << left << setw(36) << title;
    cout << setw(22) << artist << left << "$" << price << endl;

}
inputFile.close();

system("pause");
return 0;
}
MikeCAT
  • 61,086
  • 10
  • 41
  • 58
Chriskt
  • 65
  • 1
  • 1
  • 8
  • Possible duplicate of [Why does std::getline() skip input after a formatted extraction?](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – MikeCAT Mar 20 '16 at 03:01

2 Answers2

0

Have you tried this:

while (inputfile >> title)
{
    getline(inputFile, artist);
    inputFile >> price;
    cout << left << setw(36) << title;
    cout << setw(22) << artist << left << "$" << price << endl;
}

Maybe the infinite loob is beeing caused by some sort bad enconding of the text file

Lucas Henrique
  • 137
  • 6
  • 13
  • Unfortunately : while (inputFile >> title) will not work because title is a string. If you were going to do that, I think you'd put: while(getline(inputFile >> title)) Also It has stopped looping. Now I just need how to successfully output it to the console in columns and rows. – Chriskt Mar 20 '16 at 17:26
0

Your loop condition is likely what's causing your error. Here's your code with some tweaks that make it work. I put comments by my changes to explain them.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{

    ifstream inputFile;
    inputFile.open("Orders.txt");
    int number, price, totalNum = 0;
    double total;
    string title, artist;

    if (inputFile.is_open())
    {
        cout << left << setw(36) << "Title";
        cout << setw(22) << "Artist" << "Price" << endl; // No need to declare 'left' again
        while (getline(inputFile, title))  // Change to 'getline(inputFile, title)'
        {
            getline(inputFile, artist);
            inputFile >> price;
            inputFile.ignore(99999, '\n');  // Discard newline char for next getline
            cout << left << setw(36) << title;
            cout << setw(22) << artist << left << "$" << price << endl;
        }
        inputFile.close();
    }

    system("pause");
    return 0;
}

Edit

The big reason your code wasn't working is because of how getline works vs how the >> operator works. When you call getline, it discards the newline character. However, the >> operator does not do this. So here's what was happening...

[Example file below]

v [Starts reading from here]
Line1\n <-- [getline() gets rid of '\n']
Line2\n <-- [getline() gets rid of '\n']
10      <-- ['>>' does NOT get rid of '\n']

v [Starts second loop from here]
\n      <-- [This was leftover from '>>' and getline gets it]
Line3   <-- [Second getline() is processing the first line here]
Line4   <-- [Now '>>' tries to put a string in a double (ERROR)]
[Now you get an infinite loop...]
Community
  • 1
  • 1
Drake Johnson
  • 544
  • 2
  • 15