In C++, how can I use vectors to output the information from this .txt file to t
ID: 3674842 • Letter: I
Question
In C++, how can I use vectors to output the information from this .txt file to the terminal? I would have to output first name, last name, birthday month, birthday day, and birthday year.
presidents - Notepad File Edit Format View Help George Washington 1732 1735 1743 1751 10 30 13 16 1809 27 30 John Adams Thomas Jefferson James Madison Abraham Lincoln 2 Ulysses Grant Franklin Roosevelt Harry Truman Dwight Eisenhower John Kennedy Richard Nixon Gerald Ford 12 4 1822 1882 1884 1890 1917 1913 1913 1924 1911 1924 1946 1946 1961 14 29 10 14 Jimmy Carter 10 Ronald Reagan George Bush Bill Clinton George Bush Barack Obama 6 12 19Explanation / Answer
#include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
#include <string>
int main()
{
ifstream inputFile("file.txt");
vector<string> vec;
// file open
if (inputFile) {
string value;
// read the elements from file into a vector line by line
while ( std::getline(inputFile, value) ) {
vec.push_back(value);
}
// read from vector and display values on screen
for (int i=0; i<=((vec.size())-1); i++) {
cout << vec[i] << ' ';
}
}
}