Lab: Sequential (text) file input/output

Requirements

Modify the supplied program (seqFileIO.cpp) to make it work properly. You will also need the file pres.txt

Program to be modified

/*
    seqFileIO.cpp
    Name:
    CIS 150 Sequential file I/O lab
    Date:

    Lab to demonstrate sequential file reading and writing.
*/

// Finish the parts of the program marked ** TODO **
// A separate data file named pres.txt should also be downloaded.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

bool displayFile(string filename);

int main(int argc, char* argv[]) {
    string fname = "pres.txt";
    string pres44 = "Barack Obama";
    string pres45 = "Donald Trump";
    int year44 = 2009;
    int year45 = 2016;

    cout << "Before adding last two presidents:\n";
    bool ok = displayFile(fname);
    if (ok) {
        // ** TODO ** Part I
        // Open the file specified by fname for appending text output.
        // If file error, display error message and exit with EXIT_FAILURE status.
        // Output to file: year44, " ", pres44, "\n"
        // Output to file: year45, " ", pres45, "\n"
        // Close the file
        // ** END OF TODO **
        cout << "\nAfter adding last two presidents:\n";
        displayFile(fname);
    }

    cout << "Press enter to continue...";
    cin.get();
    return EXIT_SUCCESS;
}

bool displayFile(string filename) {
    int year;
    string name;
    int number;

    // ** TODO ** Part II
    // Open the file specified by filename for text input.
    // If file error, display error message and return false.
    // ** END OF TODO **

    number = 0;
    // ** TODO ** Part III
    // Loop while reading the year from the file does not cause an error.
    //     Increment number.
    //     Ignore any blanks in the input stream from the file.
    //     Read a line of text from the file into the variable name.
    //     Display number, ". ", name, " (", year, ")\n" on the screen
    // Close the file.
    // ** END OF TODO **

    return true;
}

Example run of program

Before adding last two presidents:
1. George Washington (1789)
2. John Adams (1797)
3. Thomas Jefferson (1801)
4. James Madison (1809)
5. James Monroe (1817)
6. John Quincy Adams (1825)
7. Andrew Jackson (1829)
8. Martin Van Buren (1837)
9. William Henry Harrison (1841)
10. John Tyler (1841)
11. James K. Polk (1845)
12. Zachary Taylor (1849)
13. Millard Fillmore (1850)
14. Franklin Pierce (1853)
15. James Buchanan (1857)
16. Abraham Lincoln (1861)
17. Andrew Johnson (1865)
18. Ulysses S. Grant (1869)
19. Rutherford B. Hayes (1877)
20. James A. Garfield (1881)
21. Chester Arthur (1881)
22. Grover Cleveland (1885)
23. Benjamin Harrison (1889)
24. Grover Cleveland (1893)
25. William McKinley (1897)
26. Theodore Roosevelt (1901)
27. William Howard Taft (1909)
28. Woodrow Wilson (1913)
29. Warren G. Harding (1921)
30. Calvin Coolidge (1923)
31. Herbert Hoover (1929)
32. Franklin D. Roosevelt (1933)
33. Harry S. Truman (1945)
34. Dwight D. Eisenhower (1953)
35. John F. Kennedy (1961)
36. Lyndon B. Johnson (1963)
37. Richard Nixon (1969)
38. Gerald Ford (1974)
39. Jimmy Carter (1977)
40. Ronald Reagan (1981)
41. George Bush (1989)
42. Bill Clinton (1993)
43. George W. Bush (2001)

After adding last two presidents:
1. George Washington (1789)
2. John Adams (1797)
3. Thomas Jefferson (1801)
4. James Madison (1809)
5. James Monroe (1817)
6. John Quincy Adams (1825)
7. Andrew Jackson (1829)
8. Martin Van Buren (1837)
9. William Henry Harrison (1841)
10. John Tyler (1841)
11. James K. Polk (1845)
12. Zachary Taylor (1849)
13. Millard Fillmore (1850)
14. Franklin Pierce (1853)
15. James Buchanan (1857)
16. Abraham Lincoln (1861)
17. Andrew Johnson (1865)
18. Ulysses S. Grant (1869)
19. Rutherford B. Hayes (1877)
20. James A. Garfield (1881)
21. Chester Arthur (1881)
22. Grover Cleveland (1885)
23. Benjamin Harrison (1889)
24. Grover Cleveland (1893)
25. William McKinley (1897)
26. Theodore Roosevelt (1901)
27. William Howard Taft (1909)
28. Woodrow Wilson (1913)
29. Warren G. Harding (1921)
30. Calvin Coolidge (1923)
31. Herbert Hoover (1929)
32. Franklin D. Roosevelt (1933)
33. Harry S. Truman (1945)
34. Dwight D. Eisenhower (1953)
35. John F. Kennedy (1961)
36. Lyndon B. Johnson (1963)
37. Richard Nixon (1969)
38. Gerald Ford (1974)
39. Jimmy Carter (1977)
40. Ronald Reagan (1981)
41. George Bush (1989)
42. Bill Clinton (1993)
43. George W. Bush (2001)
44. Barack Obama (2009)
45. Donald Trump (2016)

Points (10 points total)