How to initialize a variable...

Hi there,
I've recently started coding and decided to have a look at Project Euler problem number one (Find the sum of the numbers below 1000 that divide by either 3 or 5)...

The logic behind this problem appears quite easy; however I can't seem to get my compiler (Visual c++ 2010 express) to accept my programme.

It keeps telling me that I have not initialized my 'sumOfValues' variable - despite me being fairly certain that I have initialized the variable sufficiently.

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
  // ProjectEuler1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

int i = 1;
int sumOfValues = 0;

using namespace std;

//will be used to check if the number is a multiple of 3 or 5
void multipleCheck(int i)
{
	if (i%3 == 0 || i%5 == 0)
		int sumOfValues = i+sumOfValues; //The programme is being stopped at this line - primarily. It hasn't gone further for me to be able to find out if other lines work correctly.
}

int main() {
// I have tried placing 'int sumOfValues = 0;' in here as well, but this recieves the same result.
do {
	multipleCheck(i);
	i++;
	cout << "\n" << i;
	cout  << "\t" << sumOfValues;
}
while (i<=1000);
cout << sumOfValues;
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}


Any help would be great;

Thanks!
Last edited on
closed account (18hRX9L8)
You have a global variable sumOfValues. This means that it can be accessed by the whole file. Therefore, you do not have to re-define the variable.


(Basically remove the int on line 16.)
Last edited on
Thank-you..
That solved the problem.
your problem is the variable sumOfValues inside the declaration of the function multipleCheck as you said but I think that we should step back for a moment and analyze your code.

you are declaring a global variable here int sumOfValues = 0;

you are declaring another variable named sumOfValues inside your multipleCheck function.

there is nothing in the C++ standard that says that you can't create 2 variable in 2 different scopes with the same name, you will just end up having 2 separate variable with the same name, your real problem is the fact that the assignment operator = operates from right to left and the + operator for the addition got the same right-to-left precedence.

you should visualize the situation this way:

- the compiler goes on that line
- it finds an equal sign =
- it starts analyzing what is on its right first ( because of the precedence rule )
- if finds a + sign and again, what is on its right should be considered first than anything else
- the + sign cares about the value of your variable, simply because it got an arithmetic addition to do, but on the right there is a variable that is not initialized yet, and to be picky, that variable it's not even created ( remember that what is on the left of the = it's not even considered yet by the compiler because of all the precedence rules ).

the fix is really simple in this case

int sumOfValues = i+sumOfValues;

should be

sumOfValues = i+sumOfValues;

and in this case you are using the first global variable sumOfValues that you have declared in your code, to use a local variable in that function you should write

1
2
3
4
5
void multipleCheck(int i)
{
	int sumOfValues = 0 ;
        ...
}


but this actions are fixing your problem with that line, your code still contains relevant errors, since you are learning that I'm not sure if it's best for you to have someone else writing the program for you but next time, to prevent errors like this, it's important to focus on :

- global and local variables
- operators and their own precedence
to fix the other major problems consider studying this topics:

- short circuit evaluation in C++ and logic operations
- do-while and similar constructs and how they operates.
- why the use of global variables is a really bad idea
you have a global variable which is accessible from the whole program. So, you don't need to declare it again.
Topic archived. No new replies allowed.