Primary expressions error

gamingfreak19

Reputable
Jul 7, 2014
32
0
4,540
When compiling the script, I keep getting "expecting primary expression before 'else.'" Does anyone know what I am doing wrong so that I can fix it? Below is a pseudocode that I implemented.

// 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 - average
// output "Number[",i,"]: ",numbers," Difference from Average is ",diffFromAvg
// endfor
// else
// output "Processing incomplete. No values in the array."
// endif
// Stop


#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int SIZE = 12;
int numbers[SIZE];
int value = 0;
int counter = 0;
int total = 0;
int average = 0;
int diffFromAvg = 0;
int SENTINEL= -1;
int i;




cout << "Please enter a positive number:";
cin >> value;
while ((counter < SIZE) && (value != SENTINEL))
{
total = total + value;
numbers[counter] = value;
counter = counter + 1;
if (counter != SIZE)
{
cout << "Please enter a positive number:";
cin >> value;
}

}

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

system ("pause");
return 0;
}

Errors:

79 C:\Dev-Cpp\PLD5-7a\PLD6-2ab.cpp expected primary-expression before "else"
79 C:\Dev-Cpp\PLD5-7a\PLD6-2ab.cpp expected `;' before "else"
 
Your link is broken to the script, for starts. Also, I think Toms has a [.code] bbc you can use if you just want to C&P the code. I would personally suggest doing so to a pastebin file and then link that, it's easier for everyone involved to access.

My suspicion is you probably have not properly ended your if() then part of your script, check to see brackets are correct. Also, if you have nothing before it as in if ... then you may need to rephrase your statement to make it so that it doesn't use else, but rather the else condition is the if condition.
 


How do you get the code for your image?

 


My apologies, I don't quote understand what you mean. BBC is a markup language frequently used in forums, along with a British producer of high quality public content.