0

I have this code where it read user code and store it on array, and later find sums of elements on each array and compare both of them, the code is as follows:

#include <iostream>
#include <vector>
#include <numeric>

typedef std::vector<int> int_vec_t;

//Call by reference to set variables in function
void readData(int_vec_t& v1, int_vec_t& v2)
{
  v1 = int_vec_t{1,1,8}; //This only works for C++11
  v2 = int_vec_t{2,2,2};
}

void readUserData(int_vec_t& v)
{
  for(;;)
  {
    int val;
    std::cin>>val;
    if(val == 0) break;
    v.push_back(val);
  }
}

int main()
{
    using namespace std;

    int_vec_t A;
    int_vec_t B;

    readData(A,B);
    //Or
    readUserData(A);
    readUserData(B);

    int sumA = accumulate(A.begin(), A.end(), 0); //Then use iterators
    int sumB = accumulate(B.begin(), B.end(), 0);

    cout << ((sumA > sumB) ? "Array A Greater Than Array B\n" : "Array B Greater Than Array A\n");

    return 0;
}

But the above code generate following errors:

test.cpp: In function ‘void readData(int_vec_t&, int_vec_t&)’:

I am using g++ test.cpp -o test to compile the code. What am I missing here?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Saraswathi Apavoo
  • 263
  • 2
  • 4
  • 18
  • 5
    That's not the error. It's saying the error is in that function. You should be able to just say `v1 = {1,1,8};`, by the way. – chris Mar 05 '13 at 15:19
  • @chris then how can i modified readData(int_vec_t& v1, int_vec_t& v2) and readUserData(int_vec_t& v) functions? – Saraswathi Apavoo Mar 05 '13 at 15:22
  • 1
    Your code compiles fine [here](http://liveworkspace.org/code/SWmBI$1) – Andy Prowl Mar 05 '13 at 15:22
  • @AndyProwl how can i set so each array only can accept 5 elements? – Saraswathi Apavoo Mar 05 '13 at 15:25
  • 1
    @SaraswathiApavoo: Is this the question you meant to ask? Because there is no trace of it in the text of your question – Andy Prowl Mar 05 '13 at 15:26
  • @AndyProwl since it keeps taking inputs, it would be better if i can limit to 5 – Saraswathi Apavoo Mar 05 '13 at 15:29
  • [@AxelOmega's code](http://stackoverflow.com/a/15225669/78845) compiles and runs as specified [here](http://liveworkspace.org/code/24Jxv0$1). Please follow the suggestions to read [a good C++ book](http://stackoverflow.com/q/388242/78845). Your questions are all due to fundamental misunderstandings as to how [tag:c++] works and StackOverflow cannot teach you to program in any language. Sorry. – johnsyweb Mar 05 '13 at 22:06

2 Answers2

3

don't you think the compilation should be something like:

$ g++ -std=c++11 test.cpp -o test ?

it keep taking inputs, how can i limit it to take only 5 elements per array

void readUserData(int_vec_t& v)
{
  for(int i = 0; i < 5; i++)
  {
    int val;
    std::cin>>val;
   // if(val == 0) return;
    v.push_back(val);
  }
}

Great thanks, in between the loop how can out put statement so that user knows he is entering array for array 1 and after that array 2?

void readUserData(int_vec_t& v, std::string default = "")
{
  for(int i = 0; i < 5; i++)
  {
    int val;
    std::cout << "Enter for "<< default << "[" << i << "]: ";
    std::cin>>val;
   // if(val == 0) return;
    v.push_back(val);
  }
}

And from your main() you can send in a different string.

for example like this:

readUserData(A, "A");
readUserData(B, "B");
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
2

This was my code posted as a suggested solution to c++ passing function into main error

Yes it needs a C++11 compiler as stated in the code. It was never meant as final code either.

I would recommend the OP to read a good book about C++. Let me suggest "C++ The Programming Language" http://www.amazon.com/The-Programming-Language-3rd-Edition/dp/0201889544

Community
  • 1
  • 1
AxelOmega
  • 974
  • 10
  • 18