Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7672647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:20:02+00:00 2026-05-31T16:20:02+00:00

This is an assignment i am working on for class. To make a long

  • 0

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;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:20:04+00:00Added an answer on May 31, 2026 at 4:20 pm

    This is because you are refering to testArray as though it was defined in the current function what you actually need is

    #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()
    {   
        int* testArray;
        int numberOfScores;
        testArray=allocate(numberOfScores);
        delete[] testArray;
        return 0;
    }
    

    However I would discourage this style of coding. You should looking into using std::vectors.

    for example

    size_t nun_scores;  
    std::cin >> nun_scores;
    std::vector<int> scores(num_scores);
    //so on
    

    http://en.cppreference.com/w/cpp/container/vector

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on a class assignment and when I run this code function paycheck1($hours,$payrate,$othours,$otrate){ if
ok so this is the assignment that I'm working on: Implement a class Student
I've been working on this school assignment. The assignment told us to make an
I've been working on this assignment, where I need to read in records and
For this assignment I had to create my own string class. I initially wrote
I'm working on an assignment has a class hierarchy like blow: abstract EventClass someEventClass1
I am working on a short java assignment that I have been set. The
I'm working on this assignment: http://www.cs.colostate.edu/~anderson/ct310/index.html/doku.php?id=assignments:assignment_2 I'm building a binary tree in Javascript. Basically
We are working on a toy operating system as a assignment for a class.
Working on a class assignment in C#, I came across a program crash without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.