How to transfer value of int to another function

Nov 22, 2014 at 6:22am
Lets say I have two functions, for example I'm trying to create a fixed size array.
1
2
3
4
5
6
7
8
9
10
11
12
  int Recursion::fixedSize(int arraySize){
	int arrayMax=1000;
	cout<<"What size do you want your array? (choose a number less than 1000): ";
	cin>>arraySize;
	if(arraySize>arrayMax){
		cout<<"Your array size is too big!";
		exit(0);
	}
	else{
		return arraySize;
	}
}


Now I need to use the value of arraySize to enter a certain amount of integers. For example this code:
1
2
3
4
5
6
7
8
9
int* Recursion::arrayValue(int arraySize){
	int value;
	for(int i=0;i<arraySize;i++){
		cout<<"Enter the "<< (i+1) <<" value of the array: ";
		cin>> value;
		arr[i]=value;
	}
		return arr;
}


But when I put cout<<arraySize; at the end of the function caller in my main, it gets the prints out 0.

Also I was wondering how I can link arraySize to my int arr[], which is private.
1
2
3
4
5
6
7
class Recursion{
	private:
		int arr[];
	public:
		Recursion();
		int fixedSize(int arraySize);
		int*arrayValue(int arraySize);


Sorry if this is a lot of questions but thank you all in advance.
Last edited on Nov 22, 2014 at 6:55am
Nov 22, 2014 at 6:51am
How did you implement them?
Nov 22, 2014 at 6:52am
In my main or in my header file?
This is the code for my main:
1
2
3
4
5
6
7
8
9
10
11
12
13
main(){
	int n=10;
	int m=9;
	int arraySize;
	int large;
	int start=0;
	int endpoint=10;
	int k;
	cout<<"This program will work with recursions and arrays"<<endl;
	Recursion a;
	a.fixedSize(arraySize);
	a.arrayValue(arraySize);
	cout<<endl<<"**************************************"<<endl;
Last edited on Nov 22, 2014 at 6:53am
Nov 22, 2014 at 7:03am
in your main.
btw, since your arr is private, you can have a function that returns its value say int return arr(number){return arr[number]};
Nov 22, 2014 at 7:09am
something like this?
1
2
3
4
int Recursion::getarr(){
size=arraySize;
return arr[size];
}
Nov 22, 2014 at 7:19am
That^ would return an error because its out of bounds(size=arraySize-1) and it also would return only the last variable, *which would not be very ideal.
For more info on how to return arrays, see here:
http://www.tutorialspoint.com/cplusplus/cpp_return_arrays_from_functions.htm
javascript:editbox2.editSend()
Last edited on Nov 22, 2014 at 7:20am
Topic archived. No new replies allowed.