C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-05-2005, 02:01 AM   #1
1479
 
Join Date: Aug 2003
Posts: 253
No compile time errors, exe crashes.

There is a lot of code to this program now and I have narrowed my problem down to one of my classes. It compiles fine but when I run the program and go through a few options its good. After I got a class set up I tried to see if I could get some output from them but it crashes the console.

Here is the class I am using:
Code:
class Weapons {
      public:
             Weapons();
             string getWeaponValue(); //Legendary, Epic, unique, rare, magical, normal
             string getWeaponType(); //Sword, mace, staff
             string getWeaponName(); //Name the weapon
             int getWeaponStats(); // damage, +mods and durability
             int getMagicStats();
             int getWeaponLevel(); // level of the weapon
             void whoCanDrop();  //check to see which mobs can drop it
      private:
              string WeaponName;
              string WeaponType;
              string WeaponValue;
              int WeaponStats;
              int WeaponDamage;
              int WeaponDurability;
              int WeaponLevel;
              char CanIDropIt;
              };
One of these members is what I think is crashing the program.

Code:
string Weapons::getWeaponValue()
{
 int x;
 srand(time(0));
 x = 1 + rand() % 8;
 switch (x)
 {
        case 1:
             WeaponValue ="Normal";
        case 2:
             WeaponValue = "Magical";
        case 3:
             WeaponValue = "Rare";
        case 4: 
             WeaponValue = "Unique";
        case 5:
             WeaponValue = "Epic";
        case 6:
             WeaponValue = "Legendary";
        default:
             WeaponValue = "Normal";
        cout <<WeaponValue;
}
}
string Weapons::getWeaponType()
{
     int x;
     srand(time(0));
     x = 1 + rand() % 10;
     switch (x){
            case 1:
                 WeaponType = "Sword";
            case 2:
                 WeaponType = "Dagger";
            case 3: 
                 WeaponType = "Mace";
            case 4:
                 WeaponType = "Staff";
            case 5:
                 WeaponType = "Wand";
            case 6:
                 WeaponType = "Axe";
            case 7:
                 WeaponType = "Club";
            default:
                 WeaponType = "Exotic";        
                 }
                 //cout <<WeaponType;
}
string Weapons::getWeaponName()
{
     int x;
     srand(time(0));
     if (WeaponType == "Sword" && WeaponValue == "Normal" && WeaponLevel <= 10)
     {
        x = 1 + rand() % 12;
        switch (x){
          
               case 1:
                    WeaponName = "RustedSword";
               case 2:
                    WeaponName = "DullSword";
               case 3:
                    WeaponName = "BrokenSword";
               case 4:
                    WeaponName = "UnforgedSword";
               case 5:
                    WeaponName = "BluntSword";
               case 6:
                    WeaponName = "WeakSword";
               case 7:
                    WeaponName = "Tiny";
               case 8:
                    WeaponName = "Sharp Knife";
               default:
                    WeaponName = "WoodenSword";
                    }
     }
     if (WeaponType == "Mace" && WeaponValue == "Normal" && WeaponLevel <= 10)
     {
        x = 1 + rand() % 12;
        switch (x){
          
               case 1:
                    WeaponName = "RustedMace";
               case 2:
                    WeaponName = "DullMace";
               case 3:
                    WeaponName = "BrokenMace";
               case 4:
                    WeaponName = "UnforgedMace";
               case 5:
                    WeaponName = "BluntMace";
               case 6:
                    WeaponName = "WeakMace";
               case 7:
                    WeaponName = "Chunky";
               case 8:
                    WeaponName = "SharpMace";
               default:
                    WeaponName = "WoodenMace";
                    }
     }
     if (WeaponType == "Axe" && WeaponValue == "Normal" && WeaponLevel <= 10)
     {
        x = 1 + rand() % 12;
        switch (x){
          
               case 1:
                    WeaponName = "RustedAxe";
               case 2:
                    WeaponName = "DullAxe";
               case 3:
                    WeaponName = "BrokenAxe";
               case 4:
                    WeaponName = "UnforgedAxe";
               case 5:
                    WeaponName = "BluntAxe";
               case 6:
                    WeaponName = "WeakAxe";
               case 7:
                    WeaponName = "Pointy";
               case 8:
                    WeaponName = "SharpAxe";
               default:
                    WeaponName = "Hatchet";
                    }
     }
     if (WeaponType == "Club" && WeaponValue == "Normal" && WeaponLevel <= 10)
     {
        x = 1 + rand() % 12;
        switch (x){
          
               case 1:
                    WeaponName = "SplinteredClub";
               case 2:
                    WeaponName = "FlatClub";
               case 3:
                    WeaponName = "BrokenClub";
               case 4:
                    WeaponName = "UnforgedClub";
               case 5:
                    WeaponName = "BluntClub";
               case 6:
                    WeaponName = "WeakClub";
               case 7:
                    WeaponName = "Skinny";
               case 8:
                    WeaponName = "ThickClub";
               default:
                    WeaponName = "WoodenClub";
                    }
     }
     if (WeaponType == "Dagger" && WeaponValue == "Normal" && WeaponLevel <= 10)
     {
        x = 1 + rand() % 12;
        switch (x){
          
               case 1:
                    WeaponName = "RustedDagger";
               case 2:
                    WeaponName = "DullDagger";
               case 3:
                    WeaponName = "BrokenDagger";
               case 4:
                    WeaponName = "UnforgedDagger";
               case 5:
                    WeaponName = "BluntDagger";
               case 6:
                    WeaponName = "WeakDagger";
               case 7:
                    WeaponName = "Poinert";
               case 8:
                    WeaponName = "SharpDagger";
               default:
                    WeaponName = "WoodenDagger";
                    }
     }
     if (WeaponType == "Staff" && WeaponValue == "Normal" && WeaponLevel <= 10)
     {
        x = 1 + rand() % 12;
        switch (x){
          
               case 1:
                    WeaponName = "SplinteredStaff";
               case 2:
                    WeaponName = "CrackedStaff";
               case 3:
                    WeaponName = "BrokenStaff";
               case 4:
                    WeaponName = "UnforgedStaff";
               case 5:
                    WeaponName = "BluntStaff";
               case 6:
                    WeaponName = "WeakStaff";
               case 7:
                    WeaponName = "LongStaff";
               case 8:
                    WeaponName = "HardStaff";
               default:
                    WeaponName = "WoodenStaff";
                    }
     }
     if (WeaponType == "Wand" && WeaponValue == "Normal" && WeaponLevel <= 10)
     {
        x = 1 + rand() % 12;
        switch (x){
          
               case 1:
                    WeaponName = "RustedWand";
               case 2:
                    WeaponName = "BurntWand";
               case 3:
                    WeaponName = "BrokenWand";
               case 4:
                    WeaponName = "UnforgedWand";
               case 5:
                    WeaponName = "WornWand";
               case 6:
                    WeaponName = "WeakWand";
               case 7:
                    WeaponName = "Sparky";
               case 8:
                    WeaponName = "BrightWand";
               default:
                    WeaponName = "WoodenWand";
                    }
                    cout <<WeaponName;
     }
     if (WeaponType == "Sword" || 
WeaponType == "Axe" ||
 WeaponType == "Mace" ||
 WeaponType == "Dagger" || 
WeaponType == "Wand" || 
WeaponType == "Club" || 
WeaponType == "Staff" && 
WeaponValue =="Magical" && 
WeaponLevel >=3)
     {
        x = 1 + (rand() % 10) - Clvl;
        switch (x){
               case 1:
                    WeaponName = "Of the Bear";
               case 2:
                    WeaponName = "Of the Wolf";
               case 3:
                    WeaponName = "Of the Bird";
                    
               default:
                    WeaponName = "Mended Weapon";
                    }
                    cout <<WeaponName;
     }
     if (WeaponType == "Sword" || 
         WeaponType == "Axe" || 
         WeaponType == "Mace" || 
         WeaponType == "Dagger" || 
         WeaponType == "Wand" ||
         WeaponType == "Club" ||
         WeaponType == "Staff" && 
         WeaponValue =="Magical" && 
         WeaponLevel >=5)
     {
        x = 1 + (rand() % 10) - Clvl;
        switch (x){
               case 1:
                    WeaponName = "Of the Bear";
                    
               case 2:
                    WeaponName = "Of the Wolf";
                    
               case 3:
                    WeaponName = "Of the Bird";
               case 4:
                    WeaponName = "Of the Monkey";
                    
               case 5: 
                    WeaponName = "Of the Scorpid";
                    
               default:
                    WeaponName = "Glowing Sword";
                    }
                    cout <<WeaponName;
     }
     if (WeaponType == "Sword" || 
         WeaponType == "Axe" ||
         WeaponType == "Mace" ||
         WeaponType == "Dagger" || 
         WeaponType == "Wand" || 
         WeaponType == "Club" || 
         WeaponType == "Staff" && 
         WeaponValue =="Magical" && 
         WeaponLevel >=10)
     {
        x = 1 + (rand() % 23) - Clvl;
        switch (x){
               case 1:
                    WeaponName = "Of the Bear";
               case 2:
                    WeaponName = "Of the Wolf";
               case 3:
                    WeaponName = "Of the Bird";
               case 4: 
                    WeaponName = "Of the Monkey";
               case 5: 
                    WeaponName = "Of the Scorpid";
               case 6:
                    WeaponName = "Of the Wise";                    
               case 7:
                    WeaponName = "Of the Mystics";
               case 8:
                    WeaponName = "Of the Devine";
              case 9:
                    WeaponName = "Of the Earth";
               case 10:
                    WeaponName = "Of the Brute";
               case 11: //might
                    WeaponName = "Of Might";
               case 12://wisdom
                    WeaponName = "Of Wisdom";                    
               case 13://gods
                    WeaponName = "Of the Gods";
               default:
                    WeaponName = "Glowing Weapon";
                    }
                    cout <<WeaponName;
     }
     
                   
}
int Weapons::getWeaponStats()
{
    if (WeaponType == "Sword" || 
        WeaponType == "Mace" || 
        WeaponType == "Dagger" || 
        WeaponType == "Axe" || 
        WeaponType == "Club" || 
        WeaponType == "Staff" || 
        WeaponType == "Wand" && 
        WeaponValue == "Normal" && 
Clvl <= 3)
    {
       WeaponStats = 0;
       WeaponDamage = (Clvl * WeaponLevel) + Str; 
       WeaponDurability = 1 + rand() % 20;
    }
    if (WeaponType == "Sword" && WeaponValue == "Normal" && Clvl <= 5)
    {
       WeaponStats = 0;
       WeaponDamage = (Clvl * WeaponLevel) + Str;
       WeaponDurability = 1 + rand() % 30;
    }
    if (WeaponType == "Sword" && WeaponValue == "Normal" && Clvl <= 10)
    {
       WeaponStats = 0;
       WeaponDamage = (Clvl * WeaponLevel) + Str; 
       WeaponDurability = 1 + rand() % 50;
    }
                                 
                     
       int getMagicStats(string WeaponType, string WeaponValue, string WeaponName);              
       
       WeaponDamage = 1 + (rand() % 10) - Clvl; 
       WeaponDurability = 1 + rand() % 20;
}

