Find the ERROR in this code (C++) and fix it: (Please Highlight where the error
ID: 3722615 • Letter: F
Question
Find the ERROR in this code (C++) and fix it: (Please Highlight where the error is and what the correct code is)
Date Header:
#ifndef date_h
#define date_h
#pragma once
class Date {
public:
Date(int, int, int);
void setMonth();
void setDay();
void setYear();
int getMonth();
int getDay();
int getYear();
private:
int Month;
int Day;
int Year;
};
#endif
Date Implementation:
#include "stdafx.h"
#include "Date.h"
#include <iostream>
#include <string>
using namespace std;
Date::Date(int M, int D, int Y) {
Month = 01;
Day = 01;
Year = 1990;
}
Date::Date(int M, int D, int Y) {
Month = M;
Day = D;
Year = Y;
}
void Date::setMonth() {
int M;
cout << "Month?: ";
cin >> M;
while (M < 1 || M > 12) {
cout << "Put in a Month: ";
cin >> M;
}
Month = M;
}
void Date::setDay() {
int D;
cout << "Day?: ";
cin >> D;
while (D < 1 || D > 31) {
cout << "Put in a Day: ";
cin >> D;
}
Day = D;
}
void Date::setYear() {
int Y;
cout << "Year?: ";
cin >> Y;
while (Y < 1000 || Y > 2018) {
cout << "Put in a Year: ";
cin >> Y;
}
Year = Y;
}
int Date::getMonth() {
return Month;
}
int Date::getDay() {
return Day;
}
int Date::getYear() {
return Year;
}
Person Header:
#ifndef Person_h
#define Person_h
#include <string>
#include "Date.h"
using namespace std;
#pragma once
class Person {
public:
Person(string, string, int, int, int);
void setFirstName();
void setLastName();
void setDOB();
string getFirstName();
string getLastName();
int getMonth();
int getDay();
int getYear();
private:
string FirstName, LastName;
Date DOB ;
};
#endif
Person Implementation:
#include "stdafx.h"
#include "Person.h"
#include "Date.h"
#include <iostream>
using namespace std;
Person::Person(string f, string l, int m, int d, int y)
: FirstName(f), LastName(l), Month(m), Day(d), Year(y)
{
FirstName = f;
LastName = l;
Month = m;
Day = d;
Year = y;
}
void Person::setFirstName() {
string F;
cout << "What is your first name?: " <<endl;
cin >> F;
FirstName = F;
}
void Person::setDOB() {
DOB.setMonth();
DOB.setDay();
DOB.setYear();
}
void Person::setLastName() {
string L;
cout << "What is your last name?: ";
cin >> L;
LastName = L;
}
string Person::getFirstName() {
return FirstName;
}
string Person::getLastName() {
return LastName;
}
int Person::getMonth() {
return DOB.getMonth();
}
int Person::getDay() {
return DOB.getDay();
}
int Person::getYear() {
return DOB.getYear();
}
Main cpp:
#include "stdafx.h"
#include <iostream>
#include "Date.h"
#include "Person.h"
using namespace std;
int main()
{
Person person1; //w/ default constructor
person1.setFirstName();
person1.setLastName();
person1.setDOB();
cout << "First Name: " << person1.getFirstName() << endl
<< "Last Name: " << person1.getLastName() << endl
<< "Date of Birth: " << person1.getMonth() << "/" << person1.getDay() << "/" << person1.getYear() << endl;
string firstName, lastName;
int month, day, year;
cout << "Enter First Name: ";
cin >> firstName;
cout << "Enter Last Name: ";
cin >> lastName;
cout << "Enter Month of Birth: ";
cin >> month;
while (month < 1 || month > 12) {
cout << "Put in a Month: ";
cin >> month;
}
cout << "Enter Day of Birth: ";
cin >> day;
while (day < 1 || day > 31) {
cout << "Put in a Day: ";
cin >> day;
}
cout << "Enter Year of Birth: ";
cin >> year;
while (year < 1900 || year > 2018) {
cout << "Put in a Year";
cin >> year;
}
Person person2(firstName, lastName, month, day, year);
cout << "First Name: " << person2.getFirstName() << endl
<< "Last Name: " << person2.getLastName() << endl
<< "Date of Birth: " << person2.getMOB() << "/" << person2.getDOB() << "/" << person2.getYOB() << endl;
return 0;
}
Explanation / Answer
Default constructors and setters are not correctly initialized...
#ifndef date_h
#define date_h
// #pragma once
class Date
{
public:
Date();
Date(int, int, int);
void setMonth();
void setDay();
void setYear();
void setMonth(int);
void setDay(int);
void setYear(int);
int getMonth();
int getDay();
int getYear();
private:
int Month;
int Day;
int Year;
};
#endif
// Date Implementation :
// #include "stdafx.h"
// #include "Date.h"
#include <iostream>
#include <string>
using namespace std;
Date::Date()
{
Month = 01;
Day = 01;
Year = 1990;
}
Date::Date(int M, int D, int Y)
{
Month = M;
Day = D;
Year = Y;
}
void Date::setMonth(int m){
Month = m;
}
void Date::setYear(int y){
Year = y;
}
void Date::setDay(int d){
Day = d;
}
void Date::setMonth()
{
int M;
cout << "Month?: ";
cin >> M;
while (M < 1 || M > 12)
{
cout << "Put in a Month: ";
cin >> M;
}
Month = M;
}
void Date::setDay()
{
int D;
cout << "Day?: ";
cin >> D;
while (D < 1 || D > 31)
{
cout << "Put in a Day: ";
cin >> D;
}
Day = D;
}
void Date::setYear()
{
int Y;
cout << "Year?: ";
cin >> Y;
while (Y < 1000 || Y > 2018)
{
cout << "Put in a Year: ";
cin >> Y;
}
Year = Y;
}
int Date::getMonth()
{
return Month;
}
int Date::getDay()
{
return Day;
}
int Date::getYear()
{
return Year;
}
// Person Header :
#ifndef Person_h
#define Person_h
#include <string>
// #include "Date.h"
using namespace std;
// #pragma once
class Person
{
public:
Person();
Person(string, string, int, int, int);
void setFirstName();
void setLastName();
void setDOB();
string getFirstName();
string getLastName();
int getMonth();
int getDay();
int getYear();
private:
string FirstName, LastName;
Date DOB;
};
#endif
// Person Implementation :
// #include "stdafx.h"
// #include "Person.h"
// #include "Date.h"
#include <iostream>
using namespace std;
Person::Person() {}
Person::Person(string f, string l, int m, int d, int y)// : FirstName(f), LastName(l), Month(m), Day(d), Year(y)
{
FirstName = f;
LastName = l;
DOB.setMonth(m);
DOB.setDay(d);
DOB.setYear(y);
}
void Person::setFirstName()
{
string F;
cout << "What is your first name?: " << endl;
cin >> F;
FirstName = F;
}
void Person::setDOB()
{
DOB.setMonth();
DOB.setDay();
DOB.setYear();
}
void Person::setLastName()
{
string L;
cout << "What is your last name?: ";
cin >> L;
LastName = L;
}
string Person::getFirstName()
{
return FirstName;
}
string Person::getLastName()
{
return LastName;
}
int Person::getMonth()
{
return DOB.getMonth();
}
int Person::getDay()
{
return DOB.getDay();
}
int Person::getYear()
{
return DOB.getYear();
}
// Main cpp :
// #include "stdafx.h"
#include <iostream>
// #include "Date.h"
// #include "Person.h"
using namespace std;
int main()
{
Person person1; //w/ default constructor
person1.setFirstName();
person1.setLastName();
person1.setDOB();
cout << "First Name: " << person1.getFirstName() << endl
<< "Last Name: " << person1.getLastName() << endl
<< "Date of Birth: " << person1.getMonth() << "/" << person1.getDay() << "/" << person1.getYear() << endl;
string firstName, lastName;
int month, day, year;
cout << "Enter First Name: ";
cin >> firstName;
cout << "Enter Last Name: ";
cin >> lastName;
cout << "Enter Month of Birth: ";
cin >> month;
while (month < 1 || month > 12)
{
cout << "Put in a Month: ";
cin >> month;
}
cout << "Enter Day of Birth: ";
cin >> day;
while (day < 1 || day > 31)
{
cout << "Put in a Day: ";
cin >> day;
}
cout << "Enter Year of Birth: ";
cin >> year;
while (year < 1900 || year > 2018)
{
cout << "Put in a Year";
cin >> year;
}
Person person2(firstName, lastName, month, day, year);
cout << "First Name: " << person2.getFirstName() << endl
<< "Last Name: " << person2.getLastName() << endl
<< "Date of Birth: " << person2.getMonth() << "/" << person2.getDay() << "/" << person2.getYear() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.