Reading File And Displaying

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.
So what are you having trouble with ?
#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
To put the output on separate lines, you need to explicitly write endlines to cout:

std::cout << text << std::endl;
Last edited on
thanks guys my code now works the way it should.
Topic archived. No new replies allowed.