int Weapons::getMagicStats()
{
    int x; //random variable to create random numbers
    int y; //random variable to decide stat
    int z; //random variable to decide number of a single stat
    srand(time(0));
    
    if (WeaponValue == "Magical")
    {
                    
                    WeaponDamage = (Clvl * WeaponLevel) + Str;
                    WeaponDurability = Clvl * WeaponLevel;
    }
    if (WeaponValue == "Magical")
    {
       if (WeaponName == "Of the Bear") // +str(lots), +hp(lots) and small amount of +agi(small) 
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
		            WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
	           case 2:
		            WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
                    WeaponMod2 = Clvl * 3 * (1 + rand() % WeaponLevel);
              case 3: 
		           WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
                   WeaponMod2 = Clvl * 4 * (1 + rand() % WeaponLevel);
		           WeaponMod4 = Clvl + (1 + rand() % WeaponLevel);
            default:
		            WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
	             }
       }
       if (WeaponName == "Of the Wolf") // +str(small), +hp(small) and +agi(lots) 
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
                    if (Clvl == 1)
		            WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
		            if (Clvl >= 2)
		            WeaponMod1 = (Clvl / 2) * (1 + rand() % WeaponLevel);
	           case 2:
		            if (Clvl == 1)
		            WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
		            if (Clvl >= 2)
		            WeaponMod1 = (Clvl / 2) * (1 + rand() % WeaponLevel);
                    WeaponMod2 = Clvl * 2 * (1 + rand() % WeaponLevel);
              case 3: 
		           if (Clvl == 1)
		              WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
		           if (Clvl >= 2)
		              WeaponMod1 = (Clvl / 2) * (1 + rand() % WeaponLevel);
                   WeaponMod2 = (Clvl * 2) * (1 + rand() % WeaponLevel);
		           WeaponMod4 = (Clvl * 3) + (1 + rand() % WeaponLevel);
            default:
		            WeaponMod4 = (Clvl * 3) + (1 + rand() % WeaponLevel);
	             }
       }
       if (WeaponName == "Of the Bird") // +hp(very small) +mp(small) +agi(lots)
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
                    WeaponMod2 = Clvl * (1 + rand() % WeaponLevel);
	           case 2:
		            WeaponMod2 = Clvl * (1 + rand() % WeaponLevel);
                    WeaponMod3 = Clvl * (1 + rand() % WeaponLevel);
              case 3: 
		           WeaponMod2 = Clvl * (1 + rand() % WeaponLevel);
                   WeaponMod3 = (Clvl * 2) * (1 + rand() % WeaponLevel);
		           WeaponMod4 = (Clvl * 3) + (1 + rand() % WeaponLevel);
            default:
		            WeaponMod4 = (Clvl * 2) + (1 + rand() % WeaponLevel);
	             }
       }
       if (WeaponName == "Of the Monkey") // +hp's(very small) +agi(very much) 
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
                    WeaponMod2 = Clvl * (1 + rand() % WeaponLevel);
	           case 2:
		            WeaponMod2 = Clvl * (1 + rand() % WeaponLevel);
                    WeaponMod4 = (Clvl * 6) + (1 + rand() % WeaponLevel);
            default:
		            WeaponMod4 = (Clvl * 4) + (1 + rand() % WeaponLevel);
	             }
       }
        if (WeaponName == "Of the Scorpid") // +hp(very small) +agi(small) +armor(lots)
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
                    WeaponMod2 = Clvl * (1 + rand() % WeaponLevel);
	           case 2:
		            WeaponMod2 = Clvl * (1 + rand() % WeaponLevel);
                    WeaponMod4 = (Clvl * 2) + (1 + rand() % WeaponLevel);
              case 3: 
                    WeaponMod2 = Clvl * (1 + rand() % WeaponLevel);
                    WeaponMod4 = (Clvl * 2) + (1 + rand() % WeaponLevel);
                    WeaponMod5 = (Clvl * 5) * (1 + rand() % WeaponLevel);
            default:
		            WeaponMod5 = (Clvl * 5) + (1 + rand() % WeaponLevel);
	             }
       }
       if (WeaponName == "Of the Wise") // +hp(very small) +agi(small) +armor(lots)
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
                    WeaponMod3 = (Clvl * 10) * (1 + rand() % WeaponLevel);
            default:
		            WeaponMod3 = (Clvl * 5) * (1 + rand() % WeaponLevel);
	             }
       }
       if (WeaponName == "Of the Mystics") // +hp's(lots) +mp's(small) +agi(small)
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
                    WeaponMod2 = (Clvl * 3) * (1 + rand() % WeaponLevel);
	           case 2:
		            WeaponMod2 = (Clvl * 4) * (1 + rand() % WeaponLevel);
                    WeaponMod3 = (Clvl * 3) * (1 + rand() % WeaponLevel);
              case 3: 
                    WeaponMod2 = (Clvl * 4) * (1 + rand() % WeaponLevel);
                    WeaponMod3 = (Clvl * 3) * (1 + rand() % WeaponLevel);
                    WeaponMod4 = (Clvl * 2) + (1 + rand() % WeaponLevel);
            default:
		            WeaponMod2 = (Clvl * 4) * (1 + rand() % WeaponLevel);
                    WeaponMod3 = (Clvl * 3) * (1 + rand() % WeaponLevel);
	             }
       }
       if (WeaponName == "Of the Devine") // +all stats(small)
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
		            WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
		            WeaponMod2 = (Clvl * 2) * (1 + rand() % WeaponLevel);
                    WeaponMod3 = (Clvl * 2) * (1 + rand() % WeaponLevel);
                    WeaponMod4 = (Clvl * 3) + (1 + rand() % WeaponLevel);           
            default:
		            if (Clvl == 1)
		            WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
		            if (Clvl >= 2)
		            WeaponMod1 = (Clvl / 2) * (1 + rand() % WeaponLevel);  
                    WeaponMod2 = Clvl * (1 + rand() % WeaponLevel);
                    WeaponMod3 = Clvl * (1 + rand() % WeaponLevel);
                    WeaponMod4 = (Clvl * 2) + (1 + rand() % WeaponLevel);
	             }
       }
       if (WeaponName == "Of the Earth") // +hp +armor(lots)
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
		            WeaponMod2 = (Clvl * 3) * (1 + rand() % WeaponLevel);
               case 2:
                    WeaponMod2 = (Clvl * 3) * (1 + rand() % WeaponLevel);
                    WeaponMod5 = (Clvl * 10) + (1 + rand() % WeaponLevel);
               default:
                    WeaponMod5 = (Clvl * 5) + (1 + rand() % WeaponLevel);
                   
	             }
       }
       if (WeaponName == "Of the Brute") // +hp +armor(lots)
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
		            WeaponMod6 = (Clvl * 5) * (1 + rand() % WeaponLevel);
               case 2:
                    WeaponMod6 = (Clvl * 3) * (1 + rand() % WeaponLevel);
                    WeaponMod5 = (Clvl * 5) + (1 + rand() % WeaponLevel);
               default:
                    WeaponMod6 = (Clvl * 7) * (1 + rand() % WeaponLevel);
                   
	             }
       }
       if (WeaponName == "Of Might") // 1str 2hp's 6damage
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
		            WeaponMod1 = (Clvl * 2) + (1 + rand() % WeaponLevel);
               case 2:
                    WeaponMod1 = (Clvl * 2) + (1 + rand() % WeaponLevel);
                    WeaponMod6 = (Clvl * 3) * (1 + rand() % WeaponLevel);
               default:
                    WeaponMod6 = (Clvl * 7) * (1 + rand() % WeaponLevel);
                   
	             }
       }
       if (WeaponName == "Of Wisdom") // hp mp
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
		            WeaponMod2 = (Clvl * 8) + (1 + rand() % WeaponLevel);
               case 2:
                    WeaponMod2 = (Clvl * 5) + (1 + rand() % WeaponLevel);
                    WeaponMod3 = (Clvl * 10) + (1 + rand() % WeaponLevel);
               default:
                    WeaponMod2 = (Clvl * 3) + (1 + rand() % WeaponLevel);
                   
	             }
       }
       if (WeaponName == "Of the Gods") // hp mp
       {
        int x;
        x = 1 + rand() % 8; //Number of stats
        switch (x){
	           case 1:
                    WeaponMod1 = (Clvl * 2) + (1 + rand() % WeaponLevel);
		            WeaponMod2 = (Clvl * 8) + (1 + rand() % WeaponLevel);
		            WeaponMod3 = (Clvl * 10) + (1 + rand() % WeaponLevel);
		            WeaponMod4 = (Clvl * 4) + (1 + rand() % WeaponLevel);
		            WeaponMod5 = (Clvl * 5) + (1 + rand() % WeaponLevel);
		            WeaponMod6 = (Clvl * 7) * (1 + rand() % WeaponLevel);
		            
               case 2:
                    WeaponMod1 = (Clvl * 3) + (1 + rand() % WeaponLevel);
		            WeaponMod2 = (Clvl * 9) + (1 + rand() % WeaponLevel);
		            WeaponMod3 = (Clvl * 11) + (1 + rand() % WeaponLevel);
		            WeaponMod4 = (Clvl * 6) + (1 + rand() % WeaponLevel);
		            WeaponMod5 = (Clvl * 7) + (1 + rand() % WeaponLevel);
		            WeaponMod6 = (Clvl * 8) * (1 + rand() % WeaponLevel);
               default:
                    WeaponMod1 = (Clvl * 2) + (1 + rand() % WeaponLevel);
		            WeaponMod2 = (Clvl * 5) + (1 + rand() % WeaponLevel);
		            WeaponMod3 = (Clvl * 5) + (1 + rand() % WeaponLevel);
		            WeaponMod4 = (Clvl * 4) + (1 + rand() % WeaponLevel);
		            WeaponMod5 = (Clvl * 5) + (1 + rand() % WeaponLevel);
		            WeaponMod6 = (Clvl * 5) * (1 + rand() % WeaponLevel);
                   
	             }
              }
            }
        }Weapons WepLoot;
