Thread: Very strange problem...

  1. #1

    Unhappy Very strange problem...

    I've been working on another RPG, except on this one, I'm building it specificly to be used by me or someone else as an engine. I've got some code to compile, but when I run it, Windows politiely crashes the program. I am using VC++6, on Windows98. Here's the code:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <dos.h>
    #include <conio.h>
    #include <stdio.h>
    #include <time.h>
    
    void cls()
    {
     COORD coordScreen = { 0, 0 };
     DWORD cCharsWritten;
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     DWORD dwConSize;
     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
     GetConsoleScreenBufferInfo(hConsole, &csbi);
     dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
     FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
     GetConsoleScreenBufferInfo(hConsole, &csbi);
     FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
     SetConsoleCursorPosition(hConsole, coordScreen);
    }
    
    void Assign(char a[10], char b[10]) {
     int times;
     do {
      a[times] = b[times];
      times++;
     } while (times != 10 || a[times + 1] == NULL);
    }
    
    void Randomize() {
     srand(time(NULL));
    }
    
    int Rand(int par1) {
     return (rand() % par1);
    }
    
    struct Spell {
     int power;
     bool enabled;
    };
    
    class Enemy {
     public:
      int str, def, lev;
      int ehp, ehpmax;
      char ename[20];
      void Set(int s, int d, int l);
    };
    
    void Enemy::Set(int s, int d, int l) {
     Randomize();
     ehpmax = Rand((s + d) * 2);
     ehp = ehpmax;
    }
    
    class Player {
     public:
      int str, def, mag, lev, luck;
      int exp, nlev, expadd, mp, hp, mpmax, hpmax, mexp;
      Spell fire1, water1, ice1, fire2, water2, ice2;
      char name[20];
      void Roll(int skill);
    };
    
    Player One;
    Enemy Orc;
    int dam;
    
    void Player::Roll(int skill) {
     Randomize();
     lev = Rand(skill + 4) + 1;
     str = Rand(lev) + 1;
     def = Rand(lev) + 1;
     mag = Rand(lev) + 1;
     luck = Rand(luck) + 2;
     exp = 0;
     nlev = ((100 * lev) + Rand(100));
     Randomize();
     hpmax = Rand((lev + (str * 2) + (def * 3)));
     hp = hpmax;
     Randomize();
     mpmax = Rand((lev + (mag * 2)));
     mp = mpmax;
     mexp = 0;
     fire1.enabled = true;
     fire1.power = 2;
     water1.enabled = true;
     water1.power = 2;
     ice1.enabled = true;
     ice1.power = 2;
     fire2.enabled = false;
     fire2.power = 5;
     water2.enabled = false;
     water2.power = 5;
     ice2.enabled = false;
     ice2.power = 5;
    }
    
    bool DieEval(int hpleft, int damage) {
     if (hpleft < damage) {
      return true;
     } else {
      return false;
     }
    }
    
    void EndBattle(Player Guy) {
     cout << "This part is almost done...\n";
    }
    
    void Death() {
     cout << "You have died...\n";
    }
    
    void EAtt(Enemy EAtt, Player EDefen) {
     Randomize();
     dam = 0;
     dam = (Rand((EAtt.lev + 2 + EAtt.str - EDefen.def)));
     if (DieEval(EDefen.hp, dam) == true) {
      Death();
     } else {
      cout << EAtt.ename << " did " << dam << " points of damage.\n";
      getch();
      cls();
     }
    }
    
    void PAtt(Player Att, Enemy Defen) {
     Randomize();
     dam = 0;
     dam = (Rand((Att.lev + 2 + Att.str - Defen.def)));
     if (DieEval(Defen.ehp, dam) == true) {
      EndBattle(Att);
     } else {
      cout << Att.name << " did " << dam << " points of damage.\n";
      getch();
      cls();
      EAtt(Orc, One);
     }
    }
    
    void BMenu(Player Play, Enemy Enem) {
     int choice;
     do {
      cout << "Your HP: " << Play.hp << "/" << Play.hpmax << " Your MP: " << Play.mp << "/" << Play.mpmax << "\nEnemy HP: " << Enem.ehp << "/" << Enem.ehpmax << endl;
      cout << "\nBattle Menu:\n[1] Attack\n[2] Cast Spell\n>";
      cin >> choice;
      if (choice == 1) {
       PAtt(Play, Enem);
      } 
     } while (Play.hp > 0 && Enem.ehp > 0);
    }
    
    
    void main() {
     cls();
     printf("RPG V0.1\n");
     Sleep(1000);
     Assign("Scott", One.name);
     One.Roll(5);
     Orc.Set(3,3,3);
     Assign("Orc", Orc.ename);
     BMenu(One, Orc);
    }
    I know it's not totally complete yet. (I.E. I need to put in the magic!) but I'm workin on it. Any help would be nice.

    Thanks, Valar_King
    -Save the whales. Collect the whole set.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Your Assign() function contains an unintialised variable - times. You are trying to use this variable to access an array and are probably going out of bounds.
    zen

  3. #3
    i tried initializing it to 0. ugh, windows...
    -Save the whales. Collect the whole set.

  4. #4
    I worked that problem out, but now, how do I assign a value to Orc.ename? I tried:
    Code:
    Orc.ename = "Orc";
    Doesn't work. How would I go about making a pointer to change it's value? Here''s my updated code:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <dos.h>
    #include <conio.h>
    #include <stdio.h>
    #include <time.h>
    
    void cls()
    {
     COORD coordScreen = { 0, 0 };
     DWORD cCharsWritten;
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     DWORD dwConSize;
     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
     GetConsoleScreenBufferInfo(hConsole, &csbi);
     dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
     FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
     GetConsoleScreenBufferInfo(hConsole, &csbi);
     FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
     SetConsoleCursorPosition(hConsole, coordScreen);
    }
    
    
    void Randomize() {
     srand(time(NULL));
    }
    
    int Rand(int par1) {
     return ((rand() % par1) + 1);
    }
    
    struct Spell {
     int power;
     bool enabled;
    };
    
    class Enemy {
     public:
      int str, def, lev;
      int ehp, ehpmax;
      char ename[12], *enamep;
      void Set(int s, int d, int l);
    };
    
    void Enemy::Set(int s, int d, int l) {
     Randomize();
     ehpmax = Rand((s + d) * 2);
     ehp = ehpmax;
     enamep = ename;
    }
    
    class Player {
     public:
      int str, def, mag, lev, luck;
      int exp, nlev, expadd, mp, hp, mpmax, hpmax, mexp;
      Spell fire1, water1, ice1, fire2, water2, ice2;
      char name[12];
      void Roll(int skill);
    };
    
    Player One;
    Enemy Orc;
    int dam;
    
    void Player::Roll(int skill) {
     Randomize();
     lev = Rand(skill + 4) + 1;
     str = Rand(lev) + 1;
     def = Rand(lev) + 1;
     mag = Rand(lev) + 1;
     luck = Rand(lev) + 2;
     exp = 0;
     nlev = ((100 * lev) + Rand(100));
     Randomize();
     hpmax = Rand((lev + (str * 2) + (def * 3)));
     hp = hpmax;
     Randomize();
     mpmax = Rand((lev + (mag * 2)));
     mp = mpmax;
     mexp = 0;
     fire1.enabled = true;
     fire1.power = 2;
     water1.enabled = true;
     water1.power = 2;
     ice1.enabled = true;
     ice1.power = 2;
     fire2.enabled = false;
     fire2.power = 5;
     water2.enabled = false;
     water2.power = 5;
     ice2.enabled = false;
     ice2.power = 5;
    }
    
    bool DieEval(int hpleft, int damage) {
     if (hpleft < damage) {
      return true;
     } else {
      return false;
     }
    }
    
    void EndBattle(Player Guy) {
     cout << "This part is almost done...\n";
     exit(0);
    }
    
    void Death() {
     cout << "You have died...\n";
    }
    
    void EAtt(Enemy EAtt, Player EDefen) {
     Randomize();
     dam = 0;
     dam = ((EAtt.lev + 2 + EAtt.str - EDefen.def) + Rand(5));
     EDefen.hp = EDefen.hp - dam;
     if (EDefen.hp <= 0) {
      Death();
     } else {
      cout << EAtt.ename << " did " << dam << " points of damage.\n\n";
     }
    }
    
    void PAtt(Player Att, Enemy Defen) {
     Randomize();
     dam = 0;
     dam = ((Att.lev + 2 + Att.str - Defen.def) + Rand(5));
     Defen.ehp = Defen.ehp - dam;
     if (Defen.ehp <= 0) {
      EndBattle(Att);
     } else {
      cout << Att.name << " did " << dam << " points of damage.\n\n";
      EAtt(Orc, One);
     }
    }
    
    void BMenu(Player Play, Enemy Enem) {
     int choice;
     do {
      cout << Play.name << "'s HP: " << Play.hp << "/ " << Play.hpmax << " " << Play.name << "'s MP: " << Play.mp << "/" << Play.mpmax << endl << Enem.ename << "'s HP: " << Enem.ehp << "/" << Enem.ehpmax << endl;
      cout << "\nBattle Menu:\n[1] Attack\n[2] Cast Spell\n>";
      cin >> choice;
      if (choice == 1) {
       PAtt(Play, Enem);
      } 
     } while (Play.hp > 0 && Enem.ehp > 0);
    }
    
    
    void main() {
     cls();
     printf("RPG V0.1\n");
     Sleep(1000);
     cout << "Enter name (8 letters Max.): ";
     cin >> One.name;
     One.Roll(81);
     Orc.Set(72,72,3);
     Orc.enamep = "Orc";
     cls();
     BMenu(One, Orc);
    }
    Thanks
    -Save the whales. Collect the whole set.

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Use strcpy().
    zen

  6. #6
    Ok, thanks, I used strcpy() and that part works fine, and now everything I've put in is working well, EXCEPT for the fact that neither the player, or the computer's hp decreases, they die if they get a hit over their max hp, but that's the only way anyone dies.

    Thanks alot,
    Valar_King
    -Save the whales. Collect the whole set.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM