ERROR expected '}' before 'else'

When I go to rebuild/compile I get the error referenced above, any input greatly appreciated. Thanks

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
#include <iostream>
#include <cstdlib>

using namespace std;
int main ()

{
int SIZE = 12;
int numbers[SIZE];
int value = 0 ;
int counter = 0;
int total = 0;
double average = 0;
double diffFromAvg = 0;
int SENTINEL =-1;
int i;
	cout << "Please enter a positive number: " << endl;
	cin >> value;

	while ((counter < SIZE) and (value != SENTINEL))
		{
		total = total + value;
		counter = counter + 1;
		}
	if (counter != SIZE)
			{
				cout<< "Please enter a postive number:" <<endl;
				cin>>value;
			}
	
	if (counter > 0)
			{
			   average= total/counter;
			{
			
	for (i==0; counter-1;)
			{
			diffFromAvg = numbers[i] - average;
			cout<< "Number["<<i<<"]: " << numbers[i] << " Difference from Average is " << diffFromAvg << endl;
			}
	else
			{
			cout << "Processing incomplete. No values in the array." << endl;
			}


	system ("PAUSE");
	return 0;
Last edited on
Code indentation is very important to code legibility when reading.

This is what your code looks like when formatted "properly" (properly in quotation marks because attempting to format it makes the error pop out).
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
#include <iostream>
#include <cstdlib>

using namespace std;
int main ()
{
	int SIZE = 12;
	int numbers[SIZE];
	int value = 0 ;
	int counter = 0;
	int total = 0;
	double average = 0;
	double diffFromAvg = 0;
	int SENTINEL =-1;
	int i;
    
	cout << "Please enter a positive number: " << endl;
	cin >> value;

	while ((counter < SIZE) and (value != SENTINEL))
	{
		total = total + value;
		counter = counter + 1;
	}
	if (counter != SIZE)
	{
		cout<< "Please enter a postive number:" <<endl;
		cin>>value;
	}
	
	if (counter > 0)
	{ // ???
		average= total/counter;
		{ // ???		
			for (i==0; counter-1;)
			{
				diffFromAvg = numbers[i] - average;
        			cout<< "Number["<<i<<"]: " << numbers[i] << " Difference from Average is " << diffFromAvg << endl;
			}
			
			// ???
			else
			{
				cout << "Processing incomplete. No values in the array." << endl;
			}
         	
         	// your else has no IF 
         	// You have no closing braces for lines 32 and 34
         	// Your opening brace on line 34 is not necessary in this case.

	system ("PAUSE");
	return 0;
}



Your else has no IF
You have no closing braces for lines 32 and 34.
Your opening brace on line 34 is not necessary in this case.


EDIT: for (i==0; counter-1;)
This is also wrong, and error prone.
= is for assignment, == is for equality.
I suggest doing:
for (int i = 0; i < counter; i++)
and removing your i variable on line 16 (

EDIT 2:
Because size is used as the size of an array, it needs to be const or constexpr.
const int SIZE = 12;
Last edited on
I believe you meant to something like this:

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
#include <iostream>
#include <cstdlib>

using namespace std;
int main ()
{
	const int SIZE = 12;
	int numbers[SIZE];
	int value = 0 ;
	int counter = 0;
	int total = 0;
	double average = 0;
	double diffFromAvg = 0;
	int SENTINEL = -1;

	while ((counter < SIZE) and (value != SENTINEL))
	{
		cout << "Please enter a postive number:" << endl;
		cin >> value;
    	
		total = total + value;
		counter = counter + 1;
	}

	if (counter > 0)
	{
		average = total/counter;	
		for (int i = 0; i < counter; i++)
		{
			diffFromAvg = numbers[i] - average;
			cout<< "Number["<<i<<"]: " << numbers[i] << " Difference from Average is " << diffFromAvg << endl;
		}
	}
	else
	{
		cout << "Processing incomplete. No values in the array." << endl;
	}

	return 0;
}


NOTE: THERE IS STILL A BUG LEFT FOR YOU.

HINT: What is the value of numbers[i] (for any given i within range)


Also, note that line 27 (line 33 in your post) is doing integer division. One of the variables must be cast to double to prevent this.

Also does your assignment involve rainfall by any chance? That seems to be a favorite question around these parts.
Last edited on
Thank you. No this does not involve rainfall. As far as far as the value of numbers [i] goes, i'm lost, been trying to figure that one out. Here is the pseudo code I was given,
// Start
// Declarations
// num SIZE = 12
// num numbers[SIZE]
// num value = 0
// num counter = 0
// num total = 0
// num average = 0
// num diffFromAvg = 0
// num SENTINEL = -1
//
// output "Please enter a positive number: "
// input value
// while ((counter < SIZE) AND (value <> SENTINEL) )
// total = total + value
// numbers[counter] = value
// counter = counter + 1
// if (counter <> SIZE)
// output "Please enter a positive number: "
// input value
// endif
// endwhile
//
// if (counter > 0) then
// average = total/counter
// for i = 0 to counter - 1
// diffFromAvg = numbers[i] - average
// output "Number[",i,"]: ",numbers[i]," Difference from Average is ",diffFromAvg
// endfor
// else
// output "Processing incomplete. No values in the array."
// endif
// Stop

I feel like I am missing something??
You need to assign values[i].
values[i] = some_value;

In your while loop, you use value to add to the total, but you never do anything else with value.
Topic archived. No new replies allowed.