Read/write data.file to structure

I'm have a very weak understanding of how to get data from a data file and store them in a string. In this project I have to prompt a list of 44 presidents using the presidents structure.name, then upon his selection i will display the presidents name and prompt him if he wants more info or go back to the list of name, the list of info (from the structure) are; city, yrs served in office, vice president/s, or fun fact from the data file.

this is the code I did(its rubbish and it doesnt work as I'm almost giving up on this one).
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Program 12-5 From Text
// File streams are passed as object

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

// Maximum amount to read from a line in the file
   const int SIZE = 81;  // Size of input array

   char input[SIZE];     // To hold file input

   struct President{
	string name;
	string city;
	string yrs;
	string vp;
	string funFact;
	};
// Function prototypes
bool openFileIn(fstream &, char *);
void readPresidentInfo(fstream &);


int main()
{


	fstream dataFile;

   if (!openFileIn(dataFile,"Presidents.dat"))
   {
      cout << "File open error!" << endl;
      return 0;   // Exit the program on error.
   }
   cout << "File opened successfully.\n";
   cout << "Now reading data from the file.\n\n";
   readPresidentInfo(dataFile);
   string data[5];


   system("pause");
   dataFile.close();
   cout << "\nDone.\n";
   return 0;
}

//***********************************************************
// Definition of function openFileIn. Accepts a reference   *
// to an fstream object as its argument. The file is opened *
// for input. The function returns true upon success, false *
// upon failure.                                            *
//***********************************************************

bool openFileIn(fstream &file, char *name)
{
   file.open(name, ios::in);
   if (file.fail())
      return false;
   else
      return true;
}

//***********************************************************
// Definition of function showContents. Accepts an fstream  *
// reference as its argument. Uses a loop to read each name *
// from the file and displays it on the screen.             *
//***********************************************************

void readPresidentInfo(fstream &file)
{	string data;
	int strpos=0;
	string line;
	file.getline(input, SIZE);  // Use \n as a delimiter.
   	 while (!file.eof())
   {   
	   cout<<input[SIZE]<<endl;
   string data[5];
stringstream ss(line);

while (getline(ss, data[strpos], ';')) 
{
      strpos++;
}
      file.getline(input, SIZE);  // Use \n as a delimiter.
   }
President president;
    president.name = data[0];
    president.city = data[1];
    president.yrs = data[2];
    president.vp = data[3];
    president.funFact = data[4];


}																																																																	
	


and this is the data file contents;
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
George Washington ; Virginia ; 1789-1797 ; John Adams ; Teeth were made from elephant and walrus tusks, not wood.
John Adams ; Mass. ; 1797-1801 ; Thomas Jefferson ; First to live in the White House
Thomas Jefferson ; Virginia ; 1801-1809 ; Aaron Burr,George Clinton ; Spoke 6 different languages
James Madison ; Virginia ; 1809-1817 ; George Clinton, Elbridge Gerry ; Smallest President at 5' 4", under 100 lbs.
James Monroe ; Virginia ; 1817-1825 ; Daniel D. Tompkins ; Died on the 4th of July, 1831, following both John Adams and Thomas Jefferson, who died on July 4, 1826
John Quincy Adams ; Mass. ; 1825-1829 ; John C. Calhoun ; Son of John Adams
Andrew Jackson ; South Carolina ; 1829-1837 ; John C. Calhoun, Martin Van Buren ; First President to ride in a train
Martin Van Buren ; New York ; 1837-1841 ; Richard M. Johnson ; First President born an American citizen - (all before him were born in the British colonies)
William Henry Harrison ; Virginia ; 1841 ; John Tyler ; President for only 31 days, died of pnueumonia
John Tyler ; Virginia ; 1841-1845 ; ----- ; Loved kids, had 15 children
James K. Polk ; North Carolina ; 1845-1849 ; George M. Dallas ; First President to serve a nation from coast to coast
Zachary Taylor ; Virginia ; 1849-1850 ; Millard Fillmore ; Never voted for a President
Millard Fillmore ; New York ; 1850-1853 ; ----- ; First President to have a stove and running water in the White House
Franklin Pierce ; New Hampshire ; 1853-1857 ; William R. King ; Memorized his entire inaugural speech - 3,319 words
James Buchanan ; Pennsylvania ; 1857-1861 ; John C. Breckinridge ; Never married, his niece was White House hostess
Abraham Lincoln ; Kentucky ; 1861-1865 ; Hannibal Hamlin, Andrew Johnson ; Tallest President, First President to be assassinated
Andrew Johnson ; North Carolina ; 1865-1869 ; ----- ; Was buried wrapped in an American flag with a copy of the Constitution
Ulysses S. Grant ; Ohio ; 1869-1877 ; Schuyler Colfax, Henry Wilson ; Was fined $20 for speeding with his horse and carriage
Rutherford B. Hayes ; Ohio ; 1877-1881 ; William A. Wheeler ; First President to use a phone - his phone number was 1
James A. Garfield ; Ohio ; 1881 ; Chester A. Arthur ; Could write with both hands at the same time - in different languages
Chester A. Arthur ; Vermont ; 1881-1885 ; Chester A. Arthur ; Changed his pants several times a day - he owned 80 pairs of pants
Grover Cleveland ; New Jersey ; 1885-1889 ; Thomas A. Hendricks ; First and only President married in the White House
Benjamin Harrison ; Ohio ; 1889-1893 ; Levi P. Morton ; First President to have electric lights and a Christmas tree in the White House, grandson of William H. Harrison
Grover Cleveland ; New Jersey ; 1893-1897 ; Adlai E. Stevenson ; First and only President to ever serve 2 non-consecutive terms
William McKinley ; Ohio ; 1897-1901 ; Garret A. Hobart, Theodore Roosevelt ; First President to use campaign buttons
Theodore Roosevelt ; New York ; 1901-1909 ; Charles W. Fairbanks ; Youngest person ever to be President
William H. Taft ; Ohio ; 1909-1913 ; James S. Sherman ; Heaviest President - 332 pounds
Woodrow Wilson ; Virginia ; 1913-1921 ; Thomas R. Marshall ; Dreamed of being a stage performer
Warren G. Harding ; Ohio ; 1921-1923 ; Calvin Coolidge ; Gambled away a set of White House china
Calvin Coolidge ; Vermont ; 1923-1929 ; Charles G. Dawes ; Only President born on the 4th of July
Herbert Hoover ; Iowa ; 1929-1933 ; Charles Curtis ; Spoke chinese to his wife to keep their stories private
Franklin D. Roosevelt ; New York ; 1933-1945 ; John N. Garner, Henry A. Wallace, Harry S. Truman ; Only President to serve more than 2 terms
Harry S. Truman ; Missouri ; 1945-1953 ; Alben W. Barkley ; Read every book in his hometown library
Dwight D. Eisenhower ; Texas ; 1953-1961 ; Richard M. Nixon ; Commander of Allied Forces during World War II
John F. Kennedy ; Mass. ; 1961-1963 ; Lyndon B. Johnson ; Youngest elected President, first Catholic President
Lyndon B. Johnson ; Texas ; 1963-1969 ; Hubert Humphrey ; Was an auto mechanic and teacher before being President
Richard M. Nixon ; California ; 1969-1974 ; Spiro Agnew, Gerald R. Ford ; Recommended a play to the Miami Dolphins in Super Bowl VI
Gerald R. Ford ; Nebraska ; 1974-1977 ; Nelson A. Rockerfeller ; Held his daughter's High School prom in the White House
Jimmy Carter ; Georgia ; 1977-1981 ; Walter F. Mondale ; First President born in a hospital
Ronald Reagan ; Illinois ; 1981-1989 ; George Bush ; Oldest President (69-77)
George Bush ; Mass. ; 1989-1993 ; J. Danforth Quayle ; Survived 4 planes crashes during World War II
William J. Clinton ; Arkansas ; 1993-2001 ; Albert Gore ; Played the saxophone on national TV
George W. Bush ; Connecticut ; 2001-2009 ; Richard Cheney ; Has a collection of over 250 signed baseballs
Barack Obama ; Hawaii ; 2009- ; Joseph Biden ; The first African-American to become President - Go USA


Any suggections or explainations on a proper structure will be appreciated. Thank you in advance fellas...
I didn't know there was a member function in fstream called fail(). Why not just say
1
2
3
if (file)
     return true;
return false;


Hey, why not just get rid of the function alltogether? It's really to little code to be called in a function, and it would be more efficient to get rid of this one.
Topic archived. No new replies allowed.