Up for the challenge?

I'm supposed to fill a vector by reading a file with a list of President's birthdays. I am to use a class to do this. I gathered what I feel I would need and i'm stuck with getting the text info inside the vector, I think I have an idea on how to fill it with user info but not info from another file.

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
98
99
100
101
102
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

class Person
{
public:
    Person(); // set birth_day,  birth_month and birth_year to 1
    // constructor to values to the parameter’s value
    Person(string fn, string ln, int m, int d, int y);
    string get_name() const; // return the person’s first_name and last_name
    int get_birth_month() const; // return person’s birth_month
    int get_birth_day() const; // return person’s birth_day
    void set_name(string fn, string ln);
    void set_birthday_day(int m, int d, int y);
    void print() const; // print out all the person’s info
    
private:
    string first_name;
    string last_name;
    int birth_day;
    int birth_month;
    int birth_year;
};
 
int main( )
{

 //********** Opens president.txt file that contains list of presidents birthdays and closes the file afterward ***************
    
        string fname, lname;
        int month, day, year;
        string text;
    
    
        ifstream myfile ("presidents.txt");                              //Opens Presidents.txt
    
        if (myfile.is_open())
        {
            while ( getline (myfile, text) )
            {
                istringstream ss (text);
                vector<Person> friend;                                  //Vector "friend"
                //copy
                ss >> fname >> lname >> month >> day >> year;
                cout << fname << "\t" << lname<<"\t"<< month <<"\t"
                << day << "\t" << year << endl;
            }
            myfile.close();                                             
        }
        else cout << "Unable to open file";          
    
    
    //*********************************************  Vector which stores our list of presidents   *************************************
//Example from book
    vector<Person> friends;  //He wants me to name it friends                                       //Vector Begins
        cout << "Enter friends data:\n";
    
    int next;
    cin >> next;
    while (next > 0)
    {
        friends.push_back(next);
        cout << next << " added. ";
        cout << "friends.size( ) = " << friends.size( ) << endl;
        cin >> next;
    }
        cout << "You entered:\n";
        int i;
        for (unsigned int i=0; i < v.size(); i++)
            friends[i]=i;
        cout << friends[i] << " ";
        cout << endl;
    
    
    return 0;
}

Here's what the president text file looks like:

George Washington  	2	 22	 1732
John Adams 		10 	30	1735
Thomas Jefferson	4	13	1743
James Madison		3	16	1751
Abraham Lincoln	2	12	1809
Ulysses Grant		4	27	1822
Franklin  Roosevelt	1	30	1882
Harry Truman		5	8	1884
Dwight Eisenhower	10	14	1890
John Kennedy		5	29	1917
Richard Nixon		1	9	1913
Gerald Ford		7	14	1913
Jimmy Carter		10	1	1924
Ronald Reagan		2	6	1911
George Bush		6	12	1924
Bill Clinton		8	19	1946
George Bush		7	6	1946
Barack Obama		8	4	1961
 
Last edited on
I do not have compiler to check it right now but:
1- move line 46 to 37
2- after line 48 add:
friends.push_bach(Person(fname, lname, month,day,year));
3- after line 54 you can loop over friend and display the data
this is what I have:

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
:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;


//***********************************************************Person Class******************************************************
class Person
{
public:
    Person(); // set birth_day,  birth_month and birth_year to 1
    // constructor to values to the parameter’s value
    Person(string fn, string ln, int m, int d, int y);
    string get_name() const; // return the person’s first_name and last_name
    int get_birth_month() const; // return person’s birth_month
    int get_birth_day() const; // return person’s birth_day
    void set_name(string fn, string ln);
    void set_birthday_day(int m, int d, int y);
    void print() const; // print out all the person’s info
    
private:
    string first_name;
    string last_name;
    int birth_day;
    int birth_month;
    int birth_year;
};


int main( )
{
    vector<Person> friend;
    
        string fname, lname;
        int month, day, year;
        string text;
        
        ifstream myfile ("presidents.txt");
        if (myfile.is_open())
        {
            while ( getline (myfile, text) )
            {
                istringstream ss (text);
                ss >> fname >> lname >> month >> day >> year;
                
                Person myFriend = Person(fname, lname, month, day, year);
                friend.push_back(myFriend);
                
                cout << fname << "\t" << lname<<"\t"<< month <<"\t"
                << day << "\t" << year << endl;
            }
            myfile.close();
        }
        else cout << "Unable to open file";
    
    for (int i = 0; i < friend.size(); i++) {
        friends[i].print();
    

 return 0;
}
Last edited on
I'm getting an error on lines 34,49, 58 and 59
34:doesn't declare anything
49:friend outside of class, and cannot use dot operator
58:unexpected expression
59:undeclared
my updated code:

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

class Person
{
public:
    Person(); // set birth_day,  birth_month and birth_year to 1
    // constructor to values to the parameter’s value
    Person(string fn, string ln, int m, int d, int y);
    string get_name() const; // return the person’s first_name and last_name
    int get_birth_month() const; // return person’s birth_month
    int get_birth_day() const; // return person’s birth_day
    void set_name(string fn, string ln);
    void set_birthday_day(int m, int d, int y);
    void print() const; // print out all the person’s info
    
private:
    string first_name;
    string last_name;
    int birth_day;
    int birth_month;
    int birth_year;
};



int main( )
{
        string fname, lname;
        int month, day, year;
        string text;
        vector<Person> friends;
    
        ifstream myfile ("presidents.txt");
        if (myfile.is_open())
        {
            while ( getline (myfile, text) )
            {
                istringstream ss (text);
                ss >> fname >> lname >> month >> day >> year;
                
                Person myFriend = Person(fname, lname, month, day, year);
                friends.push_back(myFriend);

            }
            myfile.close();
        }
        else cout << "Unable to open file";
    
    for (int i = 0; i < friends.size(); i++) {
        friends[i].print();
    
    }
 return 0;
}

void set_name(fname, lname) {
    first_name = fname;
    last_name = lname;
};

void set_birthday_day(fmo, fday, fyear) {
    birth_month = fmo;
    birth_day = fday;
    birth_year = fyear;
};

string get_name() const {
    return (first_name + last_name);
}

int get_birth_month() const {
    return birth_month;
}

int get_birth_day() const {
    return birth_day;
}
    
void print() const {
    cout << fname << "\t" << lname<<"\t"<< month <<"\t"<< day << "\t" << year << endl;
}
Topic archived. No new replies allowed.