I'm a new at C++ and need some help with this code. I searched but maybe because I'm not familiar with the correct vocabulary for C++, I couldnt' find an answer for this.

What I'm trying to do is pass a string into a function that is looking for an array.
Probably easier for me to show the code then write it out.

Code begins with AppMain()...

Let's say I have this declaration and 4 arrays:

char * theCategory;

char* arrCats[]={"fruits","veggies","colors"};
char* fruits[]={"Apple","Banana","Berry"};
char* veggies[]={"Asparagus","Bell Pepper", "Carrot","Corn"};
char* colors[]={"Aqua","Black","Blue","Brown","Gray"};

Then I have these 3 functions:

void getCatArrAndVals (char* arg[]) 
{
 //do something
}

char * LoadCategory()
{
	srand( time(NULL) );
	int RandIndex = rand() % 3;
	theCategory=arrCats[RandIndex];  //category I'm interested in
	getCatArrAndVals(colors);  //I'm passing the hard-coded value of "colors" array
                                    //here but I really want to pass theCategory
	return theCategory;
	}

void AppMain()
{
	LoadCategory();
}

How do I pass the string value into the getCatArrAndVals functions as an array?

Recommended Answers

The function wants all of the strings, not just one of them. If you want to pass just a single string then you will have to change the signature of the function to getCatArrAndVals(char *s)

Jump to Post
theCategory=arrCats[RandIndex];  //category I'm interested in
	getCatArrAndVals(colors);  //I'm passing the hard-coded value of "colors" array
                                    //here but I really want to pass theCategory

The problem with that idea is that colors is a collection of things and theCategory is just a thing. Your function needs to …

Jump to Post

All 7 Replies

The function wants all of the strings, not just one of them. If you want to pass just a single string then you will have to change the signature of the function to getCatArrAndVals(char *s)

I'm not sure I made myself clear: I actually DO want to pass the array (with all the strings) and not just one string. For example, if the chosen category is string: "colors," then I want to pass the "colors" array and the values it holds.

theCategory=arrCats[RandIndex];  //category I'm interested in
	getCatArrAndVals(colors);  //I'm passing the hard-coded value of "colors" array
                                    //here but I really want to pass theCategory

The problem with that idea is that colors is a collection of things and theCategory is just a thing. Your function needs to take either a collection or just a thing - but not both. I'd suppose that you may be trying to do the following:

char* arrCats[]={"fruits","veggies","colors"};
char* fruits[]={"Apple","Banana","Berry"};
char* veggies[]={"Asparagus","Bell Pepper", "Carrot","Corn"};
char* colors[]={"Aqua","Black","Blue","Brown","Gray"};
char **groups[] = {arrCats,fruits,veggies,colors}

// and later ...
char ** theGroup = groups[rand() % 4];

Although, with all that pointer nonsense it might be easier to just use std::string and std::vector . For example

std::vector< std::string > colors;
colors.push_back ("Aqua");
colors.push_back ("Black"); 
// and so on for each group you have above

std::vector< std::vector< std::string > > groups

Then you could still get at any of those groups with groups[rand() % 4] but the pointer mess would be gone.

Thank you for the response, however, when I tried the first example, I was getting back values like "Apple" and other items in the array instead of just the array names (i.e. - Colors, Veggies, Fruits). I suspect the 2nd example will do the same thing. I guess I can always go with a case/switch but it seems so clunky. I was hoping there was a more suave way of doing this.

Just learned that you can't use case/switch with strings in C++. So... used if/elseif:

if (theCategory=="colors") getCatArrAndVals(colors);
else if (theCategory == "fruits")  getCatArrAndVals(fruits);
else if (theCategory == "veggies")  getCatArrAndVals(veggies);

If anyone has a sleeker way of doing this, please let me know.

TIA

Just learned that you can't use case/switch with strings in C++. So... used if/elseif:

if (theCategory=="colors") getCatArrAndVals(colors);
else if (theCategory == "fruits")  getCatArrAndVals(fruits);
else if (theCategory == "veggies")  getCatArrAndVals(veggies);

If anyone has a sleeker way of doing this, please let me know.

TIA

Sure

#include <map>
#include <string>
#include <vector>
#include <iostream>
using namespace std;


typedef std::vector<std::string> StringArray;
typedef void(*FuncPtr)(const StringArray& values);


void getCatArrAndVals(const StringArray& values){
 //do something useful
 for(size_t i = 0; i < values.size(); ++i)
	 cout << values[i] << "\n";
}

struct WrappedFunc{
 StringArray savedArg; //argument to be passed to mappedFunc
 FuncPtr mappedFunc; //mappedFunc to be called
 WrappedFunc(){}
 WrappedFunc(const StringArray& arg, const FuncPtr& fp)
 	 : savedArg(arg), mappedFunc(fp){}
 void operator()(){
	 (*mappedFunc)(savedArg);
 }
};

StringArray createFruits(){
	const char* fruits[]={"Apple","Banana","Berry"};
	return StringArray(fruits,fruits + sizeof(fruits)/sizeof(*fruits));
}
StringArray createVegetables(){
	const char* veggies[]={"Asparagus","Bell Pepper", "Carrot","Corn"};
	return StringArray(veggies, veggies + sizeof(veggies)/sizeof(*veggies));
}
int main(){
 typedef std::map<string,WrappedFunc> StringMappedCallback;
 StringMappedCallback callbacks;
 StringArray fruits = createFruits();
 StringArray veggies = createVegetables();
 callbacks["fruits"] = WrappedFunc(fruits,getCatArrAndVals);
 callbacks["veggies"] = WrappedFunc(veggies,getCatArrAndVals);

 string pickedCatagory = "veggies"; //get input from somewhere
 callbacks[pickedCatagory]();
}

Thats the general idea, but of course you would use something like boost::Function adapted to it.

Thank you for this. It will be a few days before I have a chance to play with this again but what you wrote looks like just what I needed.

Be a part of the DaniWeb community

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