What does this error mean?

I am getting a really annoying error telling me that I need a ";" where I shouldn't.
The exact error is: test.cpp:38:3: expected ';' before 'median'.
What am I doing wrong?

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
67
68
69
 #include <iostream>
 using namespace std;
 
 int main()
 {
	int n;
	int i;
	int sum;
	int median;
	int numArray;
	int flag = 0;
	int array[100];
	 
	do
	{
		cout << "\nPlease enter a number: ";
		cin >> n; 
		
		numArray++;
		
		if (n < 0)
			flag = 1;
		
		else if (numArray > 100)
		{
			cout << "Too many entry numbers! Please try again.";
			return 0;
		}
		else
		{
			array[i] = n;
			sum += n;
			i++;
		}
	}
	
	while (flag == 0)
		median = numArray/2;
	
	while (1)
	{
		flag = 0;
		
		for (i = 0; i + median < numArray+1; i++)
		{
			if (array[i] < array[i + median])
			{
				n = array[i + median];
				array[i+median] = array[i];                             
                                array[i] = n;
                                flag = 1;
                        }
                }
               if (flag == 0)
               {
		      if (median <= 1)
                           break;
                      else
                           median = median / 2;
               }
        }
        
	cout << "The sorted array is " << array[i];
	cout << "The number of entries is " << numArray - 1;
	cout << "The median is " << array[median];
	
	return 0;
	
 }

Last edited on
closed account (z05DSL3A)
What does this error mean?
The exact error is: test.cpp:38:3: expected ';' before 'median'.

you are missing a ; on line 37


1
2
3
4
5
6
7
    do
    {
        //...

    }while (flag == 0);  
	
    median = numArray/2;


You have other issues but the above is the error in question.

edited to make the answer clearer
Last edited on
exaclty here:
for (i = 0; i + median < numArray+1; i++)
//should be
for (i = 0; i + median < ;numArray+1; i++)
neither of those have worked.
You need to initialize numArray to a value before you increment it on line 19.

Same thing for i. i doesn't have a valid value when you try to use it first on line 31

And sum needs to be initialized before you try to add to it on line 32.

Ooh okay, I think that fixed the segfault. Thank you!!!
however, now I'm having something else weird happening. The program stops after only one number is entered, it's supposed to only stop when over 100 numbers have been entered or when a negative is entered. It's also giving me really weird outputs.

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
67
68
69
70
 #include <iostream>
 using namespace std;
 
 int main()
 {
	int temp;
	int i = 1;
	int sum = 0;
	int median;
	int numArray = 0;
	int flag = 0;
	int array[100];
	 
	do
	{
		cout << "\nPlease enter a number: ";
		cin >> temp; 
		
		numArray++;
		
		if (temp < 0)
			flag = 1;
		
		else if (numArray > 100)
		{
			cout << "Too many entry numbers! Please try again.";
			return 0;
		}
		else
		{
			array[i] = temp;
			sum += temp;
			i++;
		}
	}
	
	while (flag = 0);
		median = numArray/2;
		
	
	while (1)
	{
		flag = 0;
		
		for (i = 0; i + median < numArray+1; i++)
		{
			if (array[i] < array[i + median])
			{
				temp = array[i + median];
				array[i+median] = array[i];                             
                array[i] = temp;
                flag = 1;
            }
         }
        if (flag == 0)
        {
			if (median <= 1)
                break;
            else
                median = median / 2;
        }
     }
        
	cout << "\nThe sorted array is " << array[i];
	cout << "\nThe number of entries is " << numArray - 1;
	cout << "\nThe median is " << array[median];
	
	return 0;
	
 }


This is a mess. What am I doing wrong? :(

Remember arrays of size 100 are 0-99, you should be checking for over 99 not over 100.

Anyway, try 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
41
42

#include <iostream>
using namespace std;

int main()
{

	int array[100];
	int temp, index = 0;

	while (1)
	{
		cout << "Enter a number: ";
		cin >> temp;
		if (temp < 0) // are we a minus?
			break;
		else {
			array[index++] = temp;
			if (index > 99) // array[100] is 0-99
				break;
		}
	}

	// index now has number of entries in array, lets
	// now sort them in ascending order.
	for (int i = 0; i < index; i++)
		for (int j = 0; j < index - i - 1; j++)
			if (array[j] > array[j + 1])
			{
				temp = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp;
			}

	cout << endl << "Numbers now sorted..." << endl;
	for (int i = 0; i < index; i++)
		cout << array[i] << endl;

	return 0;

}

Enter a number: 22
Enter a number: 1
Enter a number: 5
Enter a number: 2
Enter a number: 7
Enter a number: 6
Enter a number: 4
Enter a number: 21
Enter a number: 11
Enter a number: 9
Enter a number: -1

Numbers now sorted...
1
2
4
5
6
7
9
11
21
22
Hm, I see. I'll mess around with this and see where I'm going wrong. Thank you so much for your help!

Your welcome.
You still want == here, not =.

line 37 while (flag = 0);
As quoted above, on line 37, you're assigning flag to 0 instead of comparing it against 0.

Due to that very small mistype/error, this section of code terminates your program.

1
2
3
4
5
6
7
if (flag == 0)
		{
			if (median <= 1)
				break;
			else
				median = median / 2;
		}
Last edited on
Oh fantastic, thank you so much everyone! I've got it working!!
Topic archived. No new replies allowed.