Thread: using data returned from fucntion in one cpp file to others in a different cpp file

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    using data returned from fucntion in one cpp file to others in a different cpp file

    Hi,

    I am supposed to create a project with two different cpp files, the first cpp file arrayallocator.cpp is supposed to dynamically allocate an array of integers and return a pointer to the array. The program should ask the user how many test scores they want to enter. So I think I got that part right, but here's the next part. In the second cpp file, I am supposed to take that information that the user entered and use that to get the test scores, have the test scores sorted in ascending order, display the sorted array, and then average and display the average. I think I have it mostly figured out but the thing I have a problem with, is the values that arrayallocator returns. Can a function in one cpp file return values to a function or functions in another cpp file? Here is the code for arrayallocator.cpp
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int arrayAllocate();
    
    int main ()
    {
    
    arrayAllocate();
    return (*array,*numElem);
    
    }
    
    int arrayAllocate()
    {
    	int *numElem;
    	int *array;
    	array = new int;
    	numElem = new int;
    	cout << "How many test scores do you wish to enter?:\n";
    	cin >> *numElem;
    	array = new int[*numElem];
    	if (array ==NULL)
    	{
    		cout << "Error allocating memory\n";
    		
    	}
    	return (*array,*numElem);
    }
    testaveraging.cpp
    Code:
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    
    int getScores(int *[], int);
    void showArrayPtr(int *[], int);
    void arrSelectSort(int *[], int);
    
    
    int main()
    {
    	getScores(array, numElem);
    	
    return 0;
    }
    
    int getScores(int *array[], int size)
    {
    	double total = 0, average;
    
    	cout << "Enter the test scores below\n";
    	for (int count = 0; count < numElem; count++)
    	{
    		cout << "Test "<< (count + 1) << ": ";
    		cin >> array[count];
    	}
    	arrSelectSort(array, numElem);
    	showArrayPtr(array, numElem);
    	for (int count = 0; count < numElem; count++)
    	{
    		total += static_cast<double>(array[count]);
    	}
    	average = total / numElem;
    	cout << fixed << showpoint << setprecision(2);
    	
    	cout << "Average Score: "<< average << endl;
    
    	return 0;
    
    }
    
    void arrSelectSort(int *array[], int size)
    {
    	int startScan, minIndex;
    	int *minElem;
    
    	for (startScan = 0; startScan < (size -1); startScan++)
    	{
    		minIndex = startScan;
    		minElem = array[startScan];
    		for (int index = startScan + 1; index < size; index++)
    		{
    			if (*(array[index]) < *minElem)
    			{
    				minElem = array[index];
    				minIndex = index;
    			}
    		}
    		array[minIndex] = array[startScan];
    		array[startScan] = minElem;
    	}
    }
    void showArrayPtr(int *array[], int size)
    {
    	cout << "The test scores sorted in ascending order are:\n";
    	for (int count = 0; count < size; count++)
    	{
    		cout << *(array[count]) << " ";
    	}
    	cout << endl;
    }
    The difficulty I am facing is that most of the examples in my book, can have variable declarrations in main, but I can only call functions in main, and I cannot use global variables.

    Could someone please help me cuz I am going cross eyed from trying to figure out where i went wrong?

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    error messages

    Code:
    arrayallocator.cpp
    arrayallocator.cpp(18) : error C2065: 'array' : undeclared identifier
    arrayallocator.cpp(18) : error C2065: 'numElem' : undeclared identifier
    testaveraging.cpp
    testaveraging.cpp(20) : error C2065: 'array' : undeclared identifier
    testaveraging.cpp(20) : error C2065: 'numElem' : undeclared identifier
    testaveraging.cpp(33) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'int *' (or there is no acceptable conversion)
            
    testaveraging.cpp(43) : error C2440: 'static_cast' : cannot convert from 'int *' to 'double'
            There is no context in which this conversion is possible
    testaveraging.cpp(55) : error C2601: 'arrSelectSort' : local function definitions are illegal
            testaveraging.cpp(26): this line contains a '{' which has not yet been matched
    testaveraging.cpp(76) : error C2601: 'showArrayPtr' : local function definitions are illegal
            testaveraging.cpp(26): this line contains a '{' which has not yet been matched
    testaveraging.cpp(84) : fatal error C1075: end of file found before the left brace '{' at 'testaveraging.cpp(26)' was matched
    Generating Code...
    Build log was saved at "file://Debug\BuildLog.htm"
    8Aziz - 9 error(s), 0 warning(s)

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    First, you only want one main function. Pick one of the files to hold your main function. Then put the prototypes for the functions in the other file into a header and #include that header file in the first cpp file. That way the code in the first cpp file knows what functions from the second cpp file are available for it to call.

    Second, you cannot return two different variables like you did in arrayAllocate. You should probably just pass the array pointer by reference to the arrayAllocate function so that you can have it allocate the right amount and return the size (or some other variation of passing by reference and returning information). The point is that you have to return the pointer to the new memory and the size of the new data to the function that calls arrayAllocate.

    The reason your code is giving errors is because you are trying to use variables that haven't been declared. You declare array and numElement inside arrayAllocate, which means those variable names only work inside that function. You have to declare other variables and give them the correct values (the results of arrayAllocate) inside your main function so that they can be used outside arrayAllocate.

    Also, you need to fix how you allocate space for the array. To dynamically allocate an array you need to use [] and put the size inside. There is also no reason to make numElements a pointer to an int. Start off with a simple, single file program that just attempts to allocate the memory correctly and get it to work before continuing on with the other stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM
  4. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM