Home > OS >  Assigning an Array and a string to a function and compare them
Assigning an Array and a string to a function and compare them

Time:08-26

Hello I just do not get it further. I would like to pass a variable string to a function and compare it with an array which i filled b4. The problem is that i dont know how I can pass all values of the candidates array to the function : I can just pass a single string to the function. In my Code that would be the 0.

I would like to compare the candidates with the user inputed name in the function call in line 148. Can anybody tell me how I have to define the function in order to be able to do that? Or is it even possible to do it in this manner? Thank you really much!

https://godbolt.org/z/Evcjfxn75

CodePudding user response:

Take a look at the AnyOf function from the algorithm library. With it, you can apply the vote function to each array element, not only the first one.

Pseudocode:

if (std::all_of(candidates.begin(), candidates.end(), vote) {
      ...
}

CodePudding user response:

You could do this using global variables:

static std::string local_string = "Fred";
void Assign_Local_Variable(const std::string& new_value)
{
  local_string = new_value;
}

Another possibility is to use a static local variable:

void Update_String(const std::string& new_value)
{
    static std::string variable_in_function = "Taco";
    variable_in_function = new_value;
}

The variables will be available after execution leaves the functions.

  •  Tags:  
  • c
  • Related