C++ Code I will Never write Again...

Thursday, 3 April 2008 08:04 by frimbob
   1: void Markets::RemoveItem(AUser au)
   2: {
   3:     char YN;
   4:     int key;
   5:  
   6:     try
   7:     {
   8:         //ask for an item
   9:         cout << "please select an item to delete" ;
  10:         cin >> key;
  11:  
  12:         //cast reference to item
  13:         item* ite =(item*)lst.at(key);
  14:         //cout << ite-> << endl; 
  15:         if(ite->MaxDiscount < 0){ throw 'a'; };//end if
  16:         cout << "delete this item " << ite->Name  <<"? (y/n): ";
  17:         //following was inspired by 
  18:         //http://www.augustcouncil.com/~tgibson/tutorial/iotips.html#problems
  19:         int bad_input;
  20:         do{
  21:             bad_input=0;
  22:             std::cin >> YN;
  23:             if(!std::cin)
  24:             {
  25:                 bad_input=1;
  26:                 std::cin.clear();
  27:                 std::cin.ignore();
  28:                 
  29:             }
  30:  
  31:         }while(bad_input);
  32:         //end if inspiration
  33:                         if(YN == 'y')
  34:         {
  35:             //remove the item
  36:             cat->removeproduct(ite);
  37:         };//end if
  38:  
  39:  
  40:     }
  41:     catch(...)
  42:     {
  43:         try
  44:         {
  45:             //cast reference to catagory
  46:             catagory* tcat = (catagory*)lst.at(key); 
  47:             if(tcat->subcatagories.size()) throw 'b';
  48:  
  49:             cout << " delete this catagory " << tcat->name <<"? (y/n) " << endl;
  50:             //following was inspired by 
  51:             //http://www.augustcouncil.com/~tgibson/tutorial/iotips.html#problems
  52:             int bad_input;
  53:             do{
  54:                 bad_input=0;
  55:                 std::cin >> YN;
  56:                 if(!std::cin)
  57:                 {
  58:                     bad_input=1;
  59:                     std::cin.clear();
  60:                     std::cin.ignore();
  61:                 }
  62:  
  63:             }while(bad_input);        
  64:             //end of inspiritation
  65:                                     if(YN == 'y')
  66:             {
  67:                 //the class value cat is the parent pass this object to delete
  68:                 cat->removecatagory(tcat);
  69:             };//endif
  70:  
  71:  
  72:         }
  73:         catch (...)
  74:         {
  75:             cout << "item could not be removed press u and  to continue:" << endl;
  76:             char temp;
  77:             cin >> temp;
  78:         };//end catch
  79:  
  80:     };//end catch
  81:  
  82: };//end of remove item
 

So why did I did I create this dam thing well, I was inspired by AJAX object creation if you believe that. Since there is no function that can return the correct browser type a try loop is used until the correct object name is reached

A similar problem, I had a vector of void* pointers that need to be cast into the correct type before they could be used,the vector contained 2 different types of pointers of the item* pointer and a category* pointer, obviously the typedef would not return a correct value for a void* pointer so I had to use this ugly piece to determine the type.

My advice about University projects don't start them days before there due especially when your tired and stressed and if you wish to create a list of mixed objects, don’t use a mixed list of pointers.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   Tech Tips
Actions:   E-mail | Permalink | Comments (795) | Comment RSSRSS comment feed

Comments

Comments are closed