Ive pasted the code for actor.cpp, date.cpp and the main function. This is alot of stuff I pasted but I believe it is necessary to show my problem. At first I get the date of the individuals birth and that gets displayed and everything turns out as it should. When i try to get that date back again in the Actor class by the code inception = dateInception.getCharacteristics();
I get a result that is like 123441234/1234234234/1234234. So according to my little understanding of classes I created an object on the stack in my main. Then because the code stopped using the object, the object was automatically dumped. So when i call dateInception.getCharacteristics(); for the Date class i get a number that has day, month and year set to random values. How do I solve this problem, I tried using pointers but that wasn't working for me.
Actor.cpp
#include "Actor.h"
#include "Point3D.h"
#include "Date.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
Actor::Actor(){
inception = dateInception.getCharacteristics();
//death = dateDeath.getCharacteristics();
location = location3D.distanceFrom();
test = false;
name = "unknown";
maxSpeed = 0;
height = 0;
weight = 0;
tendency = 0;
}
//returns name of type string
string Actor::getName() const{
return name;
}
//returns maxSpeed of type double
double Actor::getMaxSpeed() const{
return maxSpeed;
}
//returns weight of type integer
int Actor::getWeight() const{
return weight;
}
//returns height of type integer
int Actor::getHeight() const{
return height;
}
//returns tendency of type double
double Actor::getTendency() const{
return tendency;
}
//returns dateInception of type Date
string Actor::getInception() const{
return inception;
}
//returns dateDeath of type Date
string Actor::getDeath() const{
return death;
}
//returns location of type Point3D
double Actor::getLocation() const{
return location;
}
//tests and sets the value of the individual's tendency
bool Actor::validityTendency(int testTendency){
if(testTendency == -1 || testTendency == 1){
tendency = testTendency;
test = true;
}
else
test = false;
return test;
}
//tests and sets the value of the individual's height
bool Actor::validityHeight(int testHeight){
if(testHeight <= 1000 && testHeight >= 0){
height = testHeight;
test = true;
}
else
test = false;
return test;
}
//test and sets the value of the individual's weight
bool Actor::validityWeight(int testWeight){
if(testWeight <= 1000 && testWeight >= 0){
weight = testWeight;
test = true;
}
else
test = false;
return test;
}
string Actor::getCharacteristics(){
//Uses get function to return the values of Actor members
tendency = getTendency();
height = getHeight();
weight = getWeight();
maxSpeed = getMaxSpeed();
name = getName();
//Opens a stringstream and allows integers to be displayed as a string
stringstream out;
string dateString;
//stores integers and strings into a string out stream
out << "The individual's name is " << name << ". " << name << " is an " << tendency << " character."
<< endl << name << " is " << height << " feet tall and weighs " << weight << " lbs. " << endl
<< name << " is located " << location << " feet from the origin. " << endl
<< name << " was born on " << inception << " and died on " << death << ". " << endl;
//displays the string using the command out instead of cout
out >> dateString;
//ensures the method returns the full string
dateString = out.str();
return dateString;
}
Date.cpp
#include "Actor.h"
#include "Date.h"
#include <iostream>
#include <sstream>
using namespace std;
//constructor that does nothing
Date::Date(){
test = false;
}
//returns day of birth
int Date::getDay() const{
return day;
}
//returns month of birth
int Date::getMonth() const{
return month;
}
//returns year of birth
int Date::getYear() const{
return year;
}
//Mutator method that checks validity of the day and sets day
bool Date::validityDay(int testDay){
if(testDay > 0 && testDay < 31){
day = testDay;
test = true;
}
else
test = false;
return test;
}
//Mutator method that checks validity of the month and sets month
bool Date::validityMonth(int testMonth){
if(testMonth > 0 && testMonth < 13){
month = testMonth;
test = true;
}
else
test = false;
return test;
}
//Mutator method checks validity of the year and sets year
bool Date::validityYear(int testYear){
if(testYear > 0 && testYear < 100000){
year = testYear;
test = true;
}
else
test = false;
return test;
}
//Collects day, month, year of birth and formats them for the appropriate display
string Date::getCharacteristics(){
string brackets = "/";
//Opens a stringstream and allows integerss to be displayed as a string
stringstream out;
string dateString;
//stores integers and strings into a string out stream
out << month << brackets << day << brackets << year;
//displays the string using the command out instead of cout
out >> dateString;
return dateString;
}
Main function
#include "Actor.h"
#include "Point3D.h"
#include "Date.h"
int main(){
//Value that returns true or false in the form of 0 or 1
bool test;
//Sets up a constructor and Date object to use in the Date class
Date individualBirth;
//creates an object to be implemented in the Actor class
Actor characterAttributes;
//Queries the user for the day of the month the individual was born
cout << "On what day was your individual born?(DD)" << endl;
int setDay;
cin >> setDay;
test = individualBirth.validityDay(setDay);
while(test == 0){
cout << "On what day was your individual born?(DD)" << endl;
cin >> setDay;
test = individualBirth.validityDay(setDay);
}
//Queries the user for the month the individual was born on
cout << "On what month was your individual born?(MM)" << endl;
int setMonth;
cin >> setMonth;
//calls mutator function and returns a boolean value
test = individualBirth.validityMonth(setMonth);
//Makes sure user enters a legitimate month within 1 to 12
while(test == 0){
cout << "On what month was your individual born?(MM)" << endl;
cin >> setMonth;
test = individualBirth.validityMonth(setMonth);
}
//Queries the user for the year the individual was born on
//Any year is possible!
cout << "What year was your individual born?(YYYY)" << endl;
int setYear;
cin >> setYear;
//calls mutator function and returns a boolean value
test = individualBirth.validityYear(setYear);
//Makes sure user enters a legitimate month within 0 to 100000 (arbitrary value)
while(test == 0){
cout << "What year was your individual born?(YYYY)" << endl;
cin >> setYear;
test = individualBirth.validityYear(setYear);
}
//Displays date of birth of the individual in correct format
cout << individualBirth.getCharacteristics() << endl;
//Queries user for the temperament of the individual
cout << "Enter 1 if the individual is good or -1 if the individual is bad:" << endl;
int setTendency;
cin >> setTendency;
test = characterAttributes.validityTendency(setTendency);
//Makes sure user typed in a 1 or a -1 for the user's temperament
while(test == 0){
cout << "Please enter the character's temperament by typing 1 or -1 in uppercase then press enter" << endl;
cin >> setTendency;
test = characterAttributes.validityTendency(setTendency);
}
//Queries user for the height of the individual
cout << "What is the height of the individual in feet?" << endl;
int setHeight;
cin >> setHeight;
test = characterAttributes.validityHeight(setHeight);
//Makes sure user typed in an integer between 0 and 1000
while(test == 0){
cout << "What is the height of the individual in feet?" << endl;
cin >> setHeight;
test = characterAttributes.validityHeight(setHeight);
}
//Queries user for the weight of the individual
cout << "What is the weight of the individual in pounds?" << endl;
int setWeight;
cin >> setWeight;
test = characterAttributes.validityWeight(setWeight);
//Makes sure user typed in an integer between 0 and 1000
while(test == 0){
cout << "What is the weight of the individual in pounds?" << endl;
cin >> setWeight;
test = characterAttributes.validityWeight(setWeight);
}
cout << characterAttributes.getCharacteristics();
return 0;
}