Reading File And Displaying

Feb 24, 2014 at 1:51pm
Write your question here.
Given a file le name I'm want to know how to write a program that reads the file and displays the
demographic data. Peter.txt gives an example fi le structure.
Example output is given below:
Enter fi le name: Peter.txt
Name: Peter
Age: 10
Gender: Male
Nationality: UK
Missing fi les and unrealistic ages should result in descriptive error messages.
Feb 24, 2014 at 2:16pm
So what are you having trouble with ?
Feb 24, 2014 at 2:25pm
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string text;
//cout << "Enter file name to be printed out: ";
fstream textfile;
textfile.open("Peter.txt");
textfile >> text;
cout << text;
textfile >> text;
cout << text;
textfile >> text;
cout << text;
textfile >> text;
cout << text;
textfile >> text;
cout << text;
cin.get();
textfile.close();
return 0;
}


thats my code so far, It only shows Peter10MaleUK in a single line. This is what i want my promm to do:

Enter fi le name: Peter.txt
Name: Peter
Age: 10
Gender: Male
Nationality: UK
Feb 24, 2014 at 2:31pm
To put the output on separate lines, you need to explicitly write endlines to cout:

std::cout << text << std::endl;
Last edited on Feb 24, 2014 at 2:32pm
Feb 25, 2014 at 9:04am
thanks guys my code now works the way it should.
Topic archived. No new replies allowed.