C++ Error in Array variable (int)

Hello,

C++ is a rather new programming language to me, But I have been working with different languages which are making things slightly easier to me.
Nevertheless, I'm currently stuck on a (I believe) obvious issue which I myself don't recognize.

I'm getting an error saying:
"Error: Expression must have pointer-to-object type"
on these spots:

cin >> Numbers [NumIncrease]; //Manually Insert random numbers
cout << Numbers [NumIncrease] << " - "; //Display Inserted numbers (on one line)

Since I have no clue where the error might be; I'll just give the entire code. It's not too long anyway.

code: NOTE, UPDATED VERSION IS IN LATER REPLY!
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
//Include files
#include <iostream>
#include <string>
#include "conio.h"
using namespace std;


//Main Program
void main()
{
	//Set Initials
	system("color 0f");
	int LimitMin, LimitMax, Counter, Amount, Numbers, NumIncrease;

	//Set values to initials (not sure if C++ requires, but anyway - just in case)
	LimitMin	=	0	;
	LimitMax	=	100	;
	Amount		=	0	;
	Counter		=	0	;
	Numbers		=	0	;
	NumIncrease	=	0	;

	//Start Introduction
	cout << endl;
	cout << endl;
	cout << "______________" << endl;
	cout << endl;
	cout << "|Use of Arrays|" << endl;
	cout << "______________" << endl;
	cout << endl;
	cout << endl;
	cout << "How many numbers do you want to insert? (<100)" << endl;

	//Start amount of numbers inserting test
	Testamount:

	cin >> Amount;
	if (Amount >= LimitMin && Amount <= LimitMax) //amount accepted
	{
		cout << endl;
		NumIncrease=0;

		//Starting the number input
		Startcount:
		if (NumIncrease <= Amount)
		{
			cout	<< "Number " << NumIncrease << ": ";
			cin	>> Numbers [NumIncrease]; //Manually Insert random numbers
			cout	<< endl;
			NumIncrease +=1;
			goto Startcount;
		}
		else //if finished inserting numbers
		{
			cout << endl;
			cout << endl;
			cout << "Inserted Numbers: " << endl;
			cout << endl;

			NumIncrease=0;

			//Draw Inserted Numbers
			Draw:
			if (NumIncrease <= Amount)
			{
				cout << Numbers [NumIncrease] << " - "; //Display Inserted numbers (on one line)
				//goto Draw;
			}
			else
			{
				cout << "Blehh, program finished for so far"; //Will continue with real code when error is solved!
				goto Endprogram;
			}
		}
	}

	else //if too many numbers inserted

	{
		cout << endl << "The number you chose is not allowed! it has to be between 0 and 100." << endl << "Try Again: ";
		goto Testamount;
	}

	Endprogram:
	//Pause Program and return basic colors
	system("pause");
	system("color 08");
}


PS. The point of it is that the program requests for a certain amount of numbers (between 0 and 100), each number (sort of id) will get a number linked to it. then at first, these numbers will all be written out and eventually another line that cancels out the numbers that have been used multiple times.

eg. insert amount = 5
number1=10
number2=29
number3=10
number4=29
number5=4

first it should draw the numbers:
10 - 29 - 10 - 29 - 4
and then cancel out the ones that have been used multiple times
10 - 29 - 4

I Hope I've put this post in the right place. My apologies if not!

Thanks in advance!
Last edited on
Well, you see, Numbers is not an array, vector, or deque, so the [ ]s aren't defined for a single integer. You'll probably want to make Numbers an int*, and then use new[Amount]; to reserve memory for your pointer to point to.
http://cplusplus.com/doc/tutorial/dynamic/

Also, I'm pretty sure you're abusing goto. Consider a while loop instead? :/
http://cplusplus.com/doc/tutorial/control/#while

Also, void main() should be int main(). That's just what the standard says, sorry. :)

-Albatross
Last edited on
Numbers is not an array. Use std::vector:
http://www.cplusplus.com/reference/stl/vector/

Do not use goto. That's the first thing you should fix.
It makes following the program's flow a herculean task even for such a short program and will pretty much kill any chances for you getting help on problems that result from structural or logical issues in your code.
It will also kill any chances of you being able to understand your own code when you look at it again in a few months.

int is the only valid return type of main(), by the way.

Edit: I'm really getting slow :(
Last edited on

mhmm, That error is gone :)

I've Updated the code to the following:

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

//Include files
#include <iostream>
#include <string>
#include "conio.h"
using namespace std;


//Main Program
int main()
{
	//Set Initials
	system("color 0f");
	int LimitMin, LimitMax, Counter, Amount, NumIncrease;
	int * Numbers;

	//Set values to initials
	LimitMin		=	0		;
	LimitMax		=	100		;
	Amount			=	0		;
	Counter			=	0		;
	Numbers			=	0		;
	NumIncrease		=	0		;


	//Start Introduction
	cout << endl;
	cout << endl;
	cout << "______________" << endl;
	cout << endl;
	cout << "|Use of Arrays|" << endl;
	cout << "______________" << endl;
	cout << endl;
	cout << endl;
	cout << "How many numbers do you want to insert? (<100)" << endl;

	//Start amount of numbers inserting test
	cin >> Amount;

	while (Amount <=0 || Amount >100)//read next line; anyone will understand (english speaking 'course:)
	{
		cout << endl << "The number you chose is not allowed! it has to be between 0 and 100." << endl << "Try Again: ";
		cin >> Amount;
	}
	cout << endl;


	//HERE WE GO!
	//if (Amount >= LimitMin && Amount <= LimitMax) //amount accepted

	//Starting the number input
	//Start at 1, +1 each loop, Limited to amount 
	for (NumIncrease=1; NumIncrease <= Amount; NumIncrease+=1) 
	{
		cout	<< "Number " << NumIncrease << ": ";
		Numbers = new (nothrow) int[NumIncrease];
		cin		>> Numbers [NumIncrease]; //Manually Insert random numbers
		cout	<< endl;
	}

	//Create a pretty pretty white(aka black-)space +info
	cout << endl << endl << "Inserted Numbers: " << endl << endl;

	//Draw Inserted Numbers
	for (NumIncrease=1; NumIncrease <= Amount; NumIncrease+=1)
	{
		cout << Numbers [NumIncrease] << ", "; //Display Inserted numbers (on one line)
	}
	//delete[] Numbers;
	//return 0;


	cout << endl << endl << "Next Step :D" << endl;
	cout << "Blehh, program finished for so far"; //Will continue with real code when error is solved!		


	//Pause Program and return basic colors
	system("pause");
	system("color 08");
}


'goto' spams have been fully terminated and I've made an int * out of "Numbers".

however this time, it only remembers the last inserted number correctly, whereas the other numbers all are -842150451?¿!?
I have no clue how it comes up with those values; But do know it's not one I like.
Does anyone know what I'm doing wrong?
Is the problem already where I insert the numbers? or where it's drawing them? or both.. pff
Use an array instead of a pointer, or make a vector.
closed account (D80DSL3A)
Your are doing the allocations incorrectly. Allocate an array large enough to hold all values ONCE then store all of the numbers there. Please study how dynamic memory allocation works.
Here's a corrected version of your code starting from line #53:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Numbers = new (nothrow) int[Amount];// an array large enough to store all values
for (NumIncrease=0; NumIncrease < Amount; NumIncrease+=1) // start at 0 not 1
	{
		cout	<< "Number " << NumIncrease << ": ";		
		cin		>> Numbers [NumIncrease]; //Manually Insert random numbers
		cout	<< endl;
	}

	//Create a pretty pretty white(aka black-)space +info
	cout << endl << endl << "Inserted Numbers: " << endl << endl;

	//Draw Inserted Numbers
	for (NumIncrease=0; NumIncrease < Amount; NumIncrease+=1)
	{
		cout << Numbers [NumIncrease] << ", "; //Display Inserted numbers (on one line)
	}
	delete[] Numbers;// OK to delete array since you are done with it 
Well, okay. In all honesty, I didn't suggest a vector because I wasn't sure if it was allowed or not. Ask your instructor, and if he allows you to use them, use them.

-Albatross
Topic archived. No new replies allowed.