Read line by line txt file and display to console in columns

For some reason I can't figure out how to read my txt file line by line to display in three columns; title, artist, and cost...

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
  #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main() {

	string line;
	double price = 0;
	double total = 0;
	int i = 1, j = 0;
	

	cout << "Welcome to Steven Kidwell's Online Music Store.  You have" << endl;
	cout << "submitted the following order:" << endl;
	cout << "***************************************************************************" << endl;

	ifstream inputFile;
	inputFile.open("orders.txt");

	while (getline(inputFile, line)){
		cout << line << endl;
		
		if (i < 3) {
			i++;
		}
		else {
			i = 1;
			price = atof(line.c_str());
			total += price;
			j++;
		}
	}

	ofstream outputFile;
	outputFile.open("summary.txt");

		outputFile << "Downloads: " << j << endl;
		outputFile << "Total Due: " << total << endl;

	system("pause");
		return 0;
}


this is my txt file i read from

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
Last edited on
You are not really having trouble displaying them, since you haven't even read them in correctly yet. I am certain you are suppose to use an array here to read them in.

Your text file you are reading in from is as follows:
Record
Artists
Price


So you need three variables to store these information in, most likely in the form or arrays or vectors. Displaying them, in your case, is perhaps more easy than reading values in.

Last edited on
We haven't gotten to arrays yet in our book. So I'm not even sure how to do that.

And that's what I'm saying, I'm not sure how to read them in correctly...

Meaning when I run it and it reads the text file I had posted, I need the console to show

Title ...................... Artist ............................... Cost
song name 1.11
.
.
.
.

and I'm not sure how to make this happen while the loop is going.
Try this:
1
2
3
4
5
6
7
8
9
10
  string title, artist;
  while (getline(inputFile, title)) 
  {
    if(title.empty())
      continue;
    getline(inputFile, artist);
    inputFile >> price;
    cout << left << setw(40) << title << setw(30) << left << artist 
         << '\t' << price << '\n';
  }
Thomas1965, that definitely works and makes sense but I will still need my original while if and else statements because not only do i need the console to display my columns I need to have my output file still display downloads; and total due;....
Just figured it out! Thank you so much for the help! :)
Topic archived. No new replies allowed.