Setting ints

Pages: 12
I am currently re-learning everything about coding. (haven't done it in 3 years)
Anyways, as I am working on problem 2 @ projecteuler.net I am having a problem that I know has a quick fix. I am having a problem with these two lines.
1
2
prev2 = prev1;
prev1 = i;


Both are declared but i'm getting the "left operand must be l-value"
How are prev1 and prev2 declared?
int prev1 = 2;
int prev2 = 1;
There are no problems with this. Could you post a small compilable code which produces this error for you?
Er, I just realized the lines were off. I figured it out. Thanks anyways. Hehe.
Last edited on
have you declared i, I think it may be getting confused
if (prev1 + prev2 = i) This line is what produces the error. Did you mean ==?
Original got deleted.

EDIT: 500th post
Last edited on
Alright. I thought I figured it out but I am still having a problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	int i = 0;
	int answer = 0;
	int prev1 = 2;
	int prev2 = 1;
	for(i = 0; i<=4000000; i++)
	{
		if (i = prev1 + prev2)
		{
			prev1 = prev2;
			i = prev1;
			if(i%2 = 0)
			{
				answer += i;
			}
		}


Getting an error at the second if loop
= is the assignment operator. You meant == which is the equality operator.
what do you mean by answer += i, is += only used for comparisons.
please explain to me if += has other uses unrelated to comparisons since I don't know any
What I was having it do was add up all of the even numbers. More problems in this than I originally though. Heh.
R0mai guy is write, when comparing its ==, and when assigning a value its =

if expressions can't be initialised to assign values, I don't know whether I've said it in the clearest way but hopeful you get the picture.
Last edited on
The += operator is not related to comparisons in any way. answer += i; is the same as answer = answer + i;
I knew I wasn't insane when using +=. Lol.

add: Although, looking at it. I isn't getting out of the if loop. If I put

1
2
3
4
5
if(i == prev1 + prev2)
		{
			prev1 = prev2;
			i = prev1;
			cout << i << endl;


All it shows is 1. :\
Last edited on
What are you trying to do anyway?
Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.
dude functions don't work in loops unless you are inside a while loop or you've decided to use a while loop instead
dude functions don't work in loops unless you are inside a while loop or you've decided to use a while loop instead

Huh? Could you explain this, in more understandable form?

EDIT: Is 4613732 the correct answer? It would be interesting to calculate it in constant time.
Last edited on
I am looking over my code and realized that I will never get out of if(i == prev1 + prev2) because I am setting i = prev1 in that loop. Not sure how to go about rewriting this besides scrapping it. Heh.
Last edited on
Sorry I'm still getting confused about calling expressions like (if, while, do...) function, sorry but I'm still trying to loose the habit.

Anyway

I've noticed that if(){} statements seem to only execute once if they are out side a do or while loop.

eg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
void main()
{
using namespace std;

bool boolean = true;
int intexample = 1;

if (boolean == 1)
{++intexample;
cout << intexample << endl;
}

system("PAUSE");
}


whereas for while loops, its the if statement gets executed multipletimes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
void main()
{
using namespace std;

bool boolean = true;
int intexample = 1;

while(boolean == true)
{
if (boolean == 1)
{++intexample;
cout << intexample << endl;

if(intexample >= 20)
{
system("PAUSE");
}
}
}

system("PAUSE");
}


try these out and you'll see the point
Pages: 12