Vectors to print a persons information

Hi, I have written part of this code but I am having trouble with some things that I dont know are missing. The requirement of the code is below:

Your program must include the following:
Implement all the class methods defined above
Must initialize the class object variable with initial value in the default constructor
Your program will read a file contains person’s info. The file has the following format:


FirstName LastName Sex BirthYear BirthMonth BirthDay




The file name must (sample file: personsInfo.txt personsInfo.txt) be passed in when you start the program using argc, argv: main (int argc, char* argv[])
After reading the file, your program will have an user’s menu similar to the one below:
Enter person’s information:
Print out person’s information
Exit

If user selects “1”, the program will search person’s last name, and if found, will print out person’s information. (Search for Clinton, Trump, Obama). If not found, output a message indicate the person’s information is not found. (search Bush)


If user selects “2”, the program will print out all the person’s information have entered so far (which is stored in a vector). After prints out all the person’s information, the program will print out the menu again and waiting for user’s input.
If user selects “3”, the program will exit. Class: Class section name (CS-106-02 or CS-106-03) Your program submission:
Pdf file contains all of the following:

What I have so far is below. I would appreciate it if you guys could help me understand and finish the code.

PersonInfo.txt
1
2
3
4
5
6
Hillary Clinton F 1947 10 26
Donald Trump M 1946 06 14
Bernie Sanders M 1941 09 08
Barack Obama M 1961 08 04
Melania Trump F 1970 04 26
Michelle Obama F 1964 01 17


Person.h
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
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <string>

//The code below was provided during the lab.

class Person
{
	public:
		Person();
		Person( std::string fname, std::string lname , string s, int byear, int bmth, int bday );
		std::string findName (std::string lname) const;
		void setName (std::string fname, std::String lname);
		void read (string lname);
		void printPerson () const;
		bool find (std::string a) const;
		//void setAge( int age );
		//int getAge() const;
		//void print() const;

	private:
		std::string firstName;
		std::string lastName;
		std::string sex;
		int birthYear;
		int birthMonth;
		int birthDay;
		
};

#endif // PERSON_H_INCLUDED  


Person.cpp

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
#include "Person.h"
#include <string>
#include <iostream>
using namespace std;

Person::Person()
{
	
}

Person::Person(std::string fname, std::string lname , string s, int byear, int bmth, int bday)
{
	firstName = fname;
	lastName = lname;
	sex = s;
	birthYear = byear;
	birthMonth = bmth;
	birthDay = bday;
}

void std::string Person::setName() const
{
	return name;
}

std::string Person::findName() const
{
	return name;
}

void Person::read(string lname)
{
	ifstream in;
	string line;
	bool more = true;
	in.open (fname.c.str());
	while (more)
	{
		std::in >> w;
		if(in.fail())
		more = fa;se;
	else
	{
		lname.push_back(w);
		getline (in, line);
		info.push_back(line)
	}
}
in.close()
		
}

