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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:22:16+00:00 2026-06-10T22:22:16+00:00

I am writing a Little Man computer simulation and i want to overload the

  • 0

I am writing a Little Man computer simulation and i want to overload the Indexing operator []. I have create a class called LMC and have done the following:

#include <iostream>

using namespace std;

class LMC
{
   public:
       LMC();
       void display();
       int& operator[](int index);
       ~LMC();
   private:
       int **array;
};

LMC::LMC()
{
   array = new int*[100];
   for(int i = 0; i < 100; i++)
   {
       array[i] = new int[3];
   }
   return array;
}

void LMC::display()
{
    for(int i = 0; i < 100;i++)
    {
       for(int j = 0; j <3;j++)
       {
          array[i][j] = 0;
          array[i][2] = i;
          cout << array[i][j]<<" ";
       }
       cout << endl;
    }
 }
 int& LMC::operator[](int index)
 {
    return array[index][2];
 }

  LMC::~LMC()
  {
     for(int i =0; i < 100 ; i++)
     { 
        delete [] array[i];
     }
     delete [] array;
     array = NULL;
  }

  int main()
  {
     LMC littleman;
     while(true)
     {
         int mailbox;
         int function;
         cout << "What is Mailbox number?" << endl;
         cin >> Mailbox;
         cout << "What is the function you want to use?" <<endl;
         cin >> finction;
         //the function is numbers eg 444 and 698;
         littleman.display();
         littleman[Mailbox] = function;
      }
     return 0;
}

I can run the program with no error. When i state that mailbox = 0 and function = 123 the is no problem.

This is displayed:

0 0 0
1 0 0
2 0 0
3 0 0
//continuing to 99

This display is wrong. The following must be displayed:

0 0 123
1 0 0
2 0 0
//continuing to 99

Do i have a logical error or am i overriding the array to display the original and how can i fix it?

  • 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-10T22:22:18+00:00Added an answer on June 10, 2026 at 10:22 pm

    These lines

      array[i][j] = 0;
      array[i][2] = i;
    

    in LMC::display() destroy the contents of the array you’re trying to display.

    Furthermore, there’s an extra semicolon at the end of void LMC::display();, so your code is not supposed to compile.

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

Sidebar

Related Questions

I am writing a little app similar to omegle. I have a http server
I want to functionalize more of my jQuery code, instead of writing little standalone
I´m writing a little audio player in c#. Now I want drag and drop
I'm writing a little program in JavaScript in which I want to parse the
Im writing little widget that shows time, date.. with many settings. And I want
I am writing a little gui application with Tkinter, which requires me to have
I'm writing a little thing for an assignment, and I have to manage TCP
I'm writing a little piece of code where I'll have to insert values into
I'm writing a little single page app (using knockout.js :-)) whereby i have a
I have been writing a little application where Form1 opens, checks that a config

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.