Hi

I have an array which contains a number and string in each element - itemClass.itemID and itemClass.itemName. the two combined making up an inventory

I am trying to build a function which uses a for loop to take each itemID and concatenate them all together to produce a string of all the item numbers

Any hints or ideas as to what functions to use??

I have tried to convert the int into a char and then concatenate them all together on each iteration of the for loop but have had little success

Thanks

Can we see the define of the structure?

OK
i have given up with the conersion of int to char - save that for a rainy day
i have this function

string inventoryContents="Current items in inventory: ";


    for (int counter=0;counter<INVENTORYLIMIT;counter++)
    {
        string contentsToAdd=inventory[counter].itemName;
        strcat (inventoryContents, contentsToAdd);
    }

but it is not working :s

Are you talking about something like below?

#include <iostream>
#include <string>
#include <cstdio>

#define ARR_SIZE 4

struct mystr
{
  std::string mystr;
  int myint;
};

int main()
{
  char *str = new char[10];
  str[0] = '\0';
  std::string ans;
  struct mystr thestr[ARR_SIZE];
  
  thestr[0].myint = 1;
  thestr[1].myint = 2;
  thestr[2].myint = 3;
  thestr[3].myint = 4;
  
  thestr[0].mystr = "the first";
  thestr[1].mystr = "the second";
  thestr[2].mystr = "the third";
  thestr[3].mystr = "the fourth";
  
  for (std::string::size_type i = 0; i < ARR_SIZE; ++i)
  {
    sprintf(str, "%d", thestr[i].myint);
    ans += (std::string)str + " ";
  }
  
  std::cout << "ans->" << ans << std::endl;
  return 0;
}

Sort of, but ive simplified what i want to do now.

basically, i have an array where each element contains itemID and itemName.
i am trying to write a function where a for loop can gather each itemName - there is an arraysize of 5 - and put them all in one string that can be shown on screen.

This is what i came up with

string inventoryContents="Current items in inventory: ";


    for (int counter=0;counter<INVENTORYLIMIT;counter++)
    {
        string contentsToAdd=inventory[counter].itemName;
        strcat (inventoryContents, contentsToAdd);
    }

i have included the library #include <cstring> so i can use strcat - but when i run it i get the following error messages
error: no matching function for call to `strcat(std::string&, std::string&)'|

Try using something like:

std::string mystr = "the string";
mystr.c_str();//to return a C style string

If your using strings...why won't you just

string contentsToAdd += inventory[counter].itemName + " ";

hi

i had no idea you could do that in C++, in C i was used to using strcat e.t.c
i have the following code working

string characters::viewAllItems()
{

  string inventoryContents="Current items in inventory: ";



    for (int counter=0;counter<itemsInInventory;counter++)
    {
        inventoryContents+=inventory[counter].itemName + " " ;

    }
        
        return inventoryContents;

}

the only problem is, it wont print on the screen]

ah my mistake, i was missing a cout in main - all working now! thanks for everyones help!

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.