Program closes as soon as it opens?

This is a discussion on Program closes as soon as it opens? within the C++ Programming forums, part of the General Programming Boards category; I can't figure it out at all. I'm using Dev C++: for those who don't know, system("PAUSE"); is used to ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    Question Program closes as soon as it opens?

    I can't figure it out at all. I'm using Dev C++: for those who don't know, system("PAUSE"); is used to keep the console window open.

    For clarification: it's one of those Windows errors that say it has encountered a problem and needs to close.

    Code:
    #include <cstdlib>
    #include <stdlib.h>
    #include <iostream>
    #include <time.h>
    #include <string.h>
    
    
    using namespace std;
    
    class Player
    {
          public:
                 int punch();
                 int whut();
                 void define();
          private:
                  int HP;
                  int M;
                  int P;
                  int D;
                  int MP;
                  int MD;
                  int A;
                  int damage;
                  string Class;
                  string choices[7][7];
    };
    
    int Player::punch()
    {    
                    float n;
                    damage = 10;
                    n = (rand() &#37; 100) + 50;
                    n = n/100;
                    damage = (damage * n);                
                    cout << damage << '\n';
    }
                 
    int Player::whut()
    {
        int specialty1=0, specialty2=0;
        
        
        cout << "Pick your first specialty.\n";
        cin >> specialty1;
        cout << "Pick your second specialty.\n";
        cin >> specialty2;
        cout << "You chose the " << choices[specialty1][specialty2];
        cout << ".\n";
        Class = choices[specialty1][specialty2];
        
    }
    
    void Player::define()
    {
        choices[1][1] = "N/A";
        choices[1][2] = "Keeper";
        choices[1][3] = "Juggernaut";
        choices[1][4] = "Defender";
        choices[1][5] = "Battlemage";
        choices[1][6] = "Spell Canceler";
        choices[1][7] = "Cutpurse";
        choices[2][1] = "Keeper";
        choices[2][2] = "N/A";
        choices[2][3] = "Wizard";
        choices[2][4] = "Warlock";
        choices[2][5] = "Destroyer";
        choices[2][6] = "Spirit Breaker";
        choices[2][7] = "Fiend";
        choices[3][1] = "Juggernaut";
        choices[3][2] = "Wizard";
        choices[3][3] = "N/A";
        choices[3][4] = "Warrior";
        choices[3][5] = "Spellblade";
        choices[3][6] = "Knight";
        choices[3][7] = "Assassin";
        choices[4][1] = "Defender";
        choices[4][2] = "Warlock";
        choices[4][3] = "Warrior";
        choices[4][4] = "N/A";
        choices[4][5] = "Spellshield";
        choices[4][6] = "Protector";
        choices[4][7] = "Rogue";
        choices[5][1] = "Battlemage";
        choices[5][2] = "Destroyer";
        choices[5][3] = "Spellblade";
        choices[5][4] = "Spellshield";
        choices[5][5] = "N/A";
        choices[5][6] = "Mage";
        choices[5][7] = "Slayer";
        choices[6][1] = "Spell Canceler";
        choices[6][2] = "Spirit Breaker";
        choices[6][3] = "Knight";
        choices[6][4] = "Protector";
        choices[6][5] = "Mage";
        choices[6][6] = "N/A";
        choices[6][7] = "Stalker";
        choices[7][1] = "Cutpurse";
        choices[7][2] = "Thief";
        choices[7][3] = "Assassin";
        choices[7][4] = "Rogue";
        choices[7][5] = "Slayer";
        choices[7][6] = "Stalker";
        choices[7][7] = "N/A";
    }
    
    
        
        
    
    
          
    
    int main(int argc, char *argv[])
    {
        srand(unsigned(time(NULL)));
    
        Player test;    
        test.define();
        test.whut();
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Last edited by Kamarill; 09-24-2008 at 08:19 PM. Reason: Clarification

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Arrays go from 0 to 6, not 1 to 7.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Quote Originally Posted by tabstop View Post
    Arrays go from 0 to 6, not 1 to 7.
    That's what I thought too, so I changed it. But it doesn't matter what I put for the second number, as long as it isn't too huge. Is this because it's a two dimension array?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Hmm you program has several errors. What you are seeing is the build process being canceled. The program never gets to be executed.

    choices is an array of std::string. cout cannot use it. It needs to be converted to a c-style string:
    Code:
    cout << "You chose the " << choices[specialty1][specialty2].c_str();
    The member functions punch() and whut are said to return an int, but you specify no return statement.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So you are reading in a non-allocated memory space and the program crashes

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Kamarill View Post
    That's what I thought too, so I changed it. But it doesn't matter what I put for the second number, as long as it isn't too huge. Is this because it's a two dimension array?
    ...? What second number are we discussing here, and why are you changing it?

    Edit: Or do you mean the second number you type in? As long as first_number * 7 + second_number stays under 49, you're still inside your array, yes.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Quote Originally Posted by Mario F. View Post
    Hmm you program has several errors. What you are seeing is the build process being canceled. The program never gets to be executed.

    choices is an array of std::string. cout cannot use it. It needs to be converted to a c-style string:
    Code:
    cout << "You chose the " << choices[specialty1][specialty2].c_str();
    The member functions punch() and whut are said to return an int, but you specify no return statement.
    For some reason, the cout seems to still work, actually. As for the other functions, they have been changed to voids. Thanks.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Mario F. View Post
    choices is an array of std::string. cout cannot use it. It needs to be converted to a c-style string:
    Code:
    cout << "You chose the " << choices[specialty1][specialty2].c_str();
    I'm assuming it's late where you are. (Actually if I can do the time-zone calculations, it really is late where you are.) To OP: I think you can safely ignore this bit.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by Kamarill View Post
    That's what I thought too, so I changed it. But it doesn't matter what I put for the second number, as long as it isn't too huge. Is this because it's a two dimension array?
    A two dimension array will use continuous memory, such that if you index beyond the length of a row, it will overflow onto the next row. However there is still a problem if you index beyond the last row, when specifying an overly large index to either parameter.

    Modifying such invalid indexes can result in unpredictable behavior, including silent crashes.
    Last edited by King Mir; 09-24-2008 at 08:37 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Quote Originally Posted by tabstop View Post
    ...? What second number are we discussing here, and why are you changing it?
    The program receives two variables, specialty1 and specialty2. specialty1, I've found, must be 0, 1, 2, 3, 4, 5, or 6, but the specialty2, the second part of the choices array, can be as large as 49.

    Highlighted in the original code:

    Code:
    string choices[7][7];
    cout << "You chose the " << choices[specialty1][specialty2];

    I believe this is because it's a 7 x 7 array.

    Am I right?

    EDIT: King Mir has answered the last of my questions. Thanks everyone!
    Last edited by Kamarill; 09-24-2008 at 08:36 PM. Reason: To give thanks.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Quote Originally Posted by tabstop View Post
    I'm assuming it's late where you are. (Actually if I can do the time-zone calculations, it really is late where you are.) To OP: I think you can safely ignore this bit.
    Bleh.
    It is and I've had quiet a day. I'll take the hint. Sorry about that.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  2. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 06:44 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. program open and closes
    By Musicdip in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-27-2002, 04:34 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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