5 Replies - 831 Views - Last Post: 24 February 2013 - 10:17 PM Rate Topic: -----

#1 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

How to fix expected `;' before numeric constant error?

Posted 24 February 2013 - 02:05 PM

Good morning/afternoon/evening! I have tried to fix this error for a while, but it sadly is beyond me ://>.
I found this:http://cplusplus.syntaxerrors.info/index.php?title=Expected_%60;'_before_numeric_constant
But I cannot implement it because it is bit hard for me to understand as English isnt my native language and I am still quite a beginner in programming.The problem( expected `;' before numeric constant error?) is on line 21.

#include <iostream>
        #include <cstdlib>
        #include <ctime> 
        #include <iomanip>  
        #include <conio.h>

        using namespace std;


        void selectionSort( int *, int );

        int main()
        { 
        int N; 
        int a[ N ];
	
        cout<<"\n Enter the length of sequence:";
        cin>>N;
                           for(int i = 0; i < N && i < 5; ++i) 
				        {
			                a[ N ] = rand() % 1 000 000 + 0; 
					        srand( time( 0 ) );
					
					        cout << "Random sequence";
                            for (int i =0; i<N; i++)
					        cout << setw(4) << a[i];
					        cout <<endl;


				        }
        cout << "Sorted sequence";
		        selectionSort (a,N);
		        for (int j=0; j< N; j++)
		        cout << setw(4) << a[j];
		        cout << endl;
         getch();

        } 




        void selectionSort( int *array, int N )
        {

		        int temp , i , j ; 
		        for (i=0; i< N-1; i++)
		        {
				        j = i;
				        while (j > 0 && array [j-1] > array [j])
					        {
						        temp = array [j];
						        array[j] = array [j-1];
						        array [j-1] = temp;
						        j--;
						        }
		        }
        } 

This post has been edited by needhelpest: 24 February 2013 - 02:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to fix expected `;' before numeric constant error?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,666
  • Joined: 02-June 10

Re: How to fix expected `;' before numeric constant error?

Posted 24 February 2013 - 02:11 PM

Its saying you forgot to put a ; at then end of one your lines.
So the line the compiler stopped on is the line right after where it was expecting to see a ; ending the previous line.

Line 21 sure looks pretty screwy with those spaces in the middle of the number. You might want to look into that.After all that seems like it fits the description.
It expected a ; before a numerical constant, and 000 would be a numerical constant, and that 1 would have been the end of the line before all the goofy 000 000 + 0 business.
Was This Post Helpful? 1
  • +
  • -

#3 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: How to fix expected `;' before numeric constant error?

Posted 24 February 2013 - 02:13 PM

you have to write 1 000 000 as 10000000 ... spaces are not allowed between digits of a number
Was This Post Helpful? 1
  • +
  • -

#4 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Re: How to fix expected `;' before numeric constant error?

Posted 24 February 2013 - 02:13 PM

View PosttlhIn`toq, on 24 February 2013 - 02:11 PM, said:

Its saying you forgot to put a ; at then end of one your lines.
So the line the compiler stopped on is the line right after where it was expecting to see a ; ending the previous line.

Line 21 sure looks pretty screwy with those spaces in the middle of the number. You might want to look into that.After all that seems like it fits the description.
It expected a ; before a numerical constant, and 000 would be a numerical constant, and that 1 would have been the end of the line before all the goofy 000 000 + 0 business.

Oh, youre right. I fixed the goofy spaces and the program crashes on me now :(.
Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: How to fix expected `;' before numeric constant error?

Posted 24 February 2013 - 09:59 PM

Well, you need to see your line 21

 for(int i = 0; i < N && i < 5; ++i)
 {
a[ N ] = rand() % 1000000 + 0;


You seem to be assigning a single index of the array 'a' with a different random value each time...

Well,shouldn't you be doing

 for(int i = 0; i < N && i < 5; ++i)
 {
a[ i ] = rand() % 1000000 + 0;


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: How to fix expected `;' before numeric constant error?

Posted 24 February 2013 - 10:17 PM

There are several things wrong with the following snippet.
        int N; 
        int a[ N ];
	
        cout<<"\n Enter the length of sequence:";
        cin>>N;
                           for(int i = 0; i < N && i < 5; ++i) 
				        {
			                a[ N ] = rand() % 1 000 000 + 0; 

First you try to use N before you have assigned a value to this variable.

Second I don't recommend using Variable Length Arrays, I recommend you use either a compile time constant, or dynamic memory. VLA are only supported by C99, and maybe C11 compliant compilers, so unless you know that your compiler supports this "feature" I recommend you do it the old fashioned way.

Lastly if you did properly initialize N before you used it to create your array you would be accessing this array out of bounds in that last line. Arrays in C start at 0 and end at size - 1.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1