PDA

View Full Version : use vectors to read a file and then print info.



hamzams25
12th October 2016, 20:49
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


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


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.



#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;
}



[/code]
person.cpp


#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



#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

jacobP
12th October 2016, 22:21
This is a forum to help with Qt issues, not a place to have other do your school assignments for you...

Start by breaking down the work you have to do :

-Have your program load the .txt correctly
-Have your program read the .txt correctly
-Once you know you read the file correctly, construct the person objects with the info you read from the file. By the looks of it your person.cpp is in very bad shape. Start by fixing that and making sure your Person class has all it's member correctly functioning before worrying about vectors.

Once all that is done, you will have easy-to-handle objects with all the info needed and writing the code that handles the user input will be much easier to think about.