wrong output.

This is a discussion on wrong output. within the C++ Programming forums, part of the General Programming Boards category; Sorry to keep posting stuff. i have written this tiny code from a book, pretty useless code but im just ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    wrong output.

    Sorry to keep posting stuff.

    i have written this tiny code from a book, pretty useless code but im just learning pointers.

    it compiles and runs perfect, but its not giving me the right output but i dont understand why.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class SimpleCat
    {
    public:
    	SimpleCat() { int ItsAge = 2; }
    	~SimpleCat() {}
    	int GetAge() const { return ItsAge; }
    	void SetAge(int age) { ItsAge = age; }
    private:
    	int ItsAge;
    };
    
    int main()
    {
    	SimpleCat * Frisky = new SimpleCat;
    	cout <<"Frisky is " << Frisky->GetAge() << " years old.\n";
    
    	Frisky->SetAge(5);
    	cout <<"Frisky is " << Frisky->GetAge() << " years old.\n";
    	delete Frisky;
    	system("PAUSE");
    	return 0;
    }
    the result of the program is...

    Code:
    Frisky is 3342760 years old.
    Frisky is 5 years old.
    Press any key to continue...
    can someone just take a quick look and try to see what the problem is because i cannot find it to save my life.

    cheers in advance. Reece.

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    My guess is you should change

    Code:
    SimpleCat() { int ItsAge = 2; }
    to

    Code:
    SimpleCat() { ItsAge = 2; }
    Not exactly sure if that's what you want. My output becomes

    Code:
    Frisky is 2 years old.
    Frisky is 5 years old.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Code:
    SimpleCat() { int ItsAge = 2; }
    Change that to:
    Code:
    SimpleCat() { ItsAge = 2; }
    If not, you have a local variable named ItsAge that hides the member variable that you intend to assign 2 to. You might also use a constructor initialisation list, e.g.,
    Code:
    SimpleCat() : ItsAge(2) {}
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    oh my god ive been trying to fix it for about an hour lmao.

    that was it!

    cheers mate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 07:55 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 10:00 PM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 06:58 PM
  4. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 12:58 PM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM

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