2
int multif(std::vector<int> &catcher)
{
    printf("\nThe array numbers are:  ");
    for (int i = 0; i < catcher.size(); i++)
    {
        catcher[i]*=2;
        //printf("%d\t", catcher[i]);
        return catcher[i]; 
    } 
}

Can someone help me to understand how to pass above catcher[i] to main func() in the form of entire array? Thanks in advance...

int main()
{
    std::vector <int> varray;
    while(true)
    {
        int input;
        if (std::cin >> input)
        {
            varray.push_back(input);
        }
        else {
            break;
        }
    }
    multif(varray);

    std::cout << multif << std::endl;
ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • What does your text-books tell you about calling functions and getting the value it returned? – Some programmer dude Oct 25 '21 at 13:06
  • man, I dun have a text-book, i just started to learn coding :( – DunPushSoHard Oct 25 '21 at 13:09
  • You should definitely invest in a [textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – ChrisMM Oct 25 '21 at 13:12
  • `catcher[i]` is not an "entire array" it is a single `int`. Taking your question literally it almost sounds like coroutines, but I think you want something much simpler – 463035818_is_not_an_ai Oct 25 '21 at 13:12
  • [Here's a list of decent books](https://stackoverflow.com/a/388282/440558). You really need to invest in a couple of beginners books, and perhaps even take classes. Otherwise it's going to be very hard to learn anything useful (online resources usually aren't enough, and sometimes can be directly harmful). – Some programmer dude Oct 25 '21 at 13:18

2 Answers2

1

You could do it like this :

#include <vector>
#include <iostream>


void multif(std::vector<int>& catcher) 
{
    std::cout << "\nThe array numbers are: ";
    for (int i = 0; i < catcher.size(); i++)
    {
        catcher[i] *= 2;
        std::cout << catcher[i] << " ";
    }
}

int main()
{
    // use initializer list to not have to test with manual input
    std::vector<int> input{ 1,2,3,4,5 }; 
    multif(input);
}
 
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
1

Your multif() function:

int multif(std::vector<int> &catcher)
{
    printf("\nThe array numbers are:  ");
    for (int i = 0; i < catcher.size(); i++)
    {
        catcher[i]*=2;
        //printf("%d\t", catcher[i]);
        return catcher[i]; 
    } 
}

This will double the first element of catcher and return it. I don't think that's what you want. If you want to double all the elements in your function, change that to:

void multif(std::vector<int> &catcher)
{
    printf("\nThe array numbers are:  ");
    for (int i = 0; i < catcher.size(); i++)
    {
        catcher[i]*=2;
        //printf("%d\t", catcher[i]); 
    } 
}

Also, in main() , you call std::cout << multif << std::endl; which will print the memory address of multif(), which also probably not what you want.

If you want to print all the values of varray, try:

void multif(std::vector<int> &catcher)
{
    printf("\nThe array numbers are:  ");
    for (int i = 0; i < catcher.size(); i++)
    {
        catcher[i]*=2;
        //std::cout << catcher[i] << '\n' you can print it here, or:
    } 
}

int main()
{
    std::vector <int> varray;
    while(true)
    {
        int input;
        if (std::cin >> input)
        {
            varray.push_back(input);
        }
        else {
            break;
        }
    }
    multif(varray);
    
    for(const auto &i : varray) std::cout << i << '\n';
}
  • 'for(const auto &i : varray) std::cout << i << '\n';' << this is very cool! – DunPushSoHard Oct 25 '21 at 13:28
  • It's a range-based for loops. If you want to have a good tutorial but don't want to spend money on books, I recommend [this](https://www.learncpp.com/) website. It picks up a lot of good practice and later in the tutorial, you'll know what my "cool" loop is. – justANewb stands with Ukraine Oct 25 '21 at 13:31