Thread: output produces funky characters

  1. #1
    Shadow12345
    Guest

    output produces funky characters

    Ok I am making a simple program that deals with inheritance. The program basically works except for when I output the names of the god and the person it puts out 50 characters with the last being the names that the user input. I am not sure what to do in order to fix this.

    code:
    -------------------------------------------------------------------------------
    #include <iostream.h>
    #include <string.h>

    class god
    {
    public:
    virtual void speak() { cout << "\nGod is speaking!\n";}

    virtual void setname(char* name) {strncpy(itsname, name, 50);}
    virtual char *getname() {return itsname;}


    protected:
    char itsname[50];

    };

    class person: public god
    {
    public:
    void speak() {cout << "\nThe person is speaking!\n";}
    };


    int main()
    {
    char name[50];
    char name1[50];

    god thor;

    person bob;

    thor.speak();
    bob.speak();


    cout << "\n";
    cout << "\nEnter a name for god ";
    cin.getline(name, 50);
    cout << "\nThe god's name is " << thor.getname();
    cout << "\n";
    cout << "\nEnter a name for the person";
    cin.getline(name1, 50);

    cout << "\nThe name of the person is " << bob.getname();


    return 0;
    }
    ------------------------------------------------------------------------------8

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    do you actually transfer the string to the class? you're inputting the two names, and printing the class names, but you never link the input to the classes. the fact that you actually see the names somewhere is just luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program compiles, runs, but produces no output
    By happyclown in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 05:43 AM
  2. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  3. Strange characters in output Need Help ASAP
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2003, 06:35 PM
  4. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM
  5. output of characters
    By deleeuw in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:17 PM