9 Replies - 689 Views - Last Post: 06 May 2009 - 02:29 PM Rate Topic: -----

#1 Ryan2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 06-March 09

Problem with reading values into an array

Posted 06 May 2009 - 12:00 PM

I wanted to make a code that can ask the user how many values they want to enter (any resonable value) and then put each value into an array. Here is the code that i came up with.

#include <iostream>
#include <conio>

using namespace std;

int main()
{

const int objectsinarray = 2;

int arrayofnumbers [objectsinarray];
int increment = 0;

for (; increment <= objectsinarray; increment ++)
	{
	cout << "please enter number in array: " << increment << " of " << objectsinarray << endl;
	cin >> arrayofnumbers[increment];
	clrscr();
	}

for ( increment = 0; increment <= objectsinarray; increment ++)
	{
	cout << arrayofnumbers[increment] << endl;
	}
	
getch();
return 0;
}



One problem though, I cannot use "cin >>" with a constant variable. (or so it seems). I want to basically do this.

const int objectsinarray;

cin >> objectsinarray;

......



But I get three compile errors;

ErrorE2304:constant varibale 'objectsinarray' must be initialized in function main()
ErrorE2094:'operator>>' not implemented in type 'istream' for arguments of type 'int' in function main()
ErrorE2313:Constant expression required in function main()

I tried to think ways around this by taking input to a different variable then assigning that variable to "objectsinarray" . eg

int temp;
cin >> temp;

const int objectsinarray = temp;



But i get an error. Is there any way around this at all? :crazy:

thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with reading values into an array

#2 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: Problem with reading values into an array

Posted 06 May 2009 - 12:50 PM

Does this help:

int main()
{
	const int NUM = 2;

	int arr[NUM];

	for (int incr = 0; incr < NUM; incr++)
	{
		cout << "please enter number in array: " << incr << " of " << NUM << endl;
		cin >> arr[incr];
	}

	for ( int incr = 0; incr < NUM; incr++)
	{
		cout << arr[incr] << endl;
	}
	
	return 0;
}



Small tip don't use such long variables, it is easier to read if the local variables are smaller (well that's what I find)
Was This Post Helpful? 0
  • +
  • -

#3 Ryan2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 06-March 09

Re: Problem with reading values into an array

Posted 06 May 2009 - 12:59 PM

View PostAndyH1963, on 6 May, 2009 - 11:50 AM, said:

Does this help:

int main()
{
	const int NUM = 2;

	int arr[NUM];

	for (int incr = 0; incr < NUM; incr++)
	{
		cout << "please enter number in array: " << incr << " of " << NUM << endl;
		cin >> arr[incr];
	}

	for ( int incr = 0; incr < NUM; incr++)
	{
		cout << arr[incr] << endl;
	}
	
	return 0;
}



Small tip don't use such long variables, it is easier to read if the local variables are smaller (well that's what I find)


Not really im afraid, what i want is for the user to decide the value of "num". Basically if i have an array for instance.

int array [];



i want the user to decide the value of the number of objects in the array. like this

const int num;

cin >> num;

int array [num];

//returns an error



But there seems to be no easy way of doing this, you must use a constant variable if you want to use a hard coded varibale like "const int num = 2". That works but using cin doesnt...

Is there any answer to this problem?

(by the way, I dont know why i used a long variable there. I dont always, i guess i just couldnt think of anything else.)

This post has been edited by Ryan2: 06 May 2009 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Problem with reading values into an array

Posted 06 May 2009 - 01:01 PM

You have to use dynamically allocated memory.

int num;
cin >> num;
//allocate
int* arr = new int[num];
//do stuff

//free memory
delete[] arr;


Was This Post Helpful? 0
  • +
  • -

#5 Ryan2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 06-March 09

Re: Problem with reading values into an array

Posted 06 May 2009 - 01:32 PM

View PostKYA, on 6 May, 2009 - 12:01 PM, said:

You have to use dynamically allocated memory.

int num;
cin >> num;
//allocate
int* arr = new int[num];
//do stuff

//free memory
delete[] arr;



Hey. I tried to use your method, ending up with this.

#include <iostream>
#include <conio>

using namespace std;

int main()
{
int arraynum;
int arrayofnumbers [arraynum];
int increment = 0;
int* arr = new int[arraynum];

cin >> arraynum;

for (; increment <= arraynum; increment ++)
	{
	cout << "please enter number in array: " << increment << " of " << arraynum << endl;
	cin >> arrayofnumbers[increment];
	clrscr();
	}

for ( increment = 0; increment <= arraynum; increment ++)
	{
	cout << arrayofnumbers[increment] << endl;
	}
	
delete[] arr;
getch();
return 0;
}



I still get an error saying I need to use a constant expression. When i use a costant, i get another error. Im not sure what i did wrong there. Could you give me more advice?

thank you
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Problem with reading values into an array

Posted 06 May 2009 - 01:40 PM

You have to get the input first.

int arraynum;
int increment = 0;

cin >> arraynum;

int* arr = new int[arraynum];


This post has been edited by KYA: 06 May 2009 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

#7 redkid  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 32
  • Joined: 23-April 09

Re: Problem with reading values into an array

Posted 06 May 2009 - 01:41 PM

You have to take the input first and then allocate that much memory.
cin >> arraynum; //this first
int* arr = new int[arraynum]; //then this


Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Problem with reading values into an array

Posted 06 May 2009 - 01:46 PM

Just to show that the world is full of different opinions I think your long but meaningful variable names are a good idea but I would give you the tip that camelCase is a good idea instead of all lower case.
http://en.wikipedia.org/wiki/CamelCase


Are you aware that there is an answer to your challenge in C++ in the form of vectors?
http://www.cplusplus...nce/stl/vector/

If you are avoiding using vectors on purpose that's fine but thought it was worth mentioning.
Was This Post Helpful? 0
  • +
  • -

#9 Ryan2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 06-March 09

Re: Problem with reading values into an array

Posted 06 May 2009 - 02:17 PM

View PostKYA, on 6 May, 2009 - 12:40 PM, said:

You have to get the input first.

int arraynum;
int increment = 0;

cin >> arraynum;

int* arr = new int[arraynum];



wow, nice one, it works. Problem is, I dont really know how it works...

Could you give me a brief explination or a good link to info on this please?

thanks
Was This Post Helpful? 0
  • +
  • -

#10 Ryan2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 06-March 09

Re: Problem with reading values into an array

Posted 06 May 2009 - 02:29 PM

View Postjanotte, on 6 May, 2009 - 12:46 PM, said:

Just to show that the world is full of different opinions I think your long but meaningful variable names are a good idea but I would give you the tip that camelCase is a good idea instead of all lower case.
http://en.wikipedia.org/wiki/CamelCase


Are you aware that there is an answer to your challenge in C++ in the form of vectors?
http://www.cplusplus...nce/stl/vector/

If you are avoiding using vectors on purpose that's fine but thought it was worth mentioning.


I wasnt avoiding vectors, thats the first time iv'e heard of them. Im very curious though.

Im not sure if i should make a definate naming convension for my variables, i think i should just make them readable. Not to long or short, just understndable to a certain degree.

thank you for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1