error C2109: subscript requires array or pointer type

Hi everyone, I'm new to C++ programming. I got a task to sort students mark in ascending order using bubble sorting. The marks are given in the text file. I had try to solve it but error still occurred and i don't know how to solve the errors. Hoping anyone can help me. Thanks for your kindness.

Here is the text file. (first column indicate matricNum, second column marks and third are gender)
001 65.5 2
002 56.7 2
003 35.8 1
004 42 2
005 49.4 1
006 55.1 1
007 87.5 2

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
#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()

{
	double numb[7];
	int i, j;
	double temp;
	int matricNum;
	double marks;
	int gender;

	// store data into array.
    {
        // open the file for input
        ifstream inputFile("myFile.txt");

        // the input file was opened 
        for( int i = 0; i < 7; i++ )
        inputFile >> matricNum[i] >> marks[i] >> gender[i];

        // the file is closed
    }

	//sort marks in ascending order using bubble sort

	for (i=0;i<=7;i++)
	{
		for (j=i+1;j<=6;j++)
		{

			if (marks[i] > marks[j])
			{
				temp = marks[i];
				marks[i] = marks[j];
				marks[j] = temp;
			}
		}
	}

	for (i=0;i<=6;i++)
	{
		cout << matricNum << marks[i] << gender << endl;
	}

	system("pause");
}


The output should be:
003 35.8 1
004 42 2
005 49.4 1
006 55.1 1
002 56.7 2
001 65.5 2
007 87.5 2
error C2109: subscript requires array or pointer type

Does your compiler/IDE show the line that causes that error? It should.

Lines 24, 36, 38, 39, 40, 47:
1
2
3
4
5
int matricNum; // one number
double marks; // one number
int gender; // one number

inputFile >> matricNum[i] >> marks[i] >> gender[i];

You do use those three variables like they were arrays.

Then again, you never use the array double numb[7];
Hi yat89,

Errors/warnings for your file:
 In function 'int main()':
24:33: error: invalid types 'int[int]' for array subscript
24:45: error: invalid types 'double[int]' for array subscript
24:58: error: invalid types 'int[int]' for array subscript
36:15: error: invalid types 'double[int]' for array subscript
36:26: error: invalid types 'double[int]' for array subscript
38:19: error: invalid types 'double[int]' for array subscript
39:12: error: invalid types 'double[int]' for array subscript
39:23: error: invalid types 'double[int]' for array subscript
40:12: error: invalid types 'double[int]' for array subscript
47:31: error: invalid types 'double[int]' for array subscript
10:9: warning: unused variable 'numb' [-Wunused-variable]


I believe your issue is that you never actually use your numb array.
Your error is that you are attempting to use matricNum as an array (by accessing it as matricNum[i]), but matricNum is simple an int.

Perhaps you meant to use numb instead of matricNum?

Similarly, marks is not an array.
If you want it to be an array, declare it as such,
perhaps double marks[7];

Last,
for (i=0;i<=7;i++)
Please note that if your array has 7 elements in it, accessing my_array[7] is out of bounds of the array, because arrays start at 0.
I would change the <= to just <.
Last edited on
Topic archived. No new replies allowed.