Pardon all the weird placement of some of the text, I had to edit this to get it to fit to the page so it wouldn't be so wide. Its not how I have it in my program. And here is the int main()

Code:
int main()
{
    int x;
    string blah;
    srand(time(0));
    StartGame();
    system ("cls");
    cout <<"Welcome " <<YourName <<" to the Age of Spire!" <<endl;
    cout <<endl;
    cout <<"Do you wish to view the history and backstory?(Y or N)" <<endl;
    getline(cin, blah, '\n');
    if (blah == "y" || blah == "Y")
      cout <<"Story here." <<endl;
      else 
      cout <<"Play then." <<endl;
      
     WepLoot.getWeaponValue();
     WepLoot.getWeaponType();
     WepLoot.getWeaponName();
     AnyKey();
 }
__________________
Knowledge is power and I want it all

-0RealityFusion0-
RealityFusion is offline   Reply With Quote
Old 09-05-2005, 05:29 AM   #2
Linker.exe64
 
Join Date: Mar 2005
Location: Croatia,Pozega
Posts: 37
Lot of hard-coding....
You probably have memory leak. If you have VisualC++, you can download VisualLeakDetector (VLD) here -> http://www.codeproject.com/tools/visualleakdetector.asp .

...and make sure that all pointers are deleted when they are not needed.
NOTE : When you delete pointer, set it to NULL, so it will not point to any memory location.
e.g
Code:
int* myPointer;
...
delete myPointer;
myPointer = NULL;
If you don't want to do all this, you can use smart pointers.
Instead of :
Code:
int* myPointer;
...
delete myPointer;
myPointer = NULL;
... you would write :
Code:
auto_ptr <Type> mySmartPointer;
Now you don't need to delete that pointer. You need to include <memory> to use smart pointers.
__________________
Code:
 
 .J?+.                             ?`+.
   .+1P .++.                         ,+` 4++.
  .+zq\ .:.?i                      .!`?  .yz+.
  ?zdd ..:.^`J.                   ,!..?...Kyz+	
 .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
 .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
  jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
   0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
    TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW= 
      7Mh,``JMMMM"`          .TMMMMb``.H#"`
 
81N4RY_DR460N is offline   Reply With Quote
Old 09-05-2005, 07:21 AM   #3
Registered User
 
Join Date: Aug 2005
Posts: 1,265
Other than it apparently crashes -- it won't work correctly either. You need to put a break line after each of the case statements, the way you have it all the case statements get executed so the variable(s) will have the values of only the last case statement, which will definitely produce unwanted results.
Ancient Dragon is offline   Reply With Quote
Old 09-05-2005, 09:08 AM   #4
ZuK
Registered User
 
Join Date: Aug 2005
Posts: 1,303
I'm just guessing but I think you don't want
Code:
     if (WeaponType == "Sword" || 
        WeaponType == "Axe" ||
        WeaponType == "Mace" ||
        WeaponType == "Dagger" || 
        WeaponType == "Wand" || 
        WeaponType == "Club" || 
        WeaponType == "Staff" && 
        WeaponValue =="Magical" && 
        WeaponLevel >=3)
more logical to me would be
Code:
     if ((WeaponType == "Sword" || 
         WeaponType == "Axe" ||
         WeaponType == "Mace" ||
         WeaponType == "Dagger" || 
         WeaponType == "Wand" || 
         WeaponType == "Club" || 
         WeaponType == "Staff" ) && ( WeaponValue =="Magical" && WeaponLevel >=3 ))
Kurt
ZuK is offline   Reply With Quote
Old 09-05-2005, 10:53 AM   #5
1479
 
Join Date: Aug 2003
Posts: 253
I thought that by adding the break; statements it would fix it but it did not .
Quote:
Originally Posted by 81N4RY_DR460N
...and make sure that all pointers are deleted when they are not needed.
No pointers were used.
__________________
Knowledge is power and I want it all

-0RealityFusion0-
RealityFusion is offline   Reply With Quote
Old 09-05-2005, 04:11 PM   #6
1479
 
Join Date: Aug 2003
Posts: 253
Anyone got any idea's why its doing this? Im kinda lost since I don't get any compile time errors.
__________________
Knowledge is power and I want it all

-0RealityFusion0-
RealityFusion is offline   Reply With Quote
Old 09-05-2005, 04:20 PM   #7
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
Seems possible to me that there's a divide-by-zero somewhere...

Where is WeaponLevel initialized? Make sure it isn't set to zero
__________________
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)

"Work hard and it might happen."
-XSquared
JaWiB is offline   Reply With Quote
Old 09-05-2005, 04:47 PM   #8
Registered User
 
Join Date: Aug 2005
Posts: 1,265
Quote:
Originally Posted by RealityFusion
I thought that by adding the break; statements it would fix it but it did not .
.
The program absolutely needs those break statements, so even if it does not fix the problem they need to be there.

I don't see anything in the code you posted that would cause that kind of problem. All that code is using std::string objects and its pretty darned hard to break it. The real problem is pobably someplace else in your program. Is your program using pointers anywhere? Or any of the C-style string functions in string.h (such as strcpy, strcat, sprintf, etc)? All those have potential to cause memory problems if you're not very very careful.
Ancient Dragon is offline   Reply With Quote
Old 09-05-2005, 06:17 PM   #9
1479
 
Join Date: Aug 2003
Posts: 253
I haven't gotten around to setting up the weapon level or the Clvl so I just declared Clvl globaly to equal 5 and added the weapon level data member.

Code:
int Weapons::getWeaponLevel()
{
    WeaponLevel = 5;
}
Adding this bit of code didn't solve the problem.

Quote:
Originally Posted by Ancient Dragon
Is your program using pointers anywhere? Or any of the C-style string functions in string.h (such as strcpy, strcat, sprintf, etc)? All those have potential to cause memory problems if you're not very very careful.
This program contains 0 pointers and I am not using strcpy, strcat or sprintf.

EDIT: I uploaded the entire source file, so if anyone has time to look over it I would be most appreciative. I know its asking a lot but I am soo lost and don't even know where to begin to look to solve this problem.
Attached Files
File Type: cpp First Draft AoS.cpp (34.1 KB, 15 views)
__________________
Knowledge is power and I want it all

-0RealityFusion0-

Last edited by RealityFusion; 09-05-2005 at 06:23 PM.
RealityFusion is offline   Reply With Quote
Old 09-05-2005, 06:37 PM   #10
Dae
Deprecated
 
Dae's Avatar
 
Join Date: Oct 2004
Location: Canada
Posts: 944
Quote:
Originally Posted by RealityFusion
Code:
int Weapons::getWeaponLevel()
{
    WeaponLevel = 5;
}
Adding this bit of code didn't solve the problem.
Shouldnt that "return WeaponLevel;" (and probably not set it to 5, set it to 5 in the constructor).

EDIT: Nevermind. Youre using the "get" method differently than I'm used to.

Cant figure out whats causing the exception, its happening at the end of getWeaponType, but it could arise anywhere and just not take effect till there. This doesnt fix it, but you should have your cout outside the switch function BTW:

Code:
 switch (x)
 {
        case 1:
             WeaponValue ="Normal";
             break;
        case 2:
             WeaponValue = "Magical";
             break;
        case 3:
             WeaponValue = "Rare";
             break;
        case 4: 
             WeaponValue = "Unique";
             break;
        case 5:
             WeaponValue = "Epic";
             break;
        case 6:
             WeaponValue = "Legendary";
             break;
        default:
             WeaponValue = "Normal";
             break;
        
}
cout <<WeaponValue;
__________________
Warning: Have doubt in anything I post.

GCC 4.5.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101

Last edited by Dae; 09-05-2005 at 09:10 PM.
Dae is offline   Reply With Quote
Old 09-06-2005, 03:02 AM   #11
Registered User
 
Join Date: Aug 2005
Posts: 1,265
I compiled your program with VC++ 6.0 and got a lot of errors because functions failed to return values when function declaration says it is supposed to return them. You must fix the errors before attempting to run any executable that your compiler may produce.
Ancient Dragon is offline   Reply With Quote
Old 09-06-2005, 10:54 AM   #12
1479
 
Join Date: Aug 2003
Posts: 253
Quote:
Originally Posted by Ancient Dragon
I compiled your program with VC++ 6.0 and got a lot of errors because functions failed to return values when function declaration says it is supposed to return them. You must fix the errors before attempting to run any executable that your compiler may produce.
Thats odd, Dev-C++ doesn't give me any errors. Could you post the errors it gave you?
__________________
Knowledge is power and I want it all

-0RealityFusion0-
RealityFusion is offline   Reply With Quote
Old 09-06-2005, 11:20 AM   #13
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> I compiled your program with VC++ 6.0 and got a lot of errors
VC++ 6.0 is now so far removed from the current C++ standard and current practice that it's not really worth worrying about what it considers to be correct code.
Search the board for further discussions on this topic.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 09-06-2005, 12:34 PM   #14
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
1. You should only need to call srand once in your code.

2. You seem to have a function prototype-ish looking line of code in the function getWeaponStats (towards the bottom of the function code), i.e.:

Code:
int getMagicStats(string WeaponType, string WeaponValue,string WeaponName);
The arguments for this function differ from the class's own getMagicStats function:

Code:
class Weapons {
    public:
        Weapons();
        string getWeaponValue(); //Legendary, Epic, unique, rare, magical, normal
        string getWeaponType(); //Sword, mace, staff
        string getWeaponName(); //Name the weapon
        int getWeaponStats(); // damage, +mods and durability
        int getMagicStats();
        int getWeaponLevel(); // level of the weapon
        void whoCanDrop();  //check to see which mobs can drop it
    private:
        string WeaponName;
        string WeaponType;
        string WeaponValue;
        int WeaponStats;
        int WeaponDamage;
        int WeaponDurability;
        int WeaponLevel;
        char CanIDropIt;
};
What's going on with that?

3. You are apparently (?) missing some brackets around some code in the getMagicStats function:

Code:
case 3:
    if (Clvl == 1)
        WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
    if (Clvl >= 2)
        WeaponMod1 = (Clvl / 2) * (1 + rand() % WeaponLevel);
        WeaponMod2 = (Clvl * 2) * (1 + rand() % WeaponLevel);
        WeaponMod4 = (Clvl * 3) + (1 + rand() % WeaponLevel);
Is that supposed to be:
Code:
case 3:
    if (Clvl == 1)
        WeaponMod1 = Clvl * (1 + rand() % WeaponLevel);
    if (Clvl >= 2)
    {
        WeaponMod1 = (Clvl / 2) * (1 + rand() % WeaponLevel);
        WeaponMod2 = (Clvl * 2) * (1 + rand() % WeaponLevel);
        WeaponMod4 = (Clvl * 3) + (1 + rand() % WeaponLevel);
    } 
It's kind of hard to tell your intentions... perhaps there are supposed to be brackets, perhaps not. If your indentation/formatting was better in the post perhaps your intentions would be clearer.

There are some more places like that in the function for you to find.

4. You say your program crashes when run but compiles fine but you do not give us a complete program to run? It is impossible for us to run your program since it is incomplete... What types are Clvl, Str, WeaponMod1, WeaponMod2, WeaponMod3, WeaponMod4, WeaponMod5, WeaponMod6 declared as? Where is the code for StartGame, AnyKey? Where is the code for the class constructor?

5. Your general use/design of the class is odd. Typically, instead of calling the individual member functions as you have to set the variables of the class from within the main function, the member variables would be initialized by a group of "set" functions when the constructor is called, i.e. setWeaponName, setWeaponType, etc... The "get" group of functions would then be one-line functions that simply returned the class member variable in question and would likely be declared as const functions.

6. What others have already said... breaks need to be added where appropriate, and your functions need to return appropriate values.
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# Compile Time Debugging mindtrap C# Programming 4 09-17-2007 09:40 AM
Read and set\change system time Hexxx C++ Programming 9 01-02-2006 07:11 AM
getting massive compile errors, need help jlmac2001 C++ Programming 3 03-20-2003 04:05 PM
weird compile errors Leeman_s C++ Programming 2 01-02-2003 10:51 PM
Simple Compile Time Problem - HELP! kamikazeecows Windows Programming 2 12-02-2001 01:30 PM


All times are GMT -6. The time now is 01:41 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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