Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have 5 C++ files (2 headers, 2 cpp files, and 1 main file). Everything works f

ID: 3825630 • Letter: I

Question

I have 5 C++ files (2 headers, 2 cpp files, and 1 main file). Everything works fine expect for the display() function. The program runs and takes input but will only display the text and not the values from the get functions and the phone number just prints a random set of numbers. What am I doing wrong? Please.

main.cpp

1 #include <iostream>

2 #include "Student.h"

3 #include "Phone.h"

4

5 using namespace std;

6

7 int main()

8 {

9 Student s;

10 string fn;

11 string ln;

12 string mi;

13 string dob;

14 string gender;

15 string id;

16

17 Phone p;

18 int area;

19 int exchange;

20 int line;

21 const int texts = 10;

22

23 s.setFirstName(fn);

24 s.setLastName(ln);

25 s.setMiddleInitial(mi);

26 s.setDateOfBirth(dob);

27 s.setGender(gender);

28 s.setStudentId(id);

29

30 p.setArea(area);

31 p.setExchange(exchange);

32 p.setLine(line);

33

34 s.display();

35 cout <<"first name is " << s.getFirstName() << endl;

36 cout <<"last name is " << s.getLastName() << endl;

37

38 p.display();

39 cout << "Phone number is " << p.getArea() << "-" << p.getExchange() << "-" << p.getLine() << endl;

40 }

Student.cpp

  1 #include <iostream>

2 #include "Student.h"

3 using namespace std;

4

5 Student::Student(){

6 firstName = " ";

7 lastName = " ";

8 middleInitial = " ";

9 dateOfBirth = " ";

10 gender = " ";

11 studentId = " ";

12 }

13

14 void Student::setFirstName(string fn){

15 firstName = fn;

16 }

17

18 void Student::setLastName(string ln){

19 lastName = ln;

20 }

21

22 void Student::setMiddleInitial(string mi){

23 middleInitial = mi;

24 }

25

26 void Student::setDateOfBirth(string dob){

27 dateOfBirth = dob;

28 }

29

30 void Student::setGender(string gender){

31 gender = gender;

32 }

33

34 void Student::setStudentId(string id){

35 studentId = id;

36 }

37

38 string Student::getFirstName(){

39 return firstName;

40 }

41

42 string Student::getLastName(){

43 return lastName;

44 }

45

46 string Student::getMiddleInitial(){

47 return middleInitial;

48 }

49

50 string Student::getDateOfBirth(){

51 return dateOfBirth;

52 }

53

54 string Student::getGender(){

55 return gender;

56 }

57

58 string Student::getStudentId(){

59 return studentId;

60 }

61

62 void Student::display(){

63

64 string fn;

65 string ln;

66

67 cout << "First: " << endl;

68 cin >> fn;

69 cout << "last: " << endl;

70 cin >> ln;

71 }

Student.h

1 #ifndef STUDENT_H

2 #define STUDENT_H

3

4

5 #include <string>

6 using namespace std;

7

8 class Student {

9 public:

10 Student();

11 Student(string studentId, string firstName, string lastName, string middleInitial, string dateOfBirth, string gender);

12 void setFirstName(string);

13 void setLastName(string);

14 void setMiddleInitial(string);

15 void setDateOfBirth(string);

16 void setGender(string);

17 void setStudentId(string);

18 void display();

19

20 std::string getFirstName();

21 std::string getLastName();

22 std::string getMiddleInitial();

23 std::string getDateOfBirth();

24 std::string getGender();

25 std::string getStudentId();

26

27 private:

28 std::string studentId;

29 std::string firstName;

30 std::string lastName;

31 std::string middleInitial;

32 std::string dateOfBirth;

33 std::string gender;

34 };

35 #endif

Phone.cpp

1 #include <iostream>

2 #include "Phone.h"

3

4 using namespace std;

5

6 Phone::Phone(){

7 int area = 0;

8 int exchange = 0;

9 int line = 0;

10 const int texts = 10;

11 }

12

13 void Phone::setArea(int area){

14 area = area;

15 }

16

17 void Phone::setExchange(int exchange){

18 exchange = exchange;

19 }

20

21 void Phone::setLine(int line){

22 line = line;

23 }

24

25 int Phone::getArea(){

26 return area;

27 }

28

29 int Phone::getExchange(){

30 return exchange;

31 }

32

33 int Phone::getLine(){

34 return line;

35 }

36

37 void Phone::display(){

38

39 int area;

40 int exchange;

41 int line;

42

43 cout << "Area code: " << endl;

44 cin >> area;

45

46 cout << "Exchange: " << endl;

47 cin >> exchange;

48

49 cout << "Line: " << endl;

50 cin >> line;

51 }

Phone.h

1 #ifndef PHONE_H

2 #define PHONE_H

3

4 #include <string>

5 using namespace std;

6

7 class Phone {

8 private:

9 int area;

10 int exchange;

11 int line;

12 const int texts = 10;

13

14 public:

15 Phone();

16 Phone(int area, int exchange, int line, const int texts);

17 int getArea();

18 int getExchange();

19 int getLine();

20 void setArea(int);

21 void setExchange(int);

22 void setLine(int);

23 void display();

24 };

25 #endif

Sample Output

First:

Bob

last:

Ross

first name is

last name is

Area code:

555

Exchange:

444

Line:

1234

Phone number is -1634512600-32712-882079472

Explanation / Answer

There is an issue with the scope. I have the changed the code and it is perfectly working. Please copy paste the below code.

main.cpp

#include <iostream>
#include "student.h"
#include "phone.h"

using namespace std;

int main()
{
Student s;
std::string fn;
std::string ln;
string mi;
string dob;
string gender;
string id;

Phone p;
int area;
int exchange;
int line;
  

s.display();
  
cout <<"first name is " << s.getFirstName() << endl;
cout <<"last name is " << s.getLastName() << endl;

p.display();
cout << "Phone number is " << p.getArea() << "-" << p.getExchange() << "-" << p.getLine() << endl;
}

student.h

#ifndef STUDENT_H
#define STUDENT_H


#include <string>


class Student {
public:
Student();
void display();
void setFirstName(std::string fn);
void setLastName(std::string ln);
void setMiddleInitial(std::string mi);
void setDateOfBirth(std::string dob);
void setGender(std::string gender);
void setStudentId(std::string id);

std::string getFirstName();
std::string getLastName();
std::string getMiddleInitial();
std::string getDateOfBirth();
std::string getGender();
std::string getStudentId();

private:
std::string studentId;
std::string firstName;
std::string lastName;
std::string middleInitial;
std::string dateOfBirth;
std::string gender;
};
#endif

student.cpp

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

Student::Student(){

}

void Student::setFirstName(string fn){
  
firstName = fn;
}

void Student::setLastName(string ln){
lastName = ln;
}

void Student::setMiddleInitial(string mi){
middleInitial = mi;
}

void Student::setDateOfBirth(string dob){
dateOfBirth = dob;
}

void Student::setGender(string gender){
gender = gender;
}

void Student::setStudentId(string id){
studentId = id;
}

string Student::getFirstName(){
return firstName;
}

string Student::getLastName(){
return lastName;
}

string Student::getMiddleInitial(){
return middleInitial;
}

string Student::getDateOfBirth(){
return dateOfBirth;
}

string Student::getGender(){
return gender;
}

string Student::getStudentId(){
return studentId;
}

void Student::display(){

string fn;
string ln;

cout << "First: " << endl;
cin >> fn;
setFirstName(fn);
cout << "last: " << endl;
cin >> ln;
setLastName(ln);
}

phone.h

#ifndef PHONE_H
#define PHONE_H

#include <string>


class Phone {
private:
int area;
int exchange;
int line;
  
  

public:
Phone();
Phone(int area, int exchange, int line);
int getArea();
int getExchange();
int getLine();
void setArea(int);
void setExchange(int);
void setLine(int);
void display();
};
#endif

phone.cpp

#include <iostream>
#include "phone.h"

using namespace std;

Phone::Phone(){

}

void Phone::setArea(int area){
area = area;
}

void Phone::setExchange(int exchange){
exchange = exchange;
}

void Phone::setLine(int line){
line = line;
}

int Phone::getArea(){
return area;
}

int Phone::getExchange(){
return exchange;
}

int Phone::getLine(){
return line;
}

void Phone::display(){

int area;
int exchange;
int line;

cout << "Area code: " << endl;
cin >> area;
setArea(area);

cout << "Exchange: " << endl;
cin >> exchange;
setExchange(exchange);

cout << "Line: " << endl;
cin >> line;
setLine(line);
}