error C2061

: error C2061: syntax error : identifier 'density'

I have done quite a bit of research about this error on the internet. It seems as if the common answer is that there are either extra brackets somewhere of that I am trying to define my variable in some incorrect ways. I checked my brackets and they seem fine to me and I moved all my variables outside the main function. The error occurs when I try to use density for the first time. This is in line 61. The code is not complete yet because I'm trying to find errors as I go instead of ending up with an overwhelming amount when the code is complete. Any help or advice is appreciated, thank you greatly.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//-------------------------------------------\\
// Purpose: Display a sequence of numbers
// caculated by given equations and a user
// defined starting point. Also calculate
// various stats about the generated numbers.
// Name: Will Beeler
// Date: October 22, 2009
//-------------------------------------------\\

#include <iostream>
#include <cctype>

using namespace std;


int firstInteger;
int integer;
int densitySpreadTermCount(0);
double densitySpreadSum(1);
double density;
int integerCount(1);



int main()

{

	//prompt user for initial number
	cout << "Enter the initial positive integer: ";
	cin >> firstInteger;
	firstInteger = integer;

	//determine if number is even, odd, or 1

	do while ( integer != 1 )
	{	
		if ( integer % 2 == 0 )
		{
			integer = integer / 2;
		}
		else 
		{
			integer = (3 * integer) + 1;
		}//end if

		// counts total number of integers in sequence
		integerCount++;

		//cout << integer << "  "; ???

		/*A continuous sum going with a counter for numbers equal to or less then the 
		starting value to calculate density spread */
		if ( integer <= firstInteger )
		{
			densitySpreadSum += integer;
			densitySpreadTermCount++;
		}//if
	}//while

density = ( densitySpreadSum + 1 ) / ( densitySpreadTermCount + 1);

cout << "Density: " << density << endl; 

	return 0;
}

Last edited on
A while loop is declared like this :

while()

As apposed to this:

do while()

At least thats what I found
Last edited on
Line 36: It's while, not do while.
1
2
3
4
5
6
7
8
9
//checks at the start of the loop
while (/*condition*/){
    //code
}

//checks at the end
do{
    //code
}while (/*condition*/);
:P You stole my answer
Ha. Wow. Perfect answer. I completely missed that. I had been changing back an forth in between the two and apparently forgot to delete the do after copy pasting the while. Thank you very much (both of you), fixed the problem right up.
Last edited on
Topic archived. No new replies allowed.