Runtime error in Class, string based program.... plz help

This is a discussion on Runtime error in Class, string based program.... plz help within the C++ Programming forums, part of the General Programming Boards category; This program compiles, but when i run it and come to the point where the string I have stored in ...

  1. #1
    Registered User PotitKing's Avatar
    Join Date
    Dec 2001
    Posts
    28

    Post 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];
    }

    }
    :)
    % gcc -v
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 3.3.3 [FreeBSD] 20031106

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    void Dog::SetName(char szNewName[50])
    {
    szName[50] = szNewName[50]; // 28
    }
    That is ugly, I am actually suprised your app doesn't crash. That sets the 51st character of szName (it only has 50, thus you are into memory thats not yours to touch) to the 51st character of szNewName. You want to do this.

    Code:
    void Dog::SetName(char szNewName[50])
    {
    strcpy(szName, szNewName);
    }
    Which will actually perform the neccesary copying.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User PotitKing's Avatar
    Join Date
    Dec 2001
    Posts
    28

    oh

    I've always wondered if there's a string-copy function, but when I looked at a list in my book, it wasen't listed. Thank you very much.... And Merry Christmas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  4. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

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