1

I finished my lab problem, but I have a quick question to fix the end. I have a vector in the function that needs to be returned to main so I can output the elements of the vector. I put return a; at the end of the function since a is the name of the vector in the function but I'm getting an error.

*Where it says "cout << the names are " should be in the main but I can't figure out what to put in the return. *I also put return 0 because it was the only way I had the whole program working since the output is also in the function, but I need it back in main and change the return 0; Sorry if this is a bad question I'm still learning, thanks.

string switching(vector<string> a, int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (a[i] > a[j]) {
                swap(a[i], a[j]);
            }
        }
    }

    cout << "The order of names are...\n";
    for (int i = 0; i < n; i++) {
        cout << a[i] << "\n";
    }

    return 0;
}
2
  • You could change the signature to return a vector<string> instead then return a; would work. Commented Apr 26, 2019 at 2:40
  • You return the thing that you want to return. return a; gives you an error because a is vector<string> but you put the return type as string. How would you fix that? Commented Apr 26, 2019 at 2:47

2 Answers 2

1

As has been suggested, you can change the function signature to

std::vector<std::string> switching(std::vector<std::string> a, int n)

Or, you could pass the string vector argument by reference:

void switching(std::vector<std::string>& a, int n)

This shows a main calling the first version:

#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> switching(std::vector<std::string> a, int n) {
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] > a[j]) {
        swap(a[i], a[j]);
      }
    }
  }
  return a;
}

int main()
{
  std::vector<std::string> strings{
    "John",
    "Fred",
    "Alice"
  };

  auto sorted = switching(strings, strings.size());
  std::cout << "The order of names are...\n";
  for (auto const& name : sorted) {
    std::cout << name << "\n";
  }

  return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much for the help, I appreciate your time helping me
Also quick question, my lab HW was to get a list of names up to 20 max and return them in alphabetical order, do you think I should change it from vectors to dynamic arrays or Vectors are fine? Thanks again!
Vectors are dynamic arrays. You may be able to stick with the vectors, You will have to guage that.
If the answer works for you, go ahead and accept it. Let me know if you have any further questions.
yes just accepted, I forgot to do it yesterday. sorry about that
0

1.You could modify the return type of the function;

   vector<string> switching(vector<string> a, int n)
{
     //Your core-code here;
     return a;    
}
  1. The parameters could be pass by reference.
void switching(vector<string> &a, int n)
{
     //Your core-code here;

}

In this way, the parameters could change simultaneously in main function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.