Results 1 to 2 of 2

Thread: use vectors to read a file and then print info.

  1. #1

    Default use vectors to read a file and then print info.

    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
    Qt Code:
    1. Hillary Clinton F 1947 10 26
    2. Donald Trump M 1946 06 14
    3. Bernie Sanders M 1941 09 08
    4. Barack Obama M 1961 08 04
    5. Melania Trump F 1970 04 26
    6. Michelle Obama F 1964 01 17
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. #include <cctype>
    2. #include <iostream>
    3. #include <vector>
    4. #include <string>
    5. #include "Person.h"
    6. using namespace std;
    7.  
    8. void printMeFirst();
    9.  
    10. void promptUserForInput( vector<Person> &people )
    11. {
    12. string firstName;
    13. /*string lname;
    14.   string sex;
    15.   int byear;
    16.   int bmth;
    17.   int bday;*/
    18.  
    19. cout << "Enter the first name: ";
    20. cin >> ws;
    21. getline( std::cin , firstName );
    22.  
    23. while( !isalpha(firstName[0]) )
    24. {
    25. cout << "Invalid Name!\n"
    26. << "Re-enter your name: ";
    27. getline( std::cin , firstName );
    28. }
    29. }
    30.  
    31. void printInformation(const vector<Person> &people)
    32. {
    33. for( const auto &person : people )
    34. {
    35. person.printPerson();
    36. }
    37. }
    38.  
    39.  
    40. int main(int argc, char *argv[])
    41. {
    42. printMeFirst();
    43.  
    44. std::vector<Person> people;
    45. int choice = 0;
    46.  
    47. while( people.size() < 30 && choice != 3 )
    48. {
    49. std::cout << "Menu\n\n";
    50. std::cout << "1. Enter Personal Information\n"
    51. << "2. Print personal information\n"
    52. << "3. Exit\n"
    53. << "Enter your choice: ";
    54. std::cin >> choice;
    55.  
    56.  
    57. if( choice == 1 )
    58. {
    59. promptUserForInput( people );
    60. }
    61. else if( choice == 2 )
    62. {
    63. printInformation( people );
    64. }
    65. else if( choice == 3)
    66. {
    67. std::cout << "\nGoodBye";
    68. }
    69. else
    70. {
    71. std::cout << "\nInvalid Choice\n";
    72. }
    73. }
    74.  
    75. if (argc != 2 )
    76. {
    77. std::cout << "usage " << argv[0] << " fname\n";
    78. return 1;
    79. }
    80. else
    81. {
    82. std::cout << "The program will print the follwoing data " << argc << " as entered\n";
    83. std::cout << "The name enter was " << argv[0] << std::endl;
    84. std::cout << "The info is " << argv[1] << std::endl;
    85. }
    86.  
    87. return 0;
    88. }
    To copy to clipboard, switch view to plain text mode 


    [/code]
    person.cpp
    Qt Code:
    1. #include <cctype>
    2. #include <iostream>
    3. #include <vector>
    4. #include <string>
    5. #include "Person.h"
    6. #include <fstream>
    7. using namespace std;
    8.  
    9. Person::Person()
    10. {
    11. firstName = "";
    12. lastName = "";
    13. sex = "";
    14. birthYear = 0;
    15. birthMonth = 0;
    16. birthYear = 0;
    17. }
    18.  
    19. Person::Person(std::string fname, std::string lname , string s, int byear, int bmth, int bday)
    20. {
    21. firstName = fname;
    22. lastName = lname;
    23. sex = s;
    24. birthYear = byear;
    25. birthMonth = bmth;
    26. birthDay = bday;
    27. }
    28.  
    29. std::string Person::setName() const
    30. {
    31. return firstName;
    32. return lastName;
    33. }
    34.  
    35. std::string Person::setSex()
    36. {
    37. return sex;
    38. }
    39.  
    40. int Person::getbirthInfo() const
    41. {
    42. return birthYear;
    43. return birthMonth;
    44. return birthDay;
    45. }
    46.  
    47. std::string Person::findName() const
    48. {
    49. return lastName;
    50. return firstName;
    51. }
    52.  
    53. void Person::read(string lname)
    54. {
    55. ifstream in;
    56. string line;
    57. bool more = true;
    58. in.open (firstName.c.str());
    59. while (more)
    60. {
    61. std::in >> w;
    62. if(in.fail())
    63. more = false;
    64. else
    65. {
    66. lname.push_back(w);
    67. getline (in, line);
    68. line.push_back(line);
    69. }
    70. }
    71. in.close();
    72.  
    73. }
    74.  
    75. bool Person::find (std::string a && std::string f)
    76. {
    77. {
    78. for (int i=0;i< fname.size();i++)
    79. {
    80. if (a==firstName[i])
    81. {
    82. f = info[i]
    83. return true;
    84. }
    85. }
    86. return false;
    87.  
    88. }
    89.  
    90. void Person::printPerson () const
    91. {
    92. for (i=0;i<firstName.size();i++)
    93. {
    94. std::cout << firstName[i] << "\t";
    95. std::cout << line[i] << "\n";
    96. }
    97. }
    To copy to clipboard, switch view to plain text mode 
    person.h

    Qt Code:
    1. #ifndef PERSON_H_INCLUDED
    2. #define PERSON_H_INCLUDED
    3. #include <string>
    4.  
    5. //The code below was provided during the lab.
    6.  
    7. class Person
    8. {
    9. public:
    10. Person();
    11. Person(std::string fname, std::string lname , std::string s, int byear, int bmth, int bday );
    12. std::string findName (std::string lname) const;
    13. void setName (std::string fname, std::string lname);
    14. void setSex (std::string s);
    15. void setBirthInfo ( int byear, int bmth, int bday);
    16. void read (std::string lname);
    17. void printPerson () const;
    18. bool find (std::string a, std::string f) const;
    19. //void setAge( int age );
    20. //int getAge() const;
    21. //void print() const;
    22.  
    23. private:
    24. std::string firstName;
    25. std::string lastName;
    26. std::string sex;
    27. int birthYear;
    28. int birthMonth;
    29. int birthDay;
    30.  
    31. };
    32.  
    33. #endif // PERSON_H_INCLUDED
    To copy to clipboard, switch view to plain text mode 

    the FILE that i have to make the program read is in the original post at the very top. Take a look at that

  2. #2
    Join Date
    Oct 2016
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: use vectors to read a file and then print info.

    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.

Similar Threads

  1. Replies: 2
    Last Post: 18th March 2016, 11:51
  2. Replies: 0
    Last Post: 11th April 2010, 21:26
  3. qmake file info
    By Lodorot in forum Qt Programming
    Replies: 1
    Last Post: 4th September 2009, 22:23
  4. File version info
    By johncharlesb in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2007, 02:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.