Random number in output of my C++ file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yoshi445511
    New Member
    • Dec 2009
    • 1

    Random number in output of my C++ file

    just trying to get used to classes.
    Whenever i run the program, it works fine, except for a random number that pops up.

    source:
    Code:
    #include <iostream>
    using namespace std;
    
    class CRectangle
    	{
    	public:
    	int x, y;
    	int area()
    		{
    		int a = x*y;
    		cout << a << "\n";
    		}
    	};
    int main()
    {
    	CRectangle area;
    	cout << "What is the length: ";
    	cin >> area.x;
    	cout << "What is the width: ";
    	cin >> area.y;
    	cout << area.area() << "\n";
    	return 0;
    }
    output:
    Code:
    What is the length: 9
    What is the width: 97256
    875304
    134521056
    any ideas?
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Your function area doesn't return the value, so some random number is inserted by the compiler and printed by cout << area.area() << "\n";

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Missing that return should have caused a compile time error. Did you not get one?

      Comment

      Working...