Strange output for class

This is a discussion on Strange output for class within the C++ Programming forums, part of the General Programming Boards category; Hi this code works and compiles without errors, the outptut is correct, but under the date, I am getting a ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Strange output for class

    Hi this code works and compiles without errors, the outptut is correct, but
    under the date, I am getting a weird set of numbers. I think it has somthing
    to do with the displayDate() function, but I am not sure. Do you know what is causing it?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    class Date
    {
    public:
       Date( int tempDay, int tempMonth, int tempYear )
       {
     		// check that number in constructor is not below zero
          if ( tempDay < 0 )
          {
             tempDay = 0;
          }
          
          day = tempDay;
          
          if ( tempMonth < 0 )
          {
             tempMonth = 0;
          }
          
          month = tempMonth;
          
          if ( tempYear < 0 )
          {
             tempYear = 0;
          }
          
          year = tempYear;
       }
       
       int setDay ( int d )
       {
          day = d;
       }
       
       int setMonth ( int m )
       {
          month = m;
       }
       
       int setYear ( int y )
       {
          year = y;
       }
       
       int getDay()
       {
          return day;
       }
       
       int getMonth()
       {
          return month;
       }
       
       int getYear()
       {
          return year;
       }
       
       int displayDate ( void )
       {
          cout << "\nDATE: " << day << " / " << month << " / " << year << endl;
       }
    		 
    private:
       int day;
       int month;
       int year;
    };
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
       Date d ( 0, 0, 0 );
       
       // set values of members and display to screen
       d.setDay(5);
       cout << "Day is: " << d.getDay() << endl;
       d.setMonth(11);
       cout << "Month is: " << d.getMonth() << endl;
       d.setYear(2006);
       cout << "Year is: " << d.getYear() << endl;
       
       // display date in correct fromat
       cout << d.displayDate();
       
       cin.get();  // freeze console window
        
       return 0;   // indicate program ended sucsessfuly
    }
    Output I get:

    Day is: 5
    Month is: 11
    Year is: 2006

    Year is: 5 / 11 / 2006
    457000 // this number should not be here!!

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Exact console output please. Your dispplaydate and what you said it printed don't work

    Code:
    cout << "\nDATE: " << day << " / " << month << " / " << year << endl;
    
    Year is: 5 / 11 / 2006 vs.
    DATE is: 5 / 11 / 2006

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Lol sorry I not checking the output:

    This is the correct one:

    Day is: 5
    Month is: 11
    Year is: 2006

    DATE: 5 / 11 / 2006
    4473856

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    int displayDate ( void )
       {
          cout << "\nDATE: " << day << " / " << month << " / " << year << endl;
       }
    This function returns an int. As you don't state what int it should return, I guess it just returns a random value.

    Code:
    cout << d.displayDate();
    This line displays the undefined return value of displayDate.

    Solution: displayDate, and all your setter functions should return void. Whole date should be displayed using d.displayDate().

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Thanks anon, I fixed it. I thought it was somthing simple. Many thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 03:56 AM
  2. Binary Search - Strange Output
    By mike_g in forum C Programming
    Replies: 13
    Last Post: 06-16-2007, 02:55 PM
  3. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21