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 8989281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:13:07+00:00 2026-06-15T22:13:07+00:00

I got strange program behaviour and crashing. I created class ‘List’, with array of

  • 0

I got strange program behaviour and crashing.

I created class ‘List’, with array of pointers to objects of class ‘Student’. I observe that I succesfully called ‘Student’ object in ‘List’ constructor. But I’m unable to call ‘Student’ object from any other ‘List’ methods.

I even checked the same line for testing inside ‘List’ constructor and ‘push’ method, resulting in program crash.

Here is testing line: cout << (studentBox[numb] -> getRef()) << endl;

Here is the problematic part of code:

#include <iostream>

using namespace std;

class Student
{
    private:
        int referenceNumb;
        int markNumb;
    public:
        Student();
        Student(int, int);
        ~Student();
        int getRef();
        int getMark();
};

class List
{
    private:
        int numb;
        Student* studentBox[1000];
    public:
        List();
        ~List();
        void push(int, int);
        int getAVG();
};

int main()
{
    List* base = NULL;
    int x, y;
    char mod;
    do
    {
        cout << "Waiting for orders." << endl;
        cout << "1 - Quit," << endl;
        cout << "2 - Add new student," << endl;
        cout << "3 - Calculate average mark," << endl;
        cout << "0 - Create new list." << endl;
        cin >> mod;

        switch(mod)
        {
            case '0': base = new List(); break;
            case '1': cout << "Bye."; break;
            case '2':
                if(base != NULL)
                {
                    cout << "Specify student's reference number: "; cin >> x;
                    cout << "Specify student's mark: "; cin >> y;
                    base->push(x, y);
                }
                else cout << "List does not exist!" << endl;
                break;
            case '3':
                if(base != NULL)
                {
                    cout << "Average mark is equal: " << base->getAVG();
                }
                else cout << "List does not exist!";
                cout << endl;
                break;
            default: cout << "Correct command required!" << endl; break;
        }
    }
    while(mod!='1');
    return 0;
}

Student::Student()
{
    referenceNumb = NULL;
    markNumb = NULL;
}

Student::Student(int r, int m)
{
    referenceNumb = r;
    markNumb = m;
}

Student::~Student()
{
    referenceNumb = NULL;
    markNumb = NULL;
    cout << "pusto." << endl;
}

int Student::getRef()
{
    return referenceNumb;
}

int Student::getMark()
{
    return markNumb;
}

List::List()
{
    int numb = 0;
    studentBox[numb] = new Student();

    cout << (studentBox[numb] -> getRef()) << endl;
}

List::~List()
{
}

void List::push(int x, int y)
{
    cout << (studentBox[numb] -> getRef()) << endl;

    if(studentBox[numb] != NULL)
    {
        studentBox[numb] = new Student();

        cout << (studentBox[numb] -> getRef()) << endl;
    }
    else cout << "Hujnia" << endl;
}

int List::getAVG()
{
    int temp = 0;
    for(int i=0; i<numb; i++)
    {
        temp += studentBox[i]->getMark();
    }

    return (temp / numb);
}
  • 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-06-15T22:13:09+00:00Added an answer on June 15, 2026 at 10:13 pm

    There are a couple of major problems that I see. The first is that the numb member of the List isn’t initialized.

    List::List()
    {
        int numb = 0;  // this creates a new local variable called numb, it doesn't
                       // initialize the numb member.
        studentBox[numb] = new Student();
    
        cout << (studentBox[numb] -> getRef()) << endl;
    }
    

    Do this instead:

    List::List()
      : numb(0) // initialize the numb member to zero.
    {
        studentBox[numb] = new Student();
    
        cout << (studentBox[numb] -> getRef()) << endl;
    }
    

    Another problem is that numb appears to be meant to indicate the size of the list, but it is never changed when a new student is added.

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

Sidebar

Related Questions

Got a strange problem created a little stored proc which need to execute a
I'm writing custom security attribute and got strange compiler behaviour... When I'm using the
I got a strange beaviour in my program. I wrote a test which always
I got a strange error with this program, It download a photo from internet,
Today I got a really strange problem. Try to execute this C# code: class
I've got kind of strange problem. I was working on a simple program which
I've got a strange problem. I have a .NET program and my process logic
I've got a strange thing goin on here. I'm designing a small program with
I got strange behavior for iOS App. My App crashes only when it is
I got strange behavior when I tried to test my navigator.geolocation.getCurrentPosition web page. Here

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.