Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

#OOP (Some High Number) Advanced (Uses STL)

Think you can spy out that little nasty bug? Try debugging some code in here

#OOP (Some High Number) Advanced (Uses STL)

Postby Argio on Fri Feb 27, 2009 2:58 pm

Stop the problem... Compilers will not catch this.

Code: Select all
// Main.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <string>
#include <iostream>
#include <vector>
#include <conio.h>
#include <sstream>

using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::stringstream;

bool intToStr( string &str, int n );

void handleErrorThings();




class cItem
{
protected:
   int duribility;
   string itemdescription;
   string itemname;
   int weight;
   string type;
public:
   cItem(string lol, string desc, int ww, int duri, string tt)
   {
      itemname = lol;
      duribility = duri;
      itemdescription = desc;
      weight = ww;
      type = tt;
   }
   virtual const string sGetDesc()
   {
      return itemdescription;
   }
   const string sGetName()
   {
      return itemname;
   }
   const int iGetWeight()
   {
      return weight;
   }
   const int iGetDuribility()
   {
      return duribility;
   }
   const string sGetType()
   {
      return type;
   }
};
class cWeapon : public cItem
{
private:
   int damage;
public:
   cWeapon(string nn, int dd,  int weight, int duri, string desc, string type) : cItem(nn, desc, weight, duri, type)
   {
      damage = dd;
   }
   const int iGetDamage()
   {
      return damage;
   }

   virtual const string sGetDesc()
   {
      string conver = "";
      intToStr(conver, damage);
      string newdesc = itemdescription + "\n  Damage: " + conver;
      return newdesc;
   }
};

class cArmor : public cItem
{
private:
   int armor;
public:
   cArmor(string nn, int aa, int ww, int duri, string desc, string tt) : cItem(nn, desc, ww, duri, tt)
   {
      armor = aa;
   }
   const int iGetArmor()
   {
      return armor;
   }
   virtual const string sGetDesc()
   {
      string conver = "";
      intToStr(conver, armor);
      string newdesc = itemdescription + "\n  Armor: " + conver;
      return newdesc;
   }
};
   

   

