Hi basically my problem is that I am getting what I believe to be is the address of the object but I want to get the value that is stored in the object. Below is the code I wrote. I've included Date.cpp and my main function file. Say if I typed in 5/26/1988
I'm getting 1231231313/123123123123/-2 or something like that. What is going wrong?

//Main file
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 default constructor to set user input values to the Date class

	Date(setDay, setMonth, 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);
	}

	//Sends values to the default constructor in the Actor class

	Actor(setHeight, setWeight, setTendency);

	characterAttributes.getCharacteristics();
}


//Date.cpp





  Date::Date(){
        test = false;
    }

    //constructor initializes variables to user input

    Date::Date(int initialDay, int initialMonth, int initialYear){
        day = initialDay;
        month = initialMonth;
        year = initialYear;
    }

    //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

    bool Date::validityDay(int testDay){
        if(testDay > 0 && testDay < 31)
            test = true;
        else
            test = false;
        return test;
    }

    //Mutator method that checks validity of the month

    bool Date::validityMonth(int testMonth){
        if(testMonth > 0 && testMonth < 13)
            test = true;
        else
            test = false;
        return test;
    }

    //Collects day, month, year of birth and formats them for the appropriate display

    string Date::getCharacteristics(){

        day = getDay();
        month = getMonth();
        year = getYear();
        string date;
        string brackets = "/";

        //Takes the first integer and converts it into a string
        //Creating a new variable and assigning it to out.str()

        std::string dayString;
        std::stringstream out;
        out << day;
        dayString = out.str();

        std::string monthString;
        std::stringstream out2;
        out2 << month;
        monthString = out2.str();

        std::string yearString;
        std::stringstream out3;
        out3 << year;
        yearString = out3.str();

        //After converting the integers to strings
        //concantenation is necessary to attach the strings to create
        //a string that looks something like this DD/MM/YYYY

        date = dayString + brackets + monthString + brackets + yearString;
        return date;
    }

Recommended Answers

All 7 Replies

Ok... the problem here is that you are confusing the idea of a class and an object. A class is a type that you define. In a class, you declare what data an object of that class holds and what functions (methods) can be used on that data. The object is an instance of that class in memory (with the data declared in the class). You should read on the subject to get a better grasp of it because it is central to object-oriented programming.

The problem that you are having in this example code is that when you create an object, called individualBirth, with a default constructor (that is implicitly provided by the compiler), the data in it is empty (or, in your case, the debug compiler you have seems to fill that memory with 123123123.. values to help you notice that the memory is not initialize by your code). Since none of the subsequent calls to validate the day and month actually set the data members of object "individualBirth", it never gets initialized at all.

When you call "Date(setDay,setMonth,setYear);", what happens is that a new object of class Date with no name is created with the initial values given, but it dies out right after the semi-colon of the same line (since there is no identifier for it). So that line has no effect at all (and if your compiler is clever, it should warn you about that... warnings are very important!).

Now, here is a simple example that illustrates what I mean, that you can run and play with:

#include <iostream>

class Foo {
  private:
    int Bar;
  public:
    Foo() { //Default constructor.
      Bar = 42;
    };
    Foo(int SomeBar) { //Parametrized constructor.
      if(SomeBar < 100)
        Bar = SomeBar;
      else
        Bar = 42;
    };
    int getBar() { return Bar; };
    bool setBar(int SomeBar) { 
      if(SomeBar < 100)
        Bar = SomeBar;
      else
        return false;
      return true;
    };
    bool validateBar(int SomeBar) { return SomeBar < 100; };
    //consider the idea of a static method if you don't modify the object:
    static bool staticValidateBar(int SomeBar) { return SomeBar < 100; };
};

using namespace std;

int main() {
  Foo obj1; //creates an uninitialized object (Bar == 42).

  cout << "Foo.Bar is now: " << obj1.getBar() << endl;
  //validateBar can be called on obj1 but will not modify it.
  if(!obj1.validateBar(120))
    cout << "120 is not a valid Bar!" << endl;
  
  //this is better done with a static method:
  if(!Foo::staticValidateBar(130))
    cout << "130 is not a valid Bar either!" << endl;

  //now, if you do Foo(30), it will not modify obj1:
  Foo(69);
  cout << "obj1.Bar is still " << obj1.getBar() << ".. damn it!" << endl;

  //calling setBar will modify obj1.Bar:
  obj1.setBar(17);
  cout << "Finally, obj1.Bar was changed to " << obj1.getBar() << endl;

  //you can also use the set function as a test:
  if(!obj1.setBar(190))
    cout << "190 was not valid, so obj1.Bar is still at " << obj1.getBar() << endl;
  if(obj1.setBar(20))
    cout << "20 is valid, so now obj1.Bar was changed to " << obj1.getBar() << endl;

  return 0;
};

