CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2004
    Location
    UK
    Posts
    31

    Lightbulb Program does not display name

    Hi ya

    Could you help me with this program? The problem is when I run the program and the name of the animal is not displayed.

    Code:
    #include "stdafx.h"
    #include "string.h"
    #include "conio.h"
    
    
    #using <mscorlib.dll>
    
    
    using namespace System;
    
    __gc class animal
    {
    public:
    	int legs;
    	
    	void SetName(String *Name)
    { strName->Copy(Name); };
    
    	String* GetName() { return strName; };
    
    private:
    	String *strName;
    };
    
    int _tmain()
    {
        animal *Cat, *Dog;
    	
    	Cat = new animal;
    	Dog = new animal;
    
    	Cat->SetName("Cat");
    	Dog->SetName("Dog");
    	Cat->legs = 4;
    	Dog->legs = 4;
    
    	Console::WriteLine("Animal 1");
    	Console::Write("Name: ");
    	Console::WriteLine(Cat->GetName());
    	Console::Write("Legs: ");
    	Console::WriteLine(Cat->legs);
    	Console::WriteLine();
    	
    	Console::WriteLine("Animal 2");
    	Console::Write("Name: ");
    	Console::WriteLine(Dog->GetName());
    	Console::Write("Legs: ");
    	Console::WriteLine(Dog->legs);
    	Console::WriteLine();
    	
    	Console::Write("Press any key to continue...");
    
    	getch();
    
    	return 0;
    }
    Thanks! :-)

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Program does not display name

    Code:
    strName->Copy(Name);
    IFAIK this won't assign Name to strName.

    - petter

  3. #3
    Join Date
    Jul 2004
    Location
    UK
    Posts
    31

    Re: Program does not display name

    That's funny because this code snippet is from Microsoft's step by step book

    Any ideas how can the code be fixed?

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Program does not display name

    Code:
    strName = strName->Copy(Name);
    This works.

    - petter

  5. #5
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Re: Program does not display name

    Its a guess, but maybe
    Console::WriteLine should get other type than 'String'
    which is the type returned by 'Cat->GetName()'
    **** **** **** **** **/**

  6. #6
    Join Date
    May 2004
    Posts
    81

    Re: Program does not display name

    Instead of the following
    =================
    void SetName(String *Name)
    { strName->Copy(Name); };

    Try like this
    ========
    void SetName(String *Name)
    { *strName = *Name };

  7. #7
    Join Date
    Jul 2004
    Location
    UK
    Posts
    31

    Thumbs up Re: Program does not display name

    Thanks wildfrog now the code works :-)

    sudhakarm the code you wrote doesn't work, unfortunately.

    Thank y'all for your answers 8)

  8. #8
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Program does not display name

    Sorry but what sort of C++ is this? Doesn't this belong in the managed C++ forum?

  9. #9
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Program does not display name

    Doesn't this belong in the managed C++ forum?
    Yes, it does...

    - petter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured