   How do I get rid of junk in c-string?
  char description[51];  // The item description // Private member function.   void createDescription(int size, char *value) { // Allocate the default amount of memory for description.    // Store a value in the memory.   strcpy(description, value); }InventoryItem(char *desc, double c, int u) { // Allocate memory and store the description.   createDescription(strlen(desc), desc);    // Assign values to cost and units.   cost = c;   units = u; } InventoryItem inventory[NUM_ITEMS] = {InventoryItem("Hammer",6.95,12),InventoryItem("Wrench",8.75,20),InventoryItem("Pliers",3.75,10),InventoryItem("Ratchet",7.95,14),InventoryItem("Screwdriver",2.50,22) };this code is causing my c-string to look like this"ÌÌÌÌÌÌÌÌHammer"Instead of "Hammer"How do I get rid of the junk in the front?   
  This looks to me like C++.Why aren't you using std::string.   ;  because the book wants me to use c-string   ;  well, generally what you're showing is related to skrewing up your pointers somewhere. With the limited amount of code you've pasted it's hard to say where you've skrewed up though.   ;  It's probably a really simple mistake.The code you posted seems ok.  How do you know that the string is that crazy value.  Are you looking at it in the debugger?  Or printing it out?If looking at it in the debugger are you in debug mode?  As opposed to release mode, which you shouldnt be in.If printing it out, can you show us the code that you are using to print it out?   ;Main codeint main(){  const int NUM_ITEMS = 5;  int Amount_Item = 0;  char name[55];  InventoryItem inventory[NUM_ITEMS] = {InventoryItem("Hammer",6.95,12),InventoryItem("Wrench",8.75,20),InventoryItem("Pliers",3.75,10),InventoryItem("Ratchet",7.95,14),InventoryItem("Screwdriver",2.50,22) };  cout << setw(14) <<"Inventory Item"   << setw(8) << "Cost" << setw(8)   << setw(16) << "Units On Hand\n";   cout << "-------------------------------------\n";   for (int i = 0; i < NUM_ITEMS; i++)   { cout << setw(14) << inventory.getDescription(); cout << setw(8) << inventory.getCost(); cout << setw(7) << inventory.getUnits() << endl;   }   CashRegister register1;  cout<<"What item do you want to buy? ";  cin.getline(name,55);  for (int i = 0; i < NUM_ITEMS; i++)  { while(!inventory[NUM_ITEMS].CheckItem(name))   {  cout<<"Item does not exist. Please reinput item name. ";  cin>>name;   }  }  register1.setDescription(name);system("PAUSE");return 0;}the classclass InventoryItem{private:   char description[51];  // The item description   double cost;   // The item cost   int units;// Number of units on hand // Private member function.   void createDescription(int size, char *value) { // Allocate the default amount of memory for description.    // Store a value in the memory.   strcpy(description, value); }public:   // Constructor #3   InventoryItem(char *desc, double c, int u) { // Allocate memory and store the description.   createDescription(strlen(desc), desc);    // Assign values to cost and units.   cost = c;   units = u; }  // Destructor   ~InventoryItem() { delete [] description; }   // Mutator functions/*   void setDescription(char *d)  { strcpy(description, d); }*/   void setCost(double c) { cost = c; }    void setUnits(int u) { units = u; }   // Accessor functions   const char *getDescription() const { return description; }   bool CheckItem(char *item)   {   if(item == description)   {   return true;   }   else    return false;   }  double getCost() const { return cost; }   int getUnits() const { return units; }};I am using visual C++ 2010 express and checkItem will not return true even though it is. And I checked when debuging it what the vaule was and it gave me that vaule. And it is compiled for debug also and not realease.   ;  Your check item function is comparing two pointers. Those will never be equal the way you think they are. This is why we recommend you use C++ strings, aka std::string, over C strings. Because they behave more like what you would expect.In general, if you have a C-string in your code, you have a bug.   ;  Also, if you do stick to c strings, you're deleting description in the destructor when you don't need to.  All the data you're effectively using are static literal strings "hammer", "pliers", etc, and even though you're using pointers to the variables that hold them, these do not need to be (and shouldn't be) deleted.  You only need to delete/delete [] data you have created on the heap with 'new' (what the compiler is doing for you is creating your string data on the stack).Also also, I don't see the setDescription method there, but you've only allocated 51 chars for the description, and you're potentially copying 55 characters into that variable, this will probably cause unpredictable results, worse case your program will crash with an unhandled exception.  Although in debug, the compiler pads variables to ensure you don't write over memory outside of your data size - this can be a common cause for release-only crashes.   ;Quote:Original post by kingpinzsbecause the book wants me to use c-stringIs this for a class? Or just something you're doing on your own?   ;Quote:Original post by kingpinzsbecause the book wants me to use c-stringWell that sucks.  Is it Starting Out with C++: From Control Structures through Objects (6th Edition) by Tony Gaddis?  That's odd, it actually has excellent reviews on Amazon.C++ strings are much more intuitive, and I'd recommend rewriting the code to use them when you're done with this assignment, just to see how easy it would have been.  In the meantime, have a look at strcmp.   