1. Please help me debug my program 2. How to create a makefile to link them toge
ID: 3592343 • Letter: 1
Question
1. Please help me debug my program
2. How to create a makefile to link them together ?
This is my account.cpp below
//Main program main.cpp
#include "Person.h"
#include "Person.cpp"
#include <vector>
#include <ctime>
#include <sstream>
#include <fstream>
#include <algorithm> // used for vector sort
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <iomanip>
#include <cmath>
#include <string>
#include <climits>
using namespace std;
string trim_words(string sentence);
vector<Person> readFile(string filename);
int printMenu();
int main(int argc, char* argv[])
{
stringstream ss;
string s, sentence;
//string out;
string str,str1, fileName;
/*sample code, your code here*/
if (argc < 2)
{
cout <<"Usage: " << argv[0] << " input_file ";
exit(0);
}
else
fileName = argv[1];
vector<Person> list = readFile(fileName);
string userInput;
int presidencyTerm;
do
{
int choice = printMenu();
switch(choice)
{
case 1:
cout<<" Enter the Person's name:";
cin >> userInput;
cout<<endl;
for(int idx = 0; idx < list.size() - 1; idx++)
{
if(list[idx].getName().find(userInput) != -1)
cout<<list[idx]<<endl<<endl;
}
break;
case 2:
cout<<" Enter the Presidency term: ";
cin >> presidencyTerm;
while(presidencyTerm < 1)
{
cout<<" Wrong input. Please reenter: ";
cin >> presidencyTerm;
}
cout<<endl;
for(int idx = 0; idx < list.size() - 1; idx++)
{
if(list[idx].getPresidency() == presidencyTerm)
cout<<list[idx]<<endl<<endl;
}
break;
case 3:
cout << " Enter political Party name: ";
cin >> userInput;
cout<<endl;
for(int idx = 0; idx < list.size() - 1; idx++)
{
if(list[idx].getParty() == userInput)
cout<<list[idx]<<endl<<endl;
}
break;
case 4:
exit(0);
default:
cout<<" Wrong choice!!"<<endl;
}
}
while(1);
return 0;
}
string trim_words(string s2)
{
stringstream ss;
string s;
string out;
ss << s2;
while (ss >> s)
{
out += (s + ' ');
}
return out.substr(0, out.length() - 1);
}
vector<Person> readFile(string fileName)
{
string str,str1;
vector<Person> presidentList;
int presidency, birthYear, deceasedYear;
string name, startDate, endDate, party;
ifstream myfile (fileName.c_str()); // open the file
if (myfile.is_open())
{
while (myfile)
{
if (!getline(myfile, str))
break; //end of file
istringstream split(str);
vector<string> tokens;
getline(split, str1, ';');
str1 = trim_words(str1);
presidency = stoi(str1);
getline(split, str1, ';');
name = trim_words(str1);
getline(split, str1, ';');
startDate = trim_words(str1);
getline(split, str1, ';');
endDate = trim_words(str1);
getline(split, str1, ';');
party = trim_words(str1);
getline(split, str1, ';');
str1 = trim_words(str1);
birthYear = stoi(str1);
getline(split, str1, ';');
str1 = trim_words(str1);
deceasedYear = stoi(str1);
Person person;
person.setPresidency(presidency);
person.setName(name);
person.setBirthYear(birthYear);
person.setDeceasedYear(deceasedYear);
person.setEndDate(endDate);
person.setStartDate(startDate);
person.setParty(party);
presidentList.push_back(person);
}
}
else
{
cout<<"Not opened!"<<endl;
}
return presidentList;
}
int printMenu()
{
cout<<"1. Search person's information based on name."
" (enter person's name and will print out the information)"
" for all the records that matches person's name) ";
cout <<"2. Search person's Information based on presidency term. ";
cout << "3. Search person's information based on political Party. ";
cout << "4. Exit. ";
cout<<">> ";
int choice;
cin >> choice;
return choice;
}
This is my Person.cpp below
#include "Person.h"
Person::Person()
{
presidency = 0;
birthYear = 1901;
deceasedYear = 1901;
name = "";
startDate = "";
endDate = "";
party = "";
}
void Person::setPresidency(int presidencyNum)
{
presidency = presidencyNum;
}
void Person::setBirthYear(int birth)
{
birthYear = birth;
}
void Person::setDeceasedYear(int deceased)
{
deceasedYear = deceased;
}
void Person::setName(string presidentName)
{
name = presidentName;
}
void Person::setStartDate(string start)
{
startDate = start;
}
void Person::setEndDate(string end)
{
endDate = end;
}
void Person::setParty(string partyName)
{
party = partyName;
}
int Person::getPresidency() const
{
return presidency;
}
int Person::getBirthYear() const
{
return birthYear;
}
int Person::getDeceasedYear() const
{
return deceasedYear;
}
string Person::getName() const
{
return name;
}
string Person::getStartDate() const
{
return startDate;
}
string Person::getEndDate() const
{
return endDate;
}
string Person::getParty() const
{
return party;
}
bool Person::operator==(const string& personName)
{
return name == personName;
}
std::ostream & operator<<(std::ostream &os, const Person& p)
{
os << p.getName() << " ("<<p.getBirthYear()<<" - ";
if(p.getDeceasedYear() == 0)
os << "Present) Age: " << (2017 - p.getBirthYear())<<" ";
else
os << p.getDeceasedYear() << ") Age: " << (p.getDeceasedYear() - p.getBirthYear())<<" ";
os << "Presidency #" << p.getPresidency() << " from " << p.getStartDate() <<" to ";
if (p.getEndDate() == "0")
os << "Present Party: "<<p.getParty();
else
os << p.getEndDate()<<" Party: "<<p.getParty();
return os;
}
This is my Person.h below
// Create header file Person.h with
#include <string>
#include <iostream>
using namespace std;
class Person
{
public:
Person();
void setPresidency(int presidencyNum);
void setBirthYear(int birth);
void setDeceasedYear(int deceased);
void setName(string presidentName);
void setStartDate(string start);
void setEndDate(string end);
void setParty(string partyName);
int getPresidency() const;
int getBirthYear() const;
int getDeceasedYear() const;
string getName() const;
string getStartDate() const;
string getEndDate() const;
string getParty() const;
bool operator==(const string& personName);
friend ostream & operator<<(ostream &os, const Person& p);
private:
int presidency, birthYear, deceasedYear;
string name, startDate, endDate, party;
};
This is my Printmefirst.h below
#ifndef PRINTMEFIRST_H_
#define PRINTMEFIRST_H_
#include <string>
#include <iostream>
void printMeFirst(std::string name,std::string courseInfor);
#endif
This is my PrintMeFirst.cpp below
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <iomanip>
#include <cmath>
#include <ctime>
#include "Printmefirst.h"
using namespace std;
/* Print out the programmer's information such as name, class information and dat/time when the program is run
@param name- the name of the programmer
@param couselnfor- the name of the course
@return- none
*/
void printMeFirst(string name,string courseInfor)
{
cout<<"Program written by:"<<name<<endl;
cout<<"Course infor:"<<courseInfor<<endl;
time_t now=time(0);
char* dt= ctime(&now);
cout<<"Date:"<< dt <<endl;
}
Here is the US presidents list-txt information below:
1; George Washington; April 30, 1789 ; March 4, 1797 ; Independent; 1732; 1799
2; John Adams; March 4, 1797 ; March 4, 1801 ; Federalist; 1735; 1826
3; Thomas Jefferson; March 4, 1801 ; March 4, 1809 ; Democratic; 1743; 1826
4; James Madison; March 4, 1809 ; March 4, 1817 ; Democratic; 1751; 1836
5; James Monroe; March 4, 1817 ; March 4, 1825 ; Democratic; 1758; 1831
6; John Quincy Adams; March 4, 1825 ; March 4, 1829 ; Democratic; 1767; 1848
7; Andrew Jackson; March 4, 1829 ; March 4, 1837 ; Democratic; 1767; 1845
8; Martin Van Buren; March 4, 1837 ; March 4, 1841 ; Democratic; 1782; 1862
9; William Henry Harrison; March 4, 1841 ; April 4, 1841 ; Whig; 1773; 1841
10; John Tyler; April 4, 1841 ; March 4, 1845 ; Whig; 1790; 1862
11; James K. Polk; March 4, 1845 ; March 4, 1849 ; Democratic; 1795; 1849
12; Zachary Taylor; March 4, 1849 ; July 9, 1850 ; Whig; 1784; 1850
13; Millard Fillmore; July 9, 1850 ; March 4, 1853 ; Whig; 1800; 1874
14; Franklin Pierce; March 4, 1853 ; March 4, 1857 ; Democratic; 1804; 1869
15; James Buchanan; March 4, 1857 ; March 4, 1861 ; Democratic; 1791; 1838
16; Abraham Lincoln; March 4, 1861 ; April 15, 1865 ; Republican; 1809; 1865
17; Andrew Johnson; April 15, 1865 ; March 4, 1869 ; Democratic; 1808; 1875
18; Ulysses S. Grant; March 4, 1869 ; March 4, 1877 ; Republican; 1822; 1885
19; Rutherford B. Hayes; March 4, 1877 ; March 4, 1881 ; Republican; 1822; 1893
20; James A. Garfield; March 4, 1881 ; September 19, 1881 ; Republican; 1831; 1881
21; Chester A. Arthur; September 19, 1881 ; March 4, 1885 ; Republican; 1829; 1886
22; Grover Cleveland; March 4, 1885 ; March 4, 1889 ; Democratic; 1837; 1908
23; Benjamin Harrison; March 4, 1889 ; March 4, 1893 ; Republican; 1833; 1901
24; Grover Cleveland; March 4, 1893 ; March 4, 1897 ; Democratic; 1837; 1908
25; William McKinley; March 4, 1897 ; September 14, 1901 ; Republican; 1743; 1901
26; Theodore Roosevelt; September 14, 1901 ; March 4, 1909 ; Republican; 1858; 1919
27; William Howard Taft; March 4, 1909 ; March 4, 1913 ; Republican; 1857; 1930
28; Woodrow Wilson; March 4, 1913 ; March 4, 1921 ; Democratic; 1856; 1924
29; Warren G. Harding; March 4, 1921 ; August 2, 1923 ; Republican; 1865; 1923
30; Calvin Coolidge; August 2, 1923 ; March 4, 1929 ; Republican; 1872; 1933
31; Herbert Hoover; March 4, 1929 ; March 4, 1933 ; Republican; 1874; 1964
32; Franklin D. Roosevelt; March 4, 1933 ; April 12, 1945 ; Democratic; 1882; 1945
33; Harry S. Truman; April 12, 1945 ; January 20, 1953 ; Democratic; 1884; 1972
34; Dwight D. Eisenhower; January 20, 1953 ; January 20, 1961 ; Republican; 1890; 1969
35; John F. Kennedy; January 20, 1961 ; November 22, 1963 ; Democratic; 1917; 1963
36; Lyndon B. Johnson; November 22, 1963 ; January 20, 1969 ; Democratic; 1908; 1973
37; Richard Nixon; January 20, 1969 ; August 9, 1974 ; Republican; 1913; 1994
38; Gerald Ford; August 9, 1974 ; January 20, 1977 ; Republican; 1913; 2006
39; Jimmy Carter; January 20, 1977 ; January 20, 1981 ; Democratic; 1924; 0
40; Ronald Reagan; January 20, 1981 ; January 20, 1989 ; Republican; 1911; 2004
41; George H. W. Bush; January 20, 1989 ; January 20, 1993 ; Republican; 1924; 0
42; Bill Clinton; January 20, 1993 ; January 20, 2001 ; Democratic; 1946; 0
43; George W. Bush; January 20, 2001 ; January 20, 2009 ; Republican; 1946; 0
44; Barack Obama; January 20, 2009 ; January 20, 2017 ; Democratic; 1961; 0
45; Donald Trump; January 20, 2017 ; 0; Republican; 1946; 0
Explanation / Answer
the code is fixed without errors
#include "Person.h"
#include "Person.cpp"
#include <vector>
#include <ctime>
#include <sstream>
#include <fstream>
#include <algorithm> // used for vector sort
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <iomanip>
#include <cmath>
#include <string>
#include <climits>
using namespace std;
string trim_words(string sentence);
vector<Person> readFile(string filename);
int printMenu();
int main(int argc, char* argv[])
{
stringstream ss;
string s, sentence;
//string out;
string str,str1, fileName;
/*sample code, your code here*/
if (argc < 2)
{
cout <<"Usage: " << argv[0] << " input_file ";
exit(0);
}
else
fileName = argv[1];
vector<Person> list = readFile(fileName);
string userInput;
int presidencyTerm;
do
{
int choice = printMenu();
switch(choice)
{
case 1:
cout<<" Enter the Person's name:";
cin >> userInput;
cout<<endl;
for(int idx = 0; idx < list.size() - 1; idx++)
{
if(list[idx].getName().find(userInput) != -1)
cout<<list[idx]<<endl<<endl;
}
break;
case 2:
cout<<" Enter the Presidency term: ";
cin >> presidencyTerm;
while(presidencyTerm < 1)
{
cout<<" Wrong input. Please reenter: ";
cin >> presidencyTerm;
}
cout<<endl;
for(int idx = 0; idx < list.size() - 1; idx++)
{
if(list[idx].getPresidency() == presidencyTerm)
cout<<list[idx]<<endl<<endl;
}
break;
case 3:
cout << " Enter political Party name: ";
cin >> userInput;
cout<<endl;
for(int idx = 0; idx < list.size() - 1; idx++)
{
if(list[idx].getParty() == userInput)
cout<<list[idx]<<endl<<endl;
}
break;
case 4:
exit(0);
default:
cout<<" Wrong choice!!"<<endl;
}
}
while(1);
return 0;
}
string trim_words(string s2)
{
stringstream ss;
string s;
string out;
ss << s2;
while (ss >> s)
{
out += (s + ' ');
}
return out.substr(0, out.length() - 1);
}
vector<Person> readFile(string fileName)
{
string str,str1;
vector<Person> presidentList;
int presidency, birthYear, deceasedYear;
string name, startDate, endDate, party;
ifstream myfile (fileName.c_str()); // open the file
if (myfile.is_open())
{
while (myfile)
{
if (!getline(myfile, str))
break; //end of file
istringstream split(str);
vector<string> tokens;
getline(split, str1, ';');
str1 = trim_words(str1);
presidency = stoi(str1);
getline(split, str1, ';');
name = trim_words(str1);
getline(split, str1, ';');
startDate = trim_words(str1);
getline(split, str1, ';');
endDate = trim_words(str1);
getline(split, str1, ';');
party = trim_words(str1);
getline(split, str1, ';');
str1 = trim_words(str1);
birthYear = stoi(str1);
getline(split, str1, ';');
str1 = trim_words(str1);
deceasedYear = stoi(str1);
Person person;
person.setPresidency(presidency);
person.setName(name);
person.setBirthYear(birthYear);
person.setDeceasedYear(deceasedYear);
person.setEndDate(endDate);
person.setStartDate(startDate);
person.setParty(party);
presidentList.push_back(person);
}
}
else
{
cout<<"Not opened!"<<endl;
}
return presidentList;
}
int printMenu()
{
cout<<"1. Search person's information based on name."
" (enter person's name and will print out the information)"
" for all the records that matches person's name) ";
cout <<"2. Search person's Information based on presidency term. ";
cout << "3. Search person's information based on political Party. ";
cout << "4. Exit. ";
cout<<">> ";
int choice;
cin >> choice;
return choice;
}
This is my Person.cpp below
#include "Person.h"
Person::Person()
{
presidency = 0;
birthYear = 1901;
deceasedYear = 1901;
name = "";
startDate = "";
endDate = "";
party = "";
}
void Person::setPresidency(int presidencyNum)
{
presidency = presidencyNum;
}
void Person::setBirthYear(int birth)
{
birthYear = birth;
}
void Person::setDeceasedYear(int deceased)
{
deceasedYear = deceased;
}
void Person::setName(string presidentName)
{
name = presidentName;
}
void Person::setStartDate(string start)
{
startDate = start;
}
void Person::setEndDate(string end)
{
endDate = end;
}
void Person::setParty(string partyName)
{
party = partyName;
}
int Person::getPresidency() const
{
return presidency;
}
int Person::getBirthYear() const
{
return birthYear;
}
int Person::getDeceasedYear() const
{
return deceasedYear;
}
string Person::getName() const
{
return name;
}
string Person::getStartDate() const
{
return startDate;
}
string Person::getEndDate() const
{
return endDate;
}
string Person::getParty() const
{
return party;
}
bool Person::operator==(const string& personName)
{
return name == personName;
}
std::ostream & operator<<(std::ostream &os, const Person& p)
{
os << p.getName() << " ("<<p.getBirthYear()<<" - ";
if(p.getDeceasedYear() == 0)
os << "Present) Age: " << (2017 - p.getBirthYear())<<" ";
else
os << p.getDeceasedYear() << ") Age: " << (p.getDeceasedYear() - p.getBirthYear())<<" ";
os << "Presidency #" << p.getPresidency() << " from " << p.getStartDate() <<" to ";
if (p.getEndDate() == "0")
os << "Present Party: "<<p.getParty();
else
os << p.getEndDate()<<" Party: "<<p.getParty();
return os;
}
This is my Person.h below
// Create header file Person.h with
#include <string>
#include <iostream>
using namespace std;
class Person
{
public:
Person();
void setPresidency(int presidencyNum);
void setBirthYear(int birth);
void setDeceasedYear(int deceased);
void setName(string presidentName);
void setStartDate(string start);
void setEndDate(string end);
void setParty(string partyName);
int getPresidency() const;
int getBirthYear() const;
int getDeceasedYear() const;
string getName() const;
string getStartDate() const;
string getEndDate() const;
string getParty() const;
bool operator==(const string& personName);
friend ostream & operator<<(ostream &os, const Person& p);
private:
int presidency, birthYear, deceasedYear;
string name, startDate, endDate, party;
};
This is my Printmefirst.h below
#ifndef PRINTMEFIRST_H_
#define PRINTMEFIRST_H_
#include <string>
#include <iostream>
void printMeFirst(std::string name,std::string courseInfor);
#endif
This is my PrintMeFirst.cpp below
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <iomanip>
#include <cmath>
#include <ctime>
#include "Printmefirst.h"
using namespace std;
/* Print out the programmer's information such as name, class information and dat/time when the program is run
@param name- the name of the programmer
@param couselnfor- the name of the course
@return- none
*/
void printMeFirst(string name,string courseInfor)
{
cout<<"Program written by:"<<name<<endl;
cout<<"Course infor:"<<courseInfor<<endl;
time_t now=time(0);
char* dt= ctime(&now);
cout<<"Date:"<< dt <<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.