displaying formatted data file

I had already marked my last post as solved, so I'm posting a new message although it's related to the last post. I now want to read my file and display to the screen. It currently prints out one word at a time. Whats the best way to format this so that it prints in the 5 original lines of my limerick as was entered by the user. Should this be done with the setw() manipulator?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

}#include<iostream>
#include <fstream>
#include<string>
using namespace std;
int main()
{
	ifstream infile;
	infile.open("C:\\Users\\Admin\\Limerick.dat");
	string limerickArray ;
	while (infile >> limerickArray)
	{
		cout << limerickArray << endl;
	}
	infile.close();
	system("pause");
	return 0;
}
Last edited on
closed account (SECMoG1T)
just use getline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include <fstream>
#include<string>

using namespace std;

int main()
{
	ifstream infile;
	infile.open("C:\\Users\\Admin\\Limerick.dat");
	string limerickArray{} ;

	while (std::getline(infile,limerickArray,'\n'))
	{
		cout << limerickArray << endl;
	}
	infile.close();
	system("pause");
	return 0;
}
Last edited on
Thanks again Yolanda! That did it.
Topic archived. No new replies allowed.