Okay, so I've been working on a program that will allow the user to input what items they want into an array and make the output look like this:

Item 1. Sword
Item 2. Shield
Item 3. Sword
Item 4. Armor

with some help, i was able to figute that part out. Now I need to make it so I can delete an item from the list. So if the user decides they don't want two swords, they can input 2 and it outputs:

Item 1. Sword
Item 2. Shield
Item 3. Armor

I've tried the delete function and that showed no results. So at the moment, I can only think of changing everything into a vector, but i was hoping I could do something that didn't need to redo what i've already done. Pease help.

Once you enter an value in an array it's there for the duration unless you change the value. One option you have is to keep track of the number of valid values in the array using another variable. All the valid values in the array will be at the indexes equal to (or less depending how you set it up) the number of valid values. All values above that index aren't valid any longer, even though they are still in the array. So lets say yous start with the values you have posted. You can overwrite the value sword at index 2 with the value armor. The array will then contain:
sword
shield
armor
armor
and the number of valid values would be decreased from 4 to 3 or the last valid index, if you prefer to keep track of that, is 2.

To do this you would need to shift all values with an index larger than the one you want to overwrite to left by one and decrease either the variable keeping track of the number of valid values or the index of the last valid value.

If you are doing this actioin very often in the program then a list may be a option you want to look into. Vectors can take the burden of doing the shifts, keeping track of the number of valid values, etc for you if you wish.

OK, i see what you mean, but i'm having a ard time putting it into practice. let me show you what i have so far:

#include <iostream>
#include <iomanip>


using namespace std;

const int NumItems = 7;
const int StringSize = 8;
int NumOfUserItems=0;
char userItems[10];
int choice = 0,
    Shown=0,
    count = 0,
    inList=0,
    KeepToss = 0,
    ItemOut=0;
char valid[2][2] = {"T", "F"};
char Items[NumItems][StringSize] =
          { "Sword", "Shield", "Helmet", "Armor", 
            "Potion", "Ether", "Elixir"};


    
int main()
{
  int YesNo = 1;
  double count = 0;
  cout << "Welcome to the inn, before you set out on your adventure, \nyou'd best "
       << "gather your equipment. \n What would you like to do?\n 1. Add Item\n"
       << " 2. Remove Item\n 3. Swap Item\n 4. Quit\n";
  cin >> YesNo;
  while (YesNo == 1)
  {

    cout << "Ok what would you like to pack?\n";
    cout << " 1. Sword \n 2. Shield \n 3. Helmet \n 4. Armor \n 5. Potion \n 6. Ether \n 7. Elixir\n";
     cin >> choice;
     
     

      if ((choice != 1) && (choice != 2) && (choice != 3) &&
         (choice != 4) && (choice != 5) && (choice != 6) && (choice != 7))
     {
      cout << "I'm sorry, what was that? ";
      cin >> choice;
     } 
     else
     {
         userItems[NumOfUserItems] = (choice-1);
         cout << "alright, i'll put one in your pack. \n";
}
    NumOfUserItems++;
     cout << "Your pack contains the following: \n";
    for (int index = 0; index < NumOfUserItems; index++)
    {
        if (valid[index] == "T") 
        {
        cout << "item " << (index + 1) << ". " << Items[userItems[index]] << endl;
        }
    }  
    if (NumOfUserItems == 10)
    {
     cout << "your pack is full now.\n";
     YesNo=2;
    }
    else
    {
    cout << "What else would you like to do?\n 1. Add Item\n"
         << " 2. Remove Item\n 3. Swap Item\n 4. Quit\n";
    cin >> YesNo;
}
}
while (YesNo == 2)
{
      cout << "OK, what would you like to remove?\n";
      for (int index = 0; index < NumOfUserItems; index++)
    {
        if (valid[index] == "T") 
        {
        cout << "item " << (index + 1) << ". " << Items[userItems[index]] << endl;
        }
    }  
      cin >> choice;
      cout << "OK, you want to remove a " << Items[userItems[choice-1]] << " correct?\n 1. Yes\n 2. No\n";
      cin >> KeepToss;
      if (KeepToss == 1)
      {
       valid[choice-1] = "F";
       cout << "OK... Done.\n Your pack now contains\n";
       for (int index = 0; index < NumOfUserItems; index++)
    {
        if (valid[index] == "T") 
        {
        cout << "item " << (index + 1) << ". " << Items[userItems[index]] << endl;
        }
    }  
        
      cin >> YesNo;
}
}

if (YesNo == 4)
{
 cout << "Alright. Have a safe journey\n";
}

  system ("pause");
  return 0;                 
}

I know that everything works up until line 67, that's when things get messed up. also note, that I'm trying anything I can to fix it (though most/all of it hasn't worked)

const int MAXSIZE = 4;
int mine[MAXSIZE] = {1, 2, 3, 4};
int actualSize = 4;
int target = 2;
int targetIndex = -1;
//goal, find the 2 and overwrite it by shifting the 3 and 4 to the left by one, leaving final values in mine of 1, 3, 4, 4 and actualSize of 3

//first find first index of element with value of 2
for(int i = 0; i < MAXSIZE; ++i) 
{
  if(mine[i] ==  target)
  {
      targetIndex = i;
      break;
   }
}

//next shift elements with indexes above value of targetIndex to left by 1
for(int j = targetIndex; j < actualSize - 1; ++j)
  mine[j] = mine[j + 1];

//decrease actualSize by 1
--actualSize;

//display result using actualSize
for(int k = 0; k < actualSize; ++k)
  cout << mine[k] << ' ';

cout << endl;

//display mine using MAXSIZE
for(int m = 0; m < MAXSIZE; ++m)
  cout << mine[m] << ' ';

Ok, I'm fairly certain that I have everything the right way, my only problem is this error message:

ISO C++ forbids assignment of arrays
it says this at the "mine[j] = mine[j + 1];" point (note: I didnt copy your words, I fixed them to use my variables). the rest of the program is error free.

If you're referring to your array Items, then you've got to use the strcpy( ) function to copy from one index (a row of your 2D array) to another.

This line

if ((choice != 1) && (choice != 2) && (choice != 3) &&
         (choice != 4) && (choice != 5) && (choice != 6) && (choice != 7))

makes me cringe! :ooh:
Why ask seven questions when two will do?

if( choice < 1 || choice > 7 )

Ah HA! my thanks to you both, (especially with that stupid if statement). Now I an finally get his progam to work for me.
Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.