it would help if i had your full source code with class declarations and headers. I can't compile or debug it as is. I'll look at it though...

Nevermind... Mike_2000 has you covered

I see other people have posted already but I am going to show what I did as well.

In the code I just put the Date class and anything to do with Date in because the Actor class was not fully shown in the code you posted.

I cleaned it up by throwing in do{}while() loops and took out variables that were being used but did not have to be there. Used 1 stringstream instead of 3 which I hope you can look at and understand how it works (it is like cin and cout).

Just read over the code and figure out what is going on then add in your Actor class either in a similar way or you can use your old code.

I see Mike posted his Foo Bar example I'm sure it teaches lots but I find examples that include what I am doing to be of more use.

#include <iostream>
#include <sstream>

using namespace std;

class Date
{
    int day, month, year;

    public:

    Date(){};
    
    bool validityDay(int);
    bool validityMonth(int);
    bool validityYear(int);

    string getCharacteristics();
};

bool Date::validityDay(int testDay)
{
    if(testDay >= 1 && testDay <= 31) //can be 31 days in a month
    {
        day = testDay; //set the day because it is valid
        return true;
    }
    else
        return false;
}

bool Date::validityMonth(int testMonth)
{
    if(testMonth >= 1 && testMonth <= 12)
    {
        month = testMonth; //set the month because it is valid
        return true;
    }
    else
        return false;
}

bool Date::validityYear(int testYear)
{
    if(testYear >= 0 )
    {
        year = testYear; //set the year because it is valid
        return true;
    }
    else
        return false;
}

string Date::getCharacteristics()
{
    stringstream out;
    string dateString;
    //note that there are no get() functions being used because the class can use it's own variables within itself
    out << day << "/" << month << "/" << year; //input all the data into the stream (fyi '/' is a slash not a bracket -- this is in brackets)
    out >> dateString; //take this string out of the stream

    return dateString;
}

int main()
{
    Date individualBirth;

    int setDay = 0, setMonth = 0, setYear = 0;

    do //do whiles do not check when running into them but will keep you looping if the while condition is true
    {
        cout << "On what day was your individual born?(DD)" << endl;
        cin >> setDay;
    }while(!individualBirth.validityDay(setDay)); //checks and sets day

    do
    {
        cout << "On what month was your individual born?(MM)" << endl;
        cin >> setMonth;
    }while(!individualBirth.validityMonth(setMonth)); //checks and sets month

    do
    {
        cout << "On what year was your individual born?(YYYY)" << endl;
        cin >> setYear;
    }while(!individualBirth.validityYear(setYear)); //checks and sets year

    //do not need to call the constructor again since the validity() functions set values
    
    //I find it a bad idea to call the constructor of an object more than once because all data is lost
    //and you might find yourself wondering why numbers are 1231231313 because they are not assigned.
    cout << individualBirth.getCharacteristics() << endl;

    return 0;
}

Thanks alot for the help sfuo and mike_2000. Classes I find have been the most difficult concept for me to understand. I was able to display the right information after the help but I still don't understand why my code output was random when I thought I set the variables using Date(setDay, setMonth, setYear). When I highlighted that command it pointed to the method in the date class I wanted to use so I thought I was doing it right.

For that to work you would need to put individualBirth = Date(setDay, setMonth, setYear); Like Mike said just putting Date(setDay, setMonth, setYear); makes this class instance but it gets deleted after the semi-colon because it is not getting assigned/stored to anything.

If you make your class instance individualBirth equal to Date(setDay, setMonth, setYear) then the constructor that you just made will be copied to individualBirth because they are of the same data-type.

If you wrote 5 that does nothing but if you went int x = 5 that assigns 5 to the variable x. Writing Date(setDay, setMonth, setYear) is like the 5 in the example, on it's own it does nothing but it is able to be assigned to a variable and used later.

Great clarification sfuo. I think i'm actually starting to understand this whole object-oriented programming. This site is great!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.