Code returning 0? help!

While writing this code and moving it into a new project, when i run it all i get is a 0, nothing else. Heres the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <sstream>
#include <vector>
#define MAX 4000000
/*
Each new term in the Fibonacci sequence is
generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the
Fibonacci sequence whose values do not
exceed four million, find the sum of the even-valued terms.
*/
void SetVector(std::vector<unsigned long>List);
int main()
{
    std::vector<unsigned long>List;
    List.push_back(1);
    List.push_back(2);
    SetVector(List);
}

void SetVector(std::vector<unsigned long>List)
{
    unsigned long sum = 0;
    for(long i = 2; List[i] < MAX;i++)
    {
        std::cout << List[i-2] << " + " << List[i-1] << " = ";
        unsigned long temp = List[i - 2] + List[i - 1];
        List.push_back(temp);
        std::cout << List[i] << "\n";
        if(i % 2 == 0)
        {
            sum += temp;
            std::cout << "sum: " << sum << "\n";

        }
    }
    std::cout << sum;
}

EDIT: even when running this program from the flash drive it still returns 0, could it be the operating system im using? i was programming this on an Xp system(worked fine). Im now at home on a windows 7 and it no longer works
Last edited on
The operating system your using shouldn't play any role in the code you have. It could be the age of the compilers you are using, unless you have them the same on both systems.

I would pass the vector by reference because the copy of the two vectors might be causing problems in the implementation between compilers, if they are not the same. Although the standard library should be maintained for both with a long legacy.
Last edited on
are vectors not passed by reference by default? I was under the impression they are similar to arrays, thus always are passed by reference.
Are arrays passed by reference now? I thought this sort of thing:

1
2
int array[10];
someFunction(array);


would pass array as a copy of an int-pointer (i.e. pass by value).
Last edited on
I thought what Moschops thought
I expect it's the old "passing a pointer by value is kind of like a longhand way of passing what it points to by reference" interpretation. If we're lucky, it'll spark a huge hissyfit in someone and a little flamewar will start here about whether passing a pointer should be considered pass-by-value or pass-by-reference. :)
Arrays are passed by reference by default in C++ now. Since arrays are really just pointers it makes sense for the arrays to be passed by reference. But on topic, code still not working!!!
Arrays are passed the same way they've always been passed, by address.
@Moshcops, lolol

So, Need4Sleep, I just happened to notice your int main doesn't return anything. Just a heads up.
This seems a bit fishy: List[i] < MAX
If I read correctly, List[i] shouldn't exist yet at the point of comparison. List[i-1] should work.

(P.S.: How does pass-by-reference have anything to do with this? He's pass-by-value-ing a vector of two elements, but it's never used outside the function anyway, so who cares.)
(I sense a parentheses debate coming on EDIT:.)
Last edited on
(If a statement in parantheses is self-contained, it requires a full stop at the end.)
(I can play this game too. Who cares is a question and requires a question mark at the end.)
[Grammatically speaking you are (of course) correct, but in easy-speak using "who cares" as a statement signifies a rhetorical question implictly saying that nobody cares.}

(can i join?)
Topic archived. No new replies allowed.