bool Person::find (std::string a & f)
{
	{
		for (int i=0;i< lname.size();i++)
		{
			if (a==lname[i])
			{
				f = info[i]
				return true;
			}
		}
		return false;
				
}

void Person::print () const
{
	for (i=0;i<lname.size();i++)
	{
		std::cout << lname[i] << "\t";
		std::cout << info[i] << "\n";
	}
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namesapce std;

int main(int argc, char *argv[])
{
	if (argc < 6)
	{ std::cout << "usage" << arg[0] << lname\n";
		return 1;
	}
	Person per;
	per.read(arg v[i])
	bool name;
	while (more)
	{
		std:: cout << "enter last name";
		std::Cin >> w;
	}
} 
closed account (E0p9LyTq)
Use an overloaded I/O operator to seamlessly overload std::cout's insertion operator when the user has given their choice to output.

http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/
It seems that your code does not even compile.
You may want to check this line :
std::cout << "usage" << arg[0] << lname\n";
I am just having trouble with what I messed up here.

I know it is not compiling which is why i came here for help
If it does not compile, then you compiler must list what it thinks to be errors. It will mention a line number of the code file for each error. An IDE might even hyperlink error messages to source files.

It is very important to read what the compiler says and to learn to understand what it says. Always start solving from the first error.
There was a number of undefined variables and misspelled keywords. Even after I corrected all these small errors, the program was not working as intended. I'd start from correcting these small misspellings.

I second keskiverto. You do need to take hints from the compiler.

I will try to point out some stuff that may help you.
Take a look at your main function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namesapce std;

int main(int argc, char *argv[])
{
	if (argc < 6)
	{ std::cout << "usage" << arg[0] << lname\n";
		return 1;
	}
	Person per;
	per.read(arg v[i])
	bool name;
	while (more)
	{
		std:: cout << "enter last name";
		std::Cin >> w;
	}
} 


The assignment requires you to provide a filename as command line argument. If that's the case, the usage should be argv[0] << " fname\n" instead of lname. You are supposed to provide the filename as an argument, not the last name. Also, you can't have if( argc < 6 ). The program only requires 2 arguments: argv[0], which the the executable itself, and argv[1], which will be the filename. So, it should really be if( argc < 2 ) or if( argc != 2) if you want to aggressively enforce the number of arguments.

I will demonstrate the command line argument with this little test program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main(int argc, char *argv[])
{
	if (argc != 2 )
	{
	    std::cout << "usage " << argv[0] << " fname\n";
	    return 1;
	}
	else {
	    std::cout << "You have provided " << argc << " arguments\n";
	    std::cout << "The first argument is " << argv[0] << std::endl;
            std::cout << "The second argument, which is the fname, is " << argv[1] << std::endl;
	}

    return 0;
}


Here's a couple of test runs. I will assume that you are using Windows. This is done in the command prompt.

Example where I do not provide any argument
C:\Users\mpark\Desktop\Test\bin\Debug>Test.exe
usage Test.exe fname


Another example where I provide the correct argument
C:\Users\mpark\Desktop\Test\bin\Debug>Test.exe test.txt
You have provided 2 arguments
The first argument is Test.exe
The second argument, which is the fname, is test.txt


test.txt should be in the same directory as the executable file and have the following contents.
FirstName LastName Sex BirthYear BirthMonth BirthDay


Hope that helps.


[EDIT] One more thing to add. The program requires you to store multiple Person objects. So in the main function, you will have to use some sort of a container (array, list, vector etc) to store multiple Person objects and allow user to search the container.
Last edited on
Okay so after working on the program and trying my best to fix all compiler issues I got to my best but I am having trouble on how I read my file using a vector in the program, and also what I need in some of the functions in the person.cpp file.

Attaching code below:

Pleae guide on how to fix it.

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
#include <cctype>
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"
using namespace std;

void printMeFirst();

void promptUserForInput( vector<Person> &people )
{
    string firstName;
    /*string lname;
    string sex;
    int byear;
    int bmth;
    int bday;*/

    cout << "Enter the first name: ";
    cin >> ws;
    getline( std::cin , firstName );

    while( !isalpha(firstName[0]) ) 
    {
        cout << "Invalid Name!\n"
             << "Re-enter your name: ";
        getline( std::cin , firstName );
    }
   }

void printInformation(const vector<Person> &people)
{
    for( const auto &person : people ) 
    {
        person.printPerson();
    }
}


int main(int argc, char *argv[])
{
	printMeFirst();
	
	std::vector<Person> people;
    int choice = 0;
    
     while( people.size() < 30 && choice != 3 ) 
     {
        std::cout << "Menu\n\n";
        std::cout << "1. Enter Personal Information\n"
                  << "2. Print personal information\n"
                  << "3. Exit\n"
                  << "Enter your choice: ";
        std::cin >> choice;
	
	
        if( choice == 1 ) 
        {
            promptUserForInput( people );
        }
        else if( choice == 2 ) 
        {
            printInformation( people );
        }
        else if( choice == 3) 
        {
            std::cout << "\nGoodBye";
        }
        else 
        {
            std::cout << "\nInvalid Choice\n";
        }
    }
   
	if (argc != 2 )
	{
	    std::cout << "usage " << argv[0] << " fname\n";
	    return 1;
	}
	else
	{
	    std::cout << "The program will print the follwoing data " << argc << " as entered\n";
	    std::cout << "The name enter was " << argv[0] << std::endl;
            std::cout << "The info is " << argv[1] << std::endl;
	}

return 0;
}

// I TOTALLY UNDERSTAND HOW WE USE THESE ARGUMENTS HERE.. as mentioned by "mpark4656" but i dont know how I can use these to print the name and info for my program

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
person.cpp
[code]
#include <cctype>
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"
#include <fstream>
using namespace std;

Person::Person()
{
	firstName = "";
	lastName = "";
	sex = "";
	birthYear = 0;
	birthMonth = 0;
	birthYear = 0;
}

Person::Person(std::string fname, std::string lname , string s, int byear, int bmth, int bday)
{
	firstName = fname;
	lastName = lname;
	sex = s;
	birthYear = byear;
	birthMonth = bmth;
	birthDay = bday;
}

std::string Person::setName() const
{
	return firstName;
	return lastName;
}

std::string Person::setSex()
{
	return sex;
}

int Person::getbirthInfo() const
{
	return birthYear;
	return birthMonth;
	return birthDay;
}

std::string Person::findName() const
{
	return lastName;
	return firstName;
}

void Person::read(string lname)
{
	ifstream in;
	string line;
	bool more = true;
	in.open (firstName.c.str());
	while (more)
	{
		std::in >> w;
		if(in.fail())
		more = false;
	else
	{
		lname.push_back(w);
		getline (in, line);
		line.push_back(line);
	}
}
in.close();
		
}

bool Person::find (std::string a && std::string f)
{
	{
		for (int i=0;i< fname.size();i++)
		{
			if (a==firstName[i])
			{
				f = info[i]
				return true;
			}
		}
		return false;
				
}

void Person::printPerson () const
{
	for (i=0;i<firstName.size();i++)
	{
		std::cout << firstName[i] << "\t";
		std::cout << line[i] << "\n";
	}
}

person.h


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
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <string>

//The code below was provided during the lab.

class Person
{
	public:
		Person();
		Person(std::string fname, std::string lname , std::string s, int byear, int bmth, int bday );
		std::string findName (std::string lname) const;
		void setName (std::string fname, std::string lname);
		void setSex (std::string s);
		void setBirthInfo ( int byear, int bmth, int bday);
		void read (std::string lname);
		void printPerson () const;
		bool find (std::string a, std::string f) const;
		//void setAge( int age );
		//int getAge() const;
		//void print() const;

	private:
		std::string firstName;
		std::string lastName;
		std::string sex;
		int birthYear;
		int birthMonth;
		int birthDay;
		
};

#endif // PERSON_H_INCLUDED 


the FILE that i have to make the program read is in the original post at the very top. Take a look at that
Last edited on
I know you said I need to create an array with several person objects but I am not sure how to do that. Please explain if possible.

I appreciate all the help I can get
This should help you get started. I didn't include all the member functions. This is to demonstrate how to read from the file, create Person type object and store each object in the vector. I've also included operator<< because it can be useful in the future. I've included detailed comments so you know what is going on.

Person.h
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
#ifndef PERSON_H
#define PERSON_H

#include <iostream>
#include <string>

class Person
{
    public:
        // Default Constructible
        Person();

        // Multi-Parameter Constructor
        Person( std::string fname, std::string lname , char s, int byear, int bmth, int bday );

        /**
         *  Public function to display this person's information
         *      This function can be explicitly called by user, but
         *      overloaded operator<< will invoke this function as well.
         *  @param out std::ostream Output Stream
         *
         *  Note: If you want to, you can remove overloaded operator<<
         *        and simply call this function to display the information.
         *        It really depends on how flexible the assignment requirements
         *        are. In that case, you can remove the parameter of this function
         *        and simply use standard output.
         */
        void print( std::ostream &out ) const;

    private:
        std::string firstName;
		std::string lastName;
		char sex;
		int birthYear;
		int birthMonth;
		int birthDay;
};

/**
 *  Overloaded operator<< for Person object
 *  This allow you to display Person in the following fashion:
 *      std::cout << person;
 *  @param out std::ostream
 *  @param person Person
 *  @return out std::ostream
 */
std::ostream & operator<< ( std::ostream &out , const Person &person );

#endif // PERSON_H 


Person.cpp
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
#include "Person.h"

// Default Constructible
Person::Person()
: firstName{ "NoName" } , lastName{ "NoName" } , sex{ 'N' } , birthYear{ 0 } ,
  birthMonth{ 0 } , birthDay{ 0 }
{}

// Multi-Parameter Constructor
Person::Person( std::string fname, std::string lname , char s, int byear, int bmth, int bday )
: firstName{ fname } , lastName{ lname } , sex{ s } , birthYear{ byear } ,
  birthMonth{ bmth } , birthDay{ bday }
{}


/**
 *  Public function to display this person's information
 *      This function can be explicitly called by user, but
 *      overloaded operator<< will invoke this function as well.
 *  @param out std::ostream Output Stream
 *
 *  Note: If you want to, you can remove overloaded operator<<
 *        and simply call this function to display the information.
 *        It really depends on how flexible the assignment requirements
 *        are. In that case, you can remove the parameter of this function
 *        and simply use standard output.
 */
void Person::print( std::ostream &out ) const
{
    out << "First Name: " << firstName << "\n"
        << "Last Name: " << lastName << "\n"
        << "Gender: " << sex << "\n"
        << "Date of Birth: " << birthMonth << '/' << birthDay << '/'
            << birthYear << "\n";
}

/**
 *  Overloaded operator<< for Person object
 *  This allow you to display Person in the following fashion:
 *      std::cout << person;
 *  @param out std::ostream
 *  @param person Person
 *  @return out std::ostream
 */
std::ostream & operator<< ( std::ostream &out , const Person &person )
{
    person.print( out );
    return out;
}



main.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "Person.h"

/*********************************************************************
*                       Function Prototypes                          *
*********************************************************************/
/**
 *  Function to print all contents in Person type vector.
 *  @param people std::vector<Person>
 */
void print( const std::vector<Person> &people );

/**
 *  Function to read contents of the file and return Person type
 *      vector that contains the information of each person from
 *      the file
 *      This is the function that accepts argv[1], which
 *      is the command-line argument.
 *  @param fileName std::string
 *  @return people std::vector<Person>
 */
std::vector<Person> read( std::string fileName );


/*********************************************************************
*                           Main Function                            *
*********************************************************************/
int main( int argc , char* argv[] )
{
    // If the correct number of command-line parameters aren't provided
    // Print correct usage and exit program.
    if( argc != 2 ) {
        std::cout << "usage " << argv[0] << " fileName\n";
        return 1;
    }

    // Create Person type vector. Call read().
    std::vector<Person> people{ read(argv[1]) };

    int choice;

    do {
        std::cout << "\n\tMenu\n\n"
                  << "1. Search a person by the last name\n"
                  << "2. Print information of all personnel\n"
                  << "3. Exit\n" << std::endl
                  << "Enter your choice: ";
        std::cin >> choice;

        switch( choice ) {
            case 1:
                // Write search function and call it here.
                break;
            case 2:
                print( people );
                break;
            case 3:
                break;
            default:
                std::cout << "\nYou did not enter a valid choice.\n";

                // Restore std::cin in case user entered characters where int is expected
                std::cin.clear();
                std::cin.ignore( 100 , '\n' );
                choice = 0;
        }
    }
    while( choice != 3 );

    return 0;
}






/*********************************************************************
*                           Functions Definition                     *
*********************************************************************/
std::vector<Person> read( std::string fileName )
{
    // Create a Person type vector to store multiple Person's record
    std::vector<Person> people;

    // Declare input stream initialized with the filename
    std::ifstream in{ fileName };

    // Declare the variables to store information from the file
    std::string fName;
    std::string lName;
    char sex;
    int bYear;
    int bMonth;
    int bDay;

    // While the first piece of information is successfully read from the file,
    // 1. Read the rest of the information from it and store the info in each
    //    corresponding variable.
    // 2. Create Person object initialized with the variables and push the object
    //    to the vector. This is where the second constructor of Person is invoked.
    // Assumes that the file has the information in the correct order with
    //  each variable separated by whitespace.
    while( in >> fName ) {
        in >> lName >> sex >> bYear >> bMonth >> bDay;
        people.push_back( Person{ fName , lName , sex , bYear , bMonth , bDay } );
    }

    // Return the vector
    return people;
}

void print( const std::vector<Person> &people )
{
    std::cout << std::endl;

    for( auto pers : people ) {
        std::cout << pers << std::endl;
    }
}


personsInfo.txt
Hillary Clinton F 1947 10 26
Donald Trump M 1946 06 14
Bernie Sanders M 1941 09 08
Barack Obama M 1961 08 04
Melania Trump F 1970 04 26
Michelle Obama F 1964 01 17
Last edited on
Test Run
C:\Users\mpark\Desktop\People\bin\Debug>People.exe personsInfo.txt

Menu

1. Search a person by the last name
2. Print information of all personnel
3. Exit

Enter your choice: 2

First Name: Hillary
Last Name: Clinton
Gender: F
Date of Birth: 10/26/1947

First Name: Donald
Last Name: Trump
Gender: M
Date of Birth: 6/14/1946

First Name: Bernie
Last Name: Sanders
Gender: M
Date of Birth: 9/8/1941

First Name: Barack
Last Name: Obama
Gender: M
Date of Birth: 8/4/1961

First Name: Melania
Last Name: Trump
Gender: F
Date of Birth: 4/26/1970

First Name: Michelle
Last Name: Obama
Gender: F
Date of Birth: 1/17/1964


Menu

1. Search a person by the last name
2. Print information of all personnel
3. Exit

Enter your choice: 3

C:\Users\mpark\Desktop\People\bin\Debug>
Topic archived. No new replies allowed.