Could someone help me Debug this code

I have a couple of questions through out the code. I will try to ask them. Please help answer the ones possible.

My first concern is that is my person.cpp file correct? or does it seem like it is missing functions? If its missing anything, I want to work on adding those.

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


Question 2:
Would I declare name in serveral functions?
Such as Person::Person
Person::SetName
Person::sex
Person::setbirthAge

such as below

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

void std::string Person::setName() const
{
    firstName = fname;
    lastName = lname;
}

void std::string Person::setSex()
{
    sex = s;
}

void Person::setBirthInfo()
{
    birthYear = byear;
    birthMonth = bmth;
    birthDay = bday;
}


Because here it seems like I am doing the same thing over and over. So if you can explain that please.
I really dont understand it at the moment.

Question 3

Where should i declare line and how? I want to be able to just output line and expect the program to know what to print.
But im not sure about declaring line.

Question 4

What exactly is wrong with these functions? It keeps giving an error that c is not declared.
I also am not able to get the program to run the .txt file successfully.
So what should I fix in teh codes below:

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


Question 5

Could you please explain arc and argv? I know they work somewhat like pointers to my understanding but im not sure how?

Question 5:

I am trying to declare objects in the main function after I print out the menu. But i am having some trouble getting started. What do you suggest I should start with?

I was trying to declare them as an array. But im not sure What it shoudl look like?

1
2
3
4
5
6
7
8
9
 Function[]
{
string fname;
string lname;
string sex;
int byear;
int bmth;
int bday;
}


But im not sure how I can make it run in the main function and then call it. Would i need a for function or something similar?
Please explain

Thank you

My code is below for reference

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


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


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
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
#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
35
36
37
#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 
Last edited on
Please. Never make the same post more than Once, especially not 3 times. Stop spamming.

http://www.cplusplus.com/forum/general/199725/
Sorry I didn't mean to spam. I just didn't know which forum was the correct one to post it in.

Can you please help me?
Sorry I didn't mean to spam. I just didn't know which forum was the correct one to post it in.

At the top of each forum is a sticky which tells you what is topical. The one at the top of this forum, for instance says:

Welcome to the lounge forum in C++.com!

In this forum, users can to talk about topics other than programming.


I responded in a thread/forum where the code was on topic, but you haven't addressed that post (which suggested that you try to compile what you have and pay attention to the compiler-generated errors.) But, please don't respond in this thread. Respond in the thread where the suggestion was made.
Topic archived. No new replies allowed.