Thread: unexpected program crash

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    unexpected program crash

    im working on a simple program.. but it constantly crashes. i have absolutely no idea why. ill start the program.. write my name.. press enter. and it gives me the windows error "send bug report" thing.. could somebody take a look and try to explain why?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Character{
          public:
    Character(){
                Health = 100;
                Level = 1;
    };
    ~Character(){};
    string getName(){
        return name;
    };
    string getClass(){
        return Class;
    };
    int getAttack(){
        return Attack;
    };
    int getDefense(){
        return Defense;
    };
    int getLevel(){
        return Level;
    };
    int getHealth(){
        return Health;
    };
    string setName(string n){
           n = name;
    };
    protected:
              string name;
              string Class;
              int Attack;
              int Defense;
              int Level;
              int Health;
    };
    
    class Monster: public Character{
          };
    
    int main(){
    Character Player;
    Character * pPlayer = &Player;
    string dName;
    cout<<"player name\n";
    cin>>dName;
    pPlayer->setName(dName);
    cin.get();
    cout<<"name is"<<Player.getName()<<"\n";
    cout<<"player Class\n";
    cin.get();
    };
    im using dev-cpp if its of any concelation
    Last edited by lilhawk2892; 12-23-2007 at 11:16 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to properly indent your code. Anyway, the first thing I see that is wrong is that
    Code:
    #include <strings.h>
    should be:
    Code:
    #include <string>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    sorry about the indentation.. i usually let the compiler handle the indentation.. thanks for the strings.h comment.. ive fixed it. but the problem persisits.. seems to happen everytime i use pointers..

    probably should mention im using dev-cpp

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, the next problem that I noticed is this:
    Code:
    string setName(string n){
        n = name;
    }
    You are assigning name, which was not given a value in the constructor, to the local variable n. You probably want to do the opposite:
    Code:
    string setName(string n){
        name = n;
    }
    but the problem persisits.. seems to happen everytime i use pointers..
    Dev-C++ 4.9.9.2 uses the MinGW port of g++ 3.4.2. I compiled with the MinGW port of g++ 3.4.5 and tested on Windows XP, but could not duplicate such a problem. Your code looks okay where pointers are concerned.

    One more thing: if you plan to use Character as a base class (and it appears that you do have such an intention), declare its destructor as virtual.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    thanks for your input.. ill try this on a few other compilers and see how it works.. i just got through my tutorial on polymorphism. so im not too knowledgeable on virtual functions. but i know what they do. just not always the best time to implement them.

    ive also edited the set name function

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by lilhawk2892 View Post
    sorry about the indentation.. i usually let the compiler handle the indentation..
    Huh? What does that even mean? The compiler doesn't care whatsoever about indentation. The ONLY reason for indentation is human readers.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    my IDE... sorry. when i say compiler im usually referring to the entire development enviroment

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't expect the IDE to do your work for you. It's your task to make the code readable. So go ahead an indent one time in each block (eg, inside main), and you'll see it becomes better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    Another problem is with your setName member function.

    You are declaring that it returns a string but you dont return anything. Im guessing that this is a mistake and that you intend to declare it returns void.

    Code:
    void setName(string n) 
    {
        name = n ;
    }
    
    // or if you did want it to return the string then use
    
    string setName(string n)
    {
        name = n ;
        return name ;
    }
    Also remember to return 0 in your int main( ) function at the end.

    Apart from that it seems to be working fine.

    Hope this helps

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    those lasts tips helped my program is now running fine. and i know how to fix it the next time

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    as far as indents are concerned, just turn on teh option of converting tabs to spaces, that wha when you copy and paste you dont lose the indents.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's not correct. Tabbed code pastes fine in the board, so long as you use code tags.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Normally, I would blame skewed indentation on Dev-C++'s default indentation setting (the "smart tabs" feature is not as smart as it should be, so it should be turned off, but is on by default), though in lilhawk2892's example code the indentation was simply lacking, not skewed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    smart tabs does a good job of almost working right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program causes my compiler to crash
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 04:06 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  4. Filling a 2d Array cause program to crash
    By Geo-Fry in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2003, 07:00 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM