error C2109: subscript requires array or pointer type

Hello, and thanks in advance for any help.

I'm having a problem with an array that I can not figure out how to fix. It's in a part of the program that's supposed to check if a number is prime or not. As long as I use a value array, I get the error message "Error: expression must have pointer-to-object type", but if I use a string array, the error goes away and the prime check doesn't run correctly.

I'm using Visual Studios 2010.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

int main()
{
	int table;
	int valuesInTable;
	int firstValueInTable;
	int increment;
	int flag = 0;
	ifstream inData;
	ofstream outData;
	int values[30];
	int squares[30];
	double squarerts[30];
	int cubes[30];
	double cuberts[30];
	int prime[30];
	string primes[30];
	string t;
	string f;
	int j = 0;
	cout << fixed << setprecision(3) << endl;

	while (flag < 1 || 2 < flag)
	{
		cout << "What is the source of your input data?" << endl;
		cout << "1 = user input data" << endl;
		cout << "2 = load data from file triples.text" << endl;
		cout << "Enter a 1 or 2: ";
		cin >> flag;
		cout << endl;
	}

	if( flag == 1 )
	{
		cout << "How many values should the table contain? (1 to 25): ";
		cin >> valuesInTable;
		while(valuesInTable <= 0 || 25 < valuesInTable)
		{
			cout << "Enter a number between 1 and 25: ";
			cin >> valuesInTable;
		}

		cout << "What is the first value in the table? (-1000 to 1000): ";
		cin >> firstValueInTable;
		while(firstValueInTable < -1000 || firstValueInTable > 1000)
		{
			cout << "Enter a number between -1000 and 1000: ";
			cin >> firstValueInTable;
		}

		cout << "What should the increment between successive values be? (1 to 20): ";
		cin >> increment;
		while(increment < 1 || increment > 20)
		{
			cout << "Enter a number between 1 and 20: ";
			cin >> increment;
		}
		cout << endl;
		
		for( int i = 0; i < valuesInTable; i++ )
		{
			values[0] = firstValueInTable;
			values[i + 1] = values[i] + increment;
			cout << "First column: " << values[i] << endl;
		}
		for ( int i = 0; i < valuesInTable; i++ )
		{
			double num = values[i];
			squares[i] = pow(num, 2);
			squarerts[i] = pow(num, .5);
			cubes[i] = pow(num, 3);
			cuberts[i] = pow(num, .33333);
		}

		for ( int i = 0; i < 25; i++ )
		{
			prime[i] = 0;
		}

		for ( int i = 0; i < valuesInTable; i++ )
		{
			int prime = values[i];
			if (prime < 0)
			{
				prime = prime * -1;
			}

			int k = prime / 2;
			for ( int j = k; j > 0; j-- )
			{
				if ( prime == 0 || prime == 1 )
				{
					prime[i] = 1;  // the errors occur here
				}
				else if ( prime > 1 )
				{
					if ( (prime % j == 0) && j > 1 )
					{
						prime[i] = 1;  // and here.
					}
				}
			}
		}


This isn't the full program, just to where the errors occur. I'm sure it's just something small that I've left out, but I just can't figure out what it is.
prime was declared again in the scope of your for loop, it is an int and your trying to subscript it...

line 24:
int prime[30];
line 90:
int prime = values[i];

I think you can find prime like this:

1
2
3
4
5
6
int num = 9;
int isPrime;

isPrime = num % 2;
if(isPrime = 1)
  cout << "Number is Prime!" << endl;


Last problem I saw was that you loop thru prime[] to assign all to 0 but when you declare a integer array without defining it it is default initialized to 0.

Hope that helps!
Last edited on
You need to get out of the habit of declaring variables inside of your loops, It's not a good practice. I beleve the problem you are having is on line 90. You already declared prime as an array on line 24.
Thanks for the help, don't know why I didn't realize that I had named a variable the same name as my array. I'm new to programming so declaring the variable inside the for loop was because it was to only be used for calculations within that for loop to keep from permanently changing outside variables.
While you should declare where your going to use it, it is error prone to do so within a loop.
Topic archived. No new replies allowed.