4 Replies - 476 Views - Last Post: 23 June 2012 - 11:52 AM Rate Topic: -----

#1 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

" missing ';' before '+=' " - Error that I do

Posted 22 June 2012 - 10:29 PM

Hey guys, working with Matricies and I'm using a new type of math value and I continue to get an error.

	{ 
		if (xin == 1) {
		for (int n=0; n<HEIGHT; n++)
			for (int m=0; m<WIDTH; ++m)
			int sum=0;
			int sum += givenArray [HEIGHT] [WIDTH];
			
			int average = sum/(HEIGHT*WIDTH);	
			cout << "Average of the matrix is = " << average << endl; }
			system ("pause");
			system ("CLR"); }



with the above simple function I continue to get an error saying
 syntax error : missing ';' before '+=' 
.

What does this mean exactly? To be honest the only reason I have
 int sum=0; 
even in there is to try and fix this. Here is the whole code for reference but feel free to ignore most of it, it is very much so a work in progress.

#include <iostream>
using namespace std;
#include <iomanip>
#define HEIGHT 5
#define WIDTH 4
int givenArray[5][4] = { { 34,  56, 79, 12 }, 
			     { 25, 37, 41, 18  },
				 { 59, 29,  38, 47 },
				 { 55,  11, 88, 34 },
				 { 45, 19,  34, 66 } };

int optionsMenu() 
{
	int xin;
	cout << "Please select an option" << endl;
	cout << "# 1 for average of matrix" << endl;
	cout << "# 2 to see the biggest number in the matrix" << endl;
	cout << "# 3 to see the smallest number in the matrix" << endl;
	cout << "# 4 to sort from largest to smallest" << endl;
	cout << "# 5 to sort from smallest to larges" << endl;
	cout << "# 6 to search for a number of your choice" << endl;
	cout << "# 7 to exit" << endl;
	cin >> xin;
	system ("CLR");

	while (xin !=7)
	{ 
		if (xin == 1) {
		for (int n=0; n<HEIGHT; n++)
			for (int m=0; m<WIDTH; ++m)
			int sum=0;
			int sum += givenArray [HEIGHT] [WIDTH];
			
			int average = sum/(HEIGHT*WIDTH);	
			cout << "Average of the matrix is = " << average << endl; }
			system ("pause");
			system ("CLR"); }
		
		else if (xin == 6) {
		for (int x=0; x<HEIGHT; x++)
			for (int y=0; y<WIDTH; y++)
				int = SearchVal
				cout << "Enter the number you would like  to search for: " << endl;
				cin >> SearchVal;
		}

		system ("pause");
		system ("CLR");

		}
	}


	int main()
	{
		cout << "===============================================" << endl;
		cout << "WELCOME TO DATA PROCESSING CENTER" << endl;
		cout << "===============================================" << endl;

		optionsMenu();
	}



I have tried to search for my error but it seems to be a code by code basis so I feel as if I have to post a topic now. I have tried to function with matrices using my professors notes but I can assure you that I will be reading up on the tutorials on this site this weekend.

I really just want to figure out what my error means! Thank's guys as always!

Is This A Good Question/Topic? 0
  • +

Replies To: " missing ';' before '+=' " - Error that I do

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: " missing ';' before '+=' " - Error that I do

Posted 22 June 2012 - 10:41 PM

you don't want to go int sum += you want just sum +=. You already defined sum on the line before.

Also keep in mind that when you have an for loop with no opening curly braces following it, it will only execute the first line after the loop. Meaning in your code above, sum += givenArray [HEIGHT] [WIDTH]; Is not being included in your inner for loop. You must wrap it in the loop with curly braces.

Also when you declare sum in your loop and set it to zero, you are going to end up resetting sum each time the loop executes. You might want to declare sum outside the scope of the inner loop.

:)

This post has been edited by Martyr2: 22 June 2012 - 10:43 PM

Was This Post Helpful? 2
  • +
  • -

#3 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Re: " missing ';' before '+=' " - Error that I do

Posted 23 June 2012 - 05:49 AM

Thanks that did the trick! What I did was declare sum outside of this loop so my code now looks like
		if (xin == 1) {
		for (int n=0; n<HEIGHT; ++n) 
			for (int m=0; m<WIDTH; ++m) {
			
			sum += givenArray[n][m];
			average = sum/(HEIGHT*WIDTH);	

			cout << "Average of the matrix is = " << average << endl; 
				 }
		}
	


I've looked at other tutorials and this is it seems I am supposed to be doing but when I run the code (which does work) it simply adds a bunch of numbers infinitely without ever seeming to execute the way I want. The rest of the code is included above so you can see where I defined HEIGHT and WIDTH and what my array/matrix is supposed to look like. The average should be 45.something.

What is wrong with my execution? It looks like my professors notes???
Was This Post Helpful? 0
  • +
  • -

#4 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: " missing ';' before '+=' " - Error that I do

Posted 23 June 2012 - 06:43 AM

This is because you've got a while loop just outside, I'm looking at
 while (xin != 7) 
Because once you enter the loop you'll never change xin, you enter an infinite loop. You probably meant to put an if conditional.
Was This Post Helpful? 1
  • +
  • -

#5 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Re: " missing ';' before '+=' " - Error that I do

Posted 23 June 2012 - 11:52 AM

Thanks man!

We used to use the while loop just like that for past projects so I was just continuing it. However, this works much, much better.

Thanks a lot again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1