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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:07:31+00:00 2026-06-08T14:07:31+00:00

I’m working on a project for school. It simulates students buying soda from a

  • 0

I’m working on a project for school.
It simulates students buying soda from a vending machine.
There is a class called Card that is a member in the class Student.
That is,

Every student has a card, which makes sense.

class Student {
public:
    Student( Office &cardOffice );
    ~Student();
    bool action();
    private:
    Office* studentOffice;          // stores cardoffice.
    Card* card;                 // stores card
};

A student’s card is created via a call to the studentOffice.create() function. That function returns a card.

Card* Office::create( int id, int money ) {
    Card* card = new Card();
    card->id = id;
    card->amount = money;
    return card;
}

Students call a function in the class VendingMachine called action() to buy food. The buy function in VendingMachine returns a type enumeration from the Status enum in the VendingMachine class.

There is a prng, generating a random number from 0 – 9. The assignment says that there is a 1 in 10 chance of the student’s card being destroyed. And he/she will obtain a new one the next time student.action() is called.

VendingMachine::Status VendingMachine::buy(Card* &card)
{
    if(prng(9) == 0) // generates number from 0-9
    {
        delete card;
    }
    return status;
}

Originally, I was thinking to check in the student’s action() routine to see if the card is NULL, (if it was deleted), and create a new one if that happens. However, I know the code gets to the delete card portion, but it fails when checking that the card is NULL. So that must mean the card is not null, which means the delete didn’t work.

But I also noticed that the card that is passed in is of type

Card* &card

I was then thinking of using a call with the “this” pointer as I know the student is what called this routine and “this” will point to the object that called it according to:

It points to the object for which the member function is called.
from http://msdn.microsoft.com/en-us/library/y0dddwwd(v=vs.80).aspx

However, if I do:

if(prng(9) == 0)
{
    delete this->card;
}

it gives me this error when running my makefile:

error: class VendingMachine has no member named card

Which is true, it doesn’t. Is the compiler assuming that a VendingMachine will call this method? Because the student does.

  1. Maybe I should add a student to every vending machine and delete the card from that member instead? I would strongly prefer not doing this, as there are multiple students and that would mean I need to store them all if they are assigned to this vending machine. Although, if it comes down to it, I could do it this way.

  2. If the delete card happened, but the card is not NULL, what exactly went down when I deleted the card?

  3. How would I go about deleting the card?

Thanks!

EDIT: After applying the changes, the code is now:

if(prng(9) == 0)
{
    cout << "Destroying card" << endl;
    delete card;
    card = NULL;
    cout << "Card Destroyed" << endl;
    }

Unfortunately, I get a segfault and that is probably because I’m accessing a destroyed card that doesn’t exist. Because Destroying card and Card Destroyed is displayed,

But the cout I have in this call is not showing up:

    if(card == NULL)
    {
        cout << "CARD DESTROYEDADJIWJDOQIODJWDIOJWQODWODIQODJWJOWDW" << endl;
        card = studentOffice->create(id, 5);
    }

So apparently the card is still not NULL? This is weird.

EDIT2: I think I know where the problem is, and why there’s a segfault. Working on it right now.

EDIT3: Solved by rearranging the order on the calls that used card when it was destroyed.

  • 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-08T14:07:32+00:00Added an answer on June 8, 2026 at 2:07 pm

    In method buy you should delete the pointer and set it to NULL (delete does not automatically set the pointer to NULL):

    VendingMachine::Status VendingMachine::buy(Card* &card)
    {
        if(prng(9) == 0) // generates number from 0-9
        {
            delete card;
            card = NULL;
        }
        return status;
    }
    

    That is the reason why the pointer is passed by reference (so you can assign NULL to the original pointer and not a copy of it).

    Besides that, this->card does not compile, since card belongs to class Student, not to VendingMachine. From VendingMachine‘s perspective, it is just a parameter in method buy.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.