Help reading from .txt file

Hello :)

I am just learning about output and input from .txt files using #include<fstream>. I'm having quite a bit of trouble understanding how to get certain things in my .txt file to display to the console. I'm having to do it in a table format which I don't have trouble with but I can't even really get started because I'm having trouble understanding. I know how to read from a file and write to a file. But not how to read certain items in the .txt file to display them to console.

I'm reading from this input file order.txt.
My orders.txt file looks like this:

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

So it has title, artist, and price of download. I've been reading my book and my notes but I only know how to do inFile if it's the same kind of data like all numbers or all characters. Not when it's all mixed together like this.

I want to give you all an example of what I have done so far but I really don't have much because I can't figure out how to start this.

Do I need to be using while loops or for statements here?

Any help would be appreciated, thank you very much :D
Last edited on
Okay so this is what I have so far. It is a mess. Because I have no idea what I'm doing with the input. I need it to read the titles, the artists, and the cost. As you can see I am doing this so wrong. There's 15 lines in the .txt file.

I've read things and seen videos and looked at examples but none explain how to do this. We haven't even covered arrays yet so I don't know how to make it read the lines in my orders.txt file. I know how to make it read all the way to the end of the text but I don't think that's what I'm looking for here.

I'm sorry if this is a pain or stupid question, I just have no idea. :/

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

using namespace std;
int main()
{
	ifstream inFile;
	string line1, line2, line3, line4, line5, line6, line7, line8, line9, line10, line11, line12, line13, line14, line15;
	inFile.open("orders.txt");

	inFile >> line1;
	inFile >> line2;
	inFile >> line3;
	inFile >> line4;
	inFile >> line5;

	cout << "Welcome to My Online Music Store" << endl;
	cout << "You have submitted the following order: " << endl;
	cout << "********************************************************" << endl;
	cout << left << setw(20) << "Title" << right << "Artist" << right << setw(30) << "Cost" << endl;
	cout << line1 << "\n" << line2 << "\n" << line3 << "\n" << line4 << "\n" << line5 << endl;
	cout << "--------------------------------------------------------" << endl;
	cout << "Total Due: " << endl;
	cout << "========================================================" << endl;
	system("PAUSE");
	return 0;
Well now I've done a while loop but because of the way the .txt file is structured with having mostly long names with spaces it doesn't handle it well. I don't know what to do.

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

using namespace std;
int main()
{
	string artist;
	string title;
	double cost;

	ifstream inFile("orders.txt");

	while (inFile >> title >> artist >> cost) {
	}

	cout << "Welcome to My Online Music Store" << endl;
	cout << "You have submitted the following order: " << endl;
	cout << "********************************************************" << endl;
	cout << left << setw(20) << "Title" << right << "Artist" << right << setw(30) << "Cost" << endl;
	cout << title << " " <<  artist << " " << cost << " " << endl;
	cout << "--------------------------------------------------------" << endl;
	cout << "Total Due: " << endl;
	cout << "========================================================" << endl;
	system("PAUSE");
	return 0;
}
Do you realize that the extraction operator>> stops processing the string when it encounters a whitespace character? Since your strings have spaces perhaps you should consider getline()?

Topic archived. No new replies allowed.