unexpected program crash

This is a discussion on unexpected program crash within the C++ Programming forums, part of the General Programming Boards category; im working on a simple program.. but it constantly crashes. i have absolutely no idea why. ill start the program.. ...

  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
    22,274
    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>
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,274
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    Posts
    23,014
    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,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    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
    22,274
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,189
    smart tabs does a good job of almost working right
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

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, 05: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, 08:00 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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