help with string type error

hello. hadn't coded in c++ in a while and am trying to do an assignment. the prompt is
Implement a class Person with two fields name and age, and a class Company with three fields:
• the company name
• a pointer to the CEO (a Person*)
• a pointer to a FOUNDER (a Person*)

Write a program that prompts the user to specify people and companies. Store them in a vector<Person*> and a vector<Company*>. Traverse the vector of Person objects and increment their ages by one year. Finally, traverse the vector of Companies and print out the company name, CEO name and age, and founder’s name and age.


i think i have coded everything i need to but can't get it to compile and dont understand the errors it's giving me. More specifically, i think the problem is the compiler not recognizing string as a type, since almost all the errors contain a variable or function i used the string type on. what can i do to fix this? here is my code

Person header and implementation
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
  #ifndef Person_h
#define Person_h
#include <string>
class Person
{
 public:
        Person(string n, int a);
        int getAge();
        string getName();
        void Person::increment_age();
        void print();
 private:
         string name;
         int age;
};
#endif

#include "Person.h"
#include <iostream>
#include <string>
using namespace std;

Person::Person(string n, int a)
{
name = n;
age = a;
}

int Person::getAge()
{
 return age;
}

string Person::getName()
{
 return name;
}

void Person::increment_age()
{
age += 1;
}

void Person::print()
{
cout << "name: " << name << endl;
cout << "age: " << age << endl;
}


Company header and implementation
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
class Company
{
 public:
    Company::Company(string);
    void Company::set_CEO(Person*);
    void Company::set_FOUNDER(Person*);
    void print()const;
 private:
    string cName;
    Person *CEO;
    Person *FOUNDER;
};

#include "Company.h"
#include <iostream>
using namespace std;

Company::Company(string name)
{
 cName = name;
}

void Company::set_CEO(Person* i)
{
 *CEO = *i;
}

void Company::set_FOUNDER(Person* i)
{
 *FOUNDER = *i;
}
    
void Company::print() const
{
 cout << "company name: " << cName << endl;
 cout << "CEO: ";
 CEO.print();
 cout << "FOUNDER: ";
 FOUNDER.print();
}


main 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
#include <iostream>
#include <string>

#include "Person.h"
#include "Company.h"

int main()
{
 int ages[100];
 int cCount = 0;
 int i = 0;
 int j = 0;
 string names[100];
 string tempC;
 string tempCEO;
 string tempFOUNDER;
 vector<Person*> people;
 vector<Company*> companies;
 char ans = 'y';
 while (ans = y)
 {
  cout << "enter name of person: ";
  cin >> names[i];
  cout << "enter the age of that person: ";
  cin >> ages[i];
  cout << "enter another person(y for yes, n for no)?: ";\
  cin >> ans;
  tolower(ans);
  i++;
 }
 const int numPeople = i;
 for ( int i = 0; i < numPeople; i++)
 {
  Person *a = new Person(names[i], ages[i]);
  people.push_back(a);
 }
 ans = y;
 i = 0;
 while (ans = y)
 {
  cout << "enter a company name: ";
  cin >> tempC;
  Company *b = new Company( tempC );
  companies.push_back(b);
  cout << "who is the CEO of this company?: ";
  cin >> tempCEO;
  cout << "who is the founder of this company?: ";
  cin >> tempFOUNDER;
  for (int x = 0; x < numPeople; x++)
  {
   if ( tempCEO == people[i] )
      b.Company::set_CEO(people[i]);
  }
  for (int x = 0; x < numPeople; x++)
  {
   if ( tempFOUNDER == people[i] )
      b.Company::set_FOUNDER(people[i]);
  }
  cout << "enter another company? (y for yes n for no): ";
  cin >> ans;
  tolower(ans);
  cCount++;
 }
 for( int x = 0; x < numPeople; x++)
 {
  people[x]->increment_age;
 }
 for int x = 0; x < cCount; x++
 {
  companies[x]->print();
 }
}
okay i managed to figure it out and get everything to compile now, think all i have left is fixing the areas where i should have used getline().
here is my updated code. it seems to crash everytime i try to enter the second name
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef Person_h
#define Person_h
#include <string>
using namespace std;

class Person
{
 public:
        Person(string n, int a);
        int getAge();
        string getName();
        void Person::increment_age();
        void print();
 private:
         string name;
         int age;
};
#endif

#include "Person.h"
#include <iostream>
#include <string>
using namespace std;

Person::Person(string n, int a)
{
name = n;
age = a;
}

int Person::getAge()
{
 return age;
}

string Person::getName()
{
 return name;
}

void Person::increment_age()
{
age += 1;
}

void Person::print()
{
cout << "name: " << name << endl;
cout << "age: " << age << endl;
}

#include <string>
#include "Person.h"
using namespace std;

class Company
{
 public:
    Company::Company(string);
    void Company::set_CEO(Person*);
    void Company::set_FOUNDER(Person*);
    void print()const;
 private:
    string cName;
    Person *CEO;
    Person *FOUNDER;
};

#include "Company.h"
#include <iostream>
using namespace std;

Company::Company(string name)
{
 cName = name;
}

void Company::set_CEO(Person* i)
{
 *CEO = *i;
}

void Company::set_FOUNDER(Person* i)
{
 *FOUNDER = *i;
}
    
void Company::print() const
{
 cout << "company name: " << cName << endl;
 cout << "CEO: ";
 CEO->print();
 cout << "FOUNDER: ";
 FOUNDER->print();
}

#include <iostream>
#include <string>
#include <vector>

#include "Person.h"
#include "Company.h"

int main()
{
 int ages[100];
 int cCount = 0;
 int i = 0;
 int j = 0;
 string names[100];
 string tempC;
 string tempCEO;
 string tempFOUNDER;
 vector<Person*> people;
 vector<Company*> companies;
 char ans = 'y';
 while (ans == 'y')
 {
  cout << "enter name of person: ";
  getline( cin,  names[i] );
  cout << "enter the age of that person: ";
  cin >> ages[i];
  cout << "enter another person(y for yes, n for no)?: ";\
  cin >> ans;
  tolower(ans);
  i++;
 }
 const int numPeople = i;
 for ( int i = 0; i < numPeople; i++)
 {
  Person *a = new Person(names[i], ages[i]);
  people.push_back(a);
 }
 ans = 'y';
 i = 0;
 while (ans == 'y')
 {
  cout << "enter a company name: ";
  cin >> tempC;
  Company *b = new Company( tempC );
  companies.push_back(b);
  cout << "who is the CEO of this company?: ";
  cin >> tempCEO;
  cout << "who is the founder of this company?: ";
  cin >> tempFOUNDER;
  for (int x = 0; x < numPeople; x++)
  {
   if ( tempCEO == people[i]->getName() )
      b->Company::set_CEO(people[i]);
  }
  for (int x = 0; x < numPeople; x++)
  {
   if ( tempFOUNDER == people[i]->getName() )
      b->Company::set_FOUNDER(people[i]);
  }
  cout << "enter another company? (y for yes n for no): ";
  cin >> ans;
  tolower(ans);
  cCount++;
 }
 for( int x = 0; x < numPeople; x++)
 {
  people[x]->increment_age();
 }
 for (int x = 0; x < cCount; x++)
 {
  companies[x]->print();
 }
}
Topic archived. No new replies allowed.