954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Weird number output

Hello

My problem is I am calling a method in my main but this method doesn't have any code written in it, and yet I am getting an output of numbers approximately 10 digits long, and a negative value.

For example, in my main I have:

Date d1(9,20,2011); // Date is an object that requires a month,day,and year
Date d2(10,1,2011);

cout << d1.dayDifference(d4) << endl;


Then in my .cpp for the[B]dayDifference method[B] I have written:

int Date::dayDifference(const Date& d) const
{
    //lots of commented out code
}

My Date object works fine, I overloaded the pre-increment and post-increment operators, and those work fine too, and I have around 20 methods I wrote that use all of this, and yet thisdayDifference method, when called, outputs a long negative number. And when I wrote in the code for that method "cerr << help; ", It then outputted the word "help" along with a long positive number.

The output is the same number each time I run it, but it shouldn't be outputting anything since there is no code inside.

Thanks for your help, and I will happily clarify anything

srednakun
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Actually my fiance just explained it to me "Because you don't have a return statement. C++ just makes room for a return variable and if you don't return anything, whatever is in that place in memory (in your case a very long negative number) will be returned."

srednakun
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

dayDifference() returns an int value in all cases. Since you didn't specify what that value is, it's garbage.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I thought that it may be a garbage value because we have arguments in the juncton ,the memory has allocated for those variables and has also created memory for the function. the output may bet that memory reference

It may or may not correct I told with my guess

pavangajula2007
Newbie Poster
6 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: