Runtime error in Class, string based program.... plz help
This program compiles, but when i run it and come to the point where the string I have stored in the szName string is printed, it displays a lot of strange char's. Why? :)
Thank you for reading this... and hopefully helping me
/* The Animal-Farm main.cpp file */
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include "dog.h"
int main(int nArg, char* pszArg[])
{
cout << "Welcome to the Animal-Farm!\n";
Dog a;
char szName[50];
cout << "What's the name of your dog? ";
cin.getline(szName, 50);
a.SetName(szName);
cout << "The name of your dog is ";
a.GetName();
cout << "\n\n";
return 0;
}
/* The Dog-class header file */
class Dog
{
public: // Constructor/Destructor prototypes
Dog();
~Dog();
// Methods
int GetAge(){ return itsAge; }
void SetAge(int theAge) { itsAge = theAge; }
void SetName(char szNewName[50]);
void GetName();
private: // Member-variables
char szName[50];
int itsAge;
};
Dog::Dog()
{
}
Dog::~Dog()
{}
void Dog::SetName(char szNewName[50])
{
szName[50] = szNewName[50]; // 28
}
void Dog::GetName()
{
int nLength = strlen(szName);
for ( int i = 0; i < nLength; i++)
{
cout << szName[i];
}
}
:)