|
-
September 19th, 2005, 07:02 AM
#1
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! :-)
-
September 19th, 2005, 07:10 AM
#2
Re: Program does not display name
Code:
strName->Copy(Name);
IFAIK this won't assign Name to strName.
- petter
-
September 19th, 2005, 07:19 AM
#3
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?
-
September 19th, 2005, 07:29 AM
#4
Re: Program does not display name
Code:
strName = strName->Copy(Name);
This works.
- petter
-
September 19th, 2005, 07:30 AM
#5
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()'
**** **** **** **** **/**
-
September 19th, 2005, 07:31 AM
#6
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 };
-
September 19th, 2005, 07:40 AM
#7
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)
-
September 19th, 2005, 08:33 AM
#8
Re: Program does not display name
Sorry but what sort of C++ is this? Doesn't this belong in the managed C++ forum?
-
September 19th, 2005, 08:35 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|