This is an assignment i am working on for class. To make a long story short i am attempting to call the allocate() function in main to execute. I keep getting the error that testArray was not declared in this scope”. I am a bit confused, If my return statement is being called through my function than how can it not be in scope? I hope this is making sense, I am confusing myself with these pointers and functions. Any suggestions are appreciated, thanks.
#include <iostream>
using namespace std;
int* allocate(int&);
int* allocate(int &numOfScores)
{
int *testArray;
//prompt user for scores
cout << "How many test scores would\n";
cout << "you like to process: " << endl;
cin >> numOfScores;
//dynammically allocate an arrray to hold the scores
testArray = new int[numOfScores];
//get the scores from user
for(int count = 0; count < numOfScores; count++)
{
cout << "Enter Score: " << endl;
cin >> testArray[count];
}
//release the memory that was allocated for *ptr
delete [] testArray;
testArray = 0;
return testArray;
}
int main()
{
allocate(testArray);
return 0;
}
This is because you are refering to testArray as though it was defined in the current function what you actually need is
However I would discourage this style of coding. You should looking into using std::vectors.
for example
http://en.cppreference.com/w/cpp/container/vector