int main(int argc, char *argv[])
{
   cin.exceptions(cin.failbit);
   try
   {
   
   vector<cItem*> itemSYSTEM;
   while(true)
   {
   
   system("cls");
   cout << "Item MENU" << endl << endl;

   cout << "[1] Make a new item" << endl;
   cout << "[2] Delete an item" << endl;
   cout << "[3] View all items" << endl;
   cout << "[4] Quit" << endl;
   
   char x;
   do x = _getch(); while(x != '1' && x != '2' && x != '3' && x != '4');

   switch(x)
   {
   case '1':
      {
      system("CLS");
      cout << "Choose a type of item" << endl;
      cout << "________________________" << endl;
      cout << endl;
      cout << "  [1]Armor" << endl;
      cout << "  [2]Weapon" << endl;
      cout << "  [3]Other" << endl;

      char lol;
      do lol = _getch(); while(lol != '1' && lol != '2' && lol != '3' && lol != '4');
      
         switch(lol)
         {
         case '1':
            {
               system("CLS");
               cout << "Armor Creation Menu" << endl;

               string name = "";
               cout << "Please enter the name of the item:" << endl;
               getline(cin, name);

               int Armor = 0;
               cout << "Please enter the amount of armor:" << endl;
               cin >> Armor;

               int Duribility = 0;
               cout << "Please enter the duribility of the armor:" << endl;
               cin >> Duribility;

               int Weight = 0;
               cout << "Please enter the weight of the armor:" << endl;
               cin >> Weight;
   
               cin.sync();
               string Itemdesc = " ";
               cout << "Please enter the item description:" << endl;
               getline(cin, Itemdesc);

               cout << endl;
               cout << endl;
               cout << "Thank you for creating the item." << endl;

               itemSYSTEM.push_back( new cArmor(name, Armor, Weight, Duribility, Itemdesc, "Armor"));
               system("PAUSE");

            }
            break;

         case '2':
            {
               system("CLS");
               cout << "Weapon Creation Menu" << endl;
               string name = "";
               cout << "Please enter the name of the item:" << endl;
               getline(cin, name);

               int Damage = 0;
               cout << "Please enter the amount of damage:" << endl;
               cin >> Damage;

               int Duribility = 0;
               cout << "Please enter the duribility of the weapon:" << endl;
               cin >> Duribility;

               int Weight = 0;
               cout << "Please enter the weight of the weapon:" << endl;
               cin >> Weight;
               
               cin.sync();
               string Itemdesc = " ";
               cout << "Please enter the item description:" << endl;
               getline(cin, Itemdesc);

               cout << endl;
               cout << endl;
               cout << "Thank you for creating the item." << endl;

               itemSYSTEM.push_back( new cWeapon(name, Damage, Weight, Duribility, Itemdesc, "Weapon"));
               system("PAUSE");
            }
            break;

         
         case '3':
            {
               system("CLS");
               cout << "Other Item Creation Menu" << endl;
               string name = "";
               cout << "Please enter the name of the item:" << endl;
               getline(cin, name);

               int Duribility = 0;
               cout << "Please enter the duribility of the item:" << endl;
               cin >> Duribility;

               int Weight = 0;
               cout << "Please enter the weight of the item:" << endl;
               cin >> Weight;

               cin.sync();
               string Itemdesc = " ";
               cout << "Please enter the item description:" << endl;
               getline(cin, Itemdesc);

               cout << endl;
               cout << endl;
               cout << "Thank you for creating the item." << endl;

               itemSYSTEM.push_back( new cItem(name, Itemdesc,  Weight,  Duribility, "Other"));
               system("PAUSE");
            }
            break;

         default:
            cout << "ERROR" << endl;

         }
      }
      break;

      case '2':
         {
            system ("CLS");
            cout << "Delete Menu" << endl << endl;
            if(itemSYSTEM.size() == 0){
               cout << "There are no items to be displayed." << endl;
               cin.sync();
               cin.get();
            }
            else
            {
               cout << "  Below you will find a list of all of the items." << endl;
               cout << "Please choose one of them to delete." << endl;
               cout << "__________________________________________________" << endl << endl;

               int i = 0;
               for(; i < itemSYSTEM.size(); i++)
               {
                  cout << "_______________________" << endl;
                  cout << i+1 << "." << endl;
                  cout << "  Name: " << itemSYSTEM[i]->sGetName() << endl;
                  cout << "  Type: " << itemSYSTEM[i]->sGetType() << endl;
                  cout << "  Duribility: " <<  itemSYSTEM[i]->iGetDuribility() << endl;
                  cout << "  Description: " << itemSYSTEM[i]->sGetDesc() << endl;
                  cout << "  Weight: " << itemSYSTEM[i]->iGetWeight() << endl;
                  cout << "_______________________" << endl;
               }
               cout << endl << endl;
               
               int itemz;
               cout << "Please choose which item you would like to delete." << endl;
               cin >> itemz;
               if(itemz < 1) break;
               else if(itemz > itemSYSTEM.size()) break;
               else
               {
               delete itemSYSTEM[itemz -1];
               itemSYSTEM.erase( itemSYSTEM.begin() + (itemz - 1));
               }
            }
            
         }
         break;
      case '3':
         {
            system("CLS");
            cout << "  Below you will find a list of all of the items." << endl;
               cout << "__________________________________________________" << endl << endl;

               int i = 0;
               for(; i < itemSYSTEM.size(); i++)
               {
                  cout << "_______________________" << endl;
                  cout << i+1 << "." << endl;
                  cout << "  Name: " << itemSYSTEM[i]->sGetName() << endl;
                  cout << "  Type: " << itemSYSTEM[i]->sGetType() << endl;
                  cout << "  Duribility: " <<  itemSYSTEM[i]->iGetDuribility() << endl;
                  cout << "  Description: " << itemSYSTEM[i]->sGetDesc() << endl;
                  cout << "  Weight: " << itemSYSTEM[i]->iGetWeight() << endl;
                  cout << "_______________________" << endl;
               }
               cout << endl << endl;
               cout << "Please press any key to return to the main menu." << endl;
               _getch();
         }
         break;

      case '4':
         {
            return 0;

         }

   }

   


   
   
   }
   return 0;

   }catch(...)
   {
      handleErrorThings();
   }
   
}

bool intToStr( string &str, int n )
{
    stringstream ss;

    //this copies str into ss.
    ss << n;
   
    //This copies data in ss into n, converting it to the correct type at the same time.
    //If the process succeeds the function will return true.  If it fails the function will
    //return false.
    return ( ss >> str );
}

void handleErrorThings()
{
   cerr << "Um ERROR ON UR INPUT" << endl;

   char BadInput[50];
   cin.sync();
   cin.clear();
   cout << "Press any key to return to the main menu." << endl;
   _getch();
}



I am C++ trainer dog testing your skizzilz.
I love C++, for reasons I have yet to discover.
All the haters love me so lovers stop hating.
Image
User avatar
Argio
 
Posts: 21
Joined: Fri Jun 20, 2008 5:04 am
Location: Utah. I am jewish.

Re: #OOP (Some High Number) Advanced (Uses STL)

Postby C++ on Wed Mar 11, 2009 2:20 pm

This won't even compile. You forgot to put in
Code: Select all
using std::cerr;
Please check your code before posting it as a debug challenge.

Your exception handler is not placed within the while(true) loop of your main program. This will obviously result in the program exciting, instead of what it should do, that is returning to the main menu.

Member function size() of std::vector returns a size_type value which is unsigned. In this case it does not lead to problems, but coding like this will sooner or later lead to problems. It's not recommended to compare signed with unsigned values.

Using operator[] to dereference a vector is a bad idea. operator[] will not throw an exception and therefore can go unnoticed by your program.

The attributes of an item can be negative. Although this could be intended, I can't think of a reason why you'd do that.
For the rest I found nothing wrong while adding/deleting items.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: #OOP (Some High Number) Advanced (Uses STL)

Postby Argio on Thu Mar 19, 2009 7:30 pm

Thank you for replying, the "cerr" was not intentional... Sorry about that.

If you did not notice, the problem was a memory leak. That's what it was supposed to be. Using the operator[] was how I was taught. Google isn't always a reliable source... If antiRTFM gets his mic and starts making videos again, I hope he covers link lists and multi-maps.
Since creating this code, I have learned to use iterators instead for accessing variable(s)(pointers in this case) in a vector.

Also, for the attributes of an item being negative I was trying to not worry about the 'usability' of this item system for a game; it was for the main purpose of showing a memory leak. For that matter I could have easily done this:
Code: Select all
#include <iostream> // Standard I/O header for console applications written in C++ (STD)
#include <vector> // STL header for vectors (STD(STL))

using std::vector;
using std::cout;
using std::cin;
using std::endl;

class cClassUsedForVector // A class used for the vector of pointers;
{
private:
      vector  <int> RAWR;
public:
       cClassUsedForVector()
       {
                  for(int iter = 0; iter < 20; iter++) // Add some values to the vectors to show that there is a memory leak.
                  {     
                         RAWR.push_back(iter); // Add values to the vector.
                  }
       }
     
};

int main()
{
    cout << "Program..." << endl;
   
    bool nquit = true;
    int i = 0;
    vector <cClassUsedForVector*> x;
    char yn = 'y';
   
    while(nquit)
    {
    i++;
   
    cout << "Amount of vectors: " << endl << i << endl << endl;
    cin.sync();
    cin.get();
   
    x.push_back( new cClassUsedForVector);
   
    cout << "Contiue? [y] Yes [n] No" << endl;
    cin >> yn;
    if(yn=='n'||yn=='N') nquit=false;
    else if(yn=='y'||yn=='Y') nquit=true;
   
   
    }
   
   
    return 0;
}



Thank you for showing me my other "Errors", but it was a memory leak that was the problem I intended for you to find, thank you.
I love C++, for reasons I have yet to discover.
All the haters love me so lovers stop hating.
Image
User avatar
Argio
 
Posts: 21
Joined: Fri Jun 20, 2008 5:04 am
Location: Utah. I am jewish.


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests