I'm new at C++ programming and am working on an assignment. I've managed to compile my codes alright but got an error while testing the program.

The error surfaced halfway through viewing details of a linked list, where suddenly, the program started printing weird characters while the computer emit beeping sounds. The output of weird characters and the beeping sounds will carry on unless I forcefully terminate the program or press the 'Pause/Break' key.

Are there any common reasons / solutions for this error? I've attached a screenshot of the program with the error. The codes are quite long tho (and very messy too :$ ), but if it is needed, I can upload it.

p.s. I've worked with this problem for about a week now and this is kind of my last resort before I dump what I have and start all over with a different program logic. Please help!

The end condition for the loop where you iterate over the list might be wrong or your list is not terminated properly (or something else).

If you've been wondering about this for a week now, it is probably better to post code that is relevant to the problem.

@mitrmkar: thanks for the reply. But I'm pretty sure that the problem is not with the loops.

I've posted the codes below. To facilitate the problem solving process, I've removed a huge chunk of the codes and left only the parts that are causing the problem.

The problem will appear if you perform the following tasks:
1.) From the main menu, check-out a video (e.g. of input: "Movie 1" and "Customer 1")
2.) From the main menu, check-in that video (e.g. of input: "Movie 1")
3.) From the main menu, view all videos
After performing step 3, the said error will surface.

I hope what I've written is clear enough...and btw, I'm using Netbeans C++, if that makes a difference...

#include <iostream>
#include <string>

using namespace std;

class Video {
protected:
    string videoTitle, movieStar1, movieStar2, movieProducer, movieDirector, movieProductionCo;
public:
    string getVideoTitle() { 
        return videoTitle;
    }
    string getMovieStar1() {
        return movieStar1;
    }
    string getMovieStar2() {
        return movieStar2;
    }
    string getMovieProducer() {
        return movieProducer;
    }
    string getMovieDirector() {
        return movieDirector;
    }
    string getMovieProductionCo() {
        return movieProductionCo;
    }	
    void displayDetails();
};

class AvailableVideo: public Video {
private: 
    int copiesInStock;
public:
    AvailableVideo* previous; 
    AvailableVideo* next; 
    static AvailableVideo* last;
    static AvailableVideo* first;

    AvailableVideo() { 
        
    }
    
    AvailableVideo(string title, string star1, string star2, string director, string producer, string productionCo, int numCopies) {
        videoTitle = title;
        movieStar1 = star1;
        movieStar2 = star2;
        movieDirector = director;
        movieProducer = producer;
        movieProductionCo = productionCo;
        copiesInStock = numCopies;
        
        next = NULL; 
        if (first == NULL) {
            first = this;
            previous = NULL;
        }
        else {
            last->next = this;
            previous = last;
        }
        last = this;        
    }    
    void setDetails() {
        cout<<"\n\nEnter the title of the movie: ";
        cin.ignore();	
        //ins>>videoTitle;		
        getline(cin, videoTitle);
        cout<<"Enter the name of the main actor: ";
        getline(cin, movieStar1);
        cout<<"Enter the name of the main actress: ";
        getline(cin, movieStar2);
        cout<<"Enter the director of the movie: ";
        getline(cin, movieDirector);        
        cout<<"Enter the producer of the movie: ";
        getline(cin, movieProducer);        
        cout<<"Enter the production company of the movie: ";
        getline(cin, movieProductionCo);
        cout<<"Enter the number of copies in stock: ";
        cin>>copiesInStock;
        
        next = NULL; 
        if (first == NULL) {
            first = this;
            previous = NULL;
        }
        else {
            last->next = this;
            previous = last;
        }
        last = this;          
    }
    int getCopiesInStock() {
        return copiesInStock;
    }
    AvailableVideo* getLast() {
        AvailableVideo* last = first;
        while (last->next != NULL)
            last = last->next;
        return last;
    } 
    void checkOutVideo() { 
        copiesInStock--;
    }
    void checkInVideo() {
        copiesInStock++;
    }
    void removeVideo(AvailableVideo *deleteVideo) {
        if (deleteVideo->previous == NULL)
            first = deleteVideo->next;
        else
            deleteVideo->previous->next = deleteVideo->next;
        if (deleteVideo->next == NULL)
            last = deleteVideo->previous;
        else
            deleteVideo->next->previous = deleteVideo->previous;
        delete deleteVideo;
    }
    void displayDetails() {
        cout<<"\n\nMovie Title:\t\t" <<videoTitle <<endl
                <<"Main Actor:\t\t" <<movieStar1 <<endl
                <<"Main Actress:\t\t" <<movieStar2
                <<"\nMovie Director:\t\t" <<movieDirector
                <<"\nMovie Producer:\t\t" <<movieProducer                
                <<"\nProduction Company:\t" <<movieProductionCo
                <<"\nNumber of Copies:\t" <<copiesInStock;            
    }
};

class RentedVideo: public Video { //a linked list to store all details of all videos rented out 
private:
    string custRentedTo; 
    
public:
    RentedVideo* next;
    RentedVideo* previous;
    static RentedVideo* last; 
    static RentedVideo* first;

    RentedVideo() { 
        
    }    
    RentedVideo(string title, string star1, string star2, string director, string producer, string productionCo, string custName) {
        videoTitle = title;
        movieStar1 = star1;
        movieStar2 = star2;
        movieDirector = director;
        movieProducer = producer;
        movieProductionCo = productionCo;
        custRentedTo = custName;
        
        next = NULL; 
        if (first == NULL) {
            first = this;
            previous = NULL;
        }
        else {
            last->next = this;
            previous = last;
        }
        last = this; 
    }    
    string getVideoTitle() { 
        return videoTitle;
    }
    string getCustRentedTo() {
        return custRentedTo;
    }
    RentedVideo* getLast(void) {
        RentedVideo* last = first;
        while (last->next != NULL)
            last = last->next;
        return last;
    } 
    void removeRentedVideo(RentedVideo *deleteVideo) { 
        if (deleteVideo->previous == NULL)
            first = deleteVideo->next;
        else
            deleteVideo->previous->next = deleteVideo->next;
        if (deleteVideo->next == NULL)
            last = deleteVideo->previous;
        else
            deleteVideo->next->previous = deleteVideo->previous;
        delete deleteVideo;
    }
    void displayDetails() {
        cout<<"\n\nMovie Title:\t\t\t" <<videoTitle
                <<"\nMain Actor:\t\t\t" <<movieStar1
                <<"\nMain Actress:\t\t\t" <<movieStar2
                <<"\nMovie Director:\t\t\t" <<movieDirector
                <<"\nMovie Producer:\t\t\t" <<movieProducer                
                <<"\nProduction Company:\t\t" <<movieProductionCo
                <<"\nCustomer Renting the Video:\t" <<custRentedTo;
    }
        
};

class Customer {
private:
    string rentedVideoTitle[3];
    string custName;
    string custPhoneNum;
public:
    Customer* next;
    Customer* previous;
    static Customer* last; 
    static Customer* first;
        
    Customer() { 
        
    }    
    Customer(string name, string phoneNum) {
        custName = name;
        custPhoneNum = phoneNum;
        for(int i=0; i<3; i++) 
            rentedVideoTitle[i] = "None";
           
        next = NULL; 
        if (first == NULL) {
            first = this;
            previous = NULL;
        }
        else {
            last->next = this;
            previous = last;
        }
        last = this;   
    }    
    void setDetails() {
        cout<<"\n\nEnter customer name: ";
        cin.ignore();
        getline(cin, custName);        
        cout<<"Enter customer phone number: ";
        getline(cin, custPhoneNum);
        for(int i=0; i<3; i++) 
            rentedVideoTitle[i] = "None";
        
        next = NULL; 
        if (first == NULL) {
            first = this;
            previous = NULL;
        }
        else {
            last->next = this;
            previous = last;
        }
        last = this;   
    }
    void setCustName(string name) {
        custName = name;
    }
    string getCustName() { 
        return custName;
    }
    string getCustPhoneNum() {
        return custPhoneNum;
    }
    Customer* getLast() 
    {
        Customer* last = first;
        while (last->next != NULL)
            last = last->next;
        return last;
    }
    int checkAvblRentSlot() {
        if (rentedVideoTitle[2] == "None")
            return 1;
        else
            return 0; 
    }
    void rentVideo(string videoToRentTitle) { 
        for (int i=0; i<3; i++) {
            if (rentedVideoTitle[i] == "None") {
                rentedVideoTitle[i] = videoToRentTitle;
                break;
            }
        }
    }
    void returnVideo(string videoTitle) {
        for (int i=0; i<3; i++) {
            if (rentedVideoTitle[i] == videoTitle) {                
                rentedVideoTitle[i] = "None";
                break;
            }
        }
    }
    void removeCustomer(Customer *deleteCustomer) {
        if (deleteCustomer->previous == NULL)
            first = deleteCustomer->next;
        else
            deleteCustomer->previous->next = deleteCustomer->next;
        if (deleteCustomer->next == NULL)
            last = deleteCustomer->previous;
        else
            deleteCustomer->next->previous = deleteCustomer->previous;
        delete deleteCustomer;
    }
    void displayDetails() { 
        cout<<"\n\nCustomer Name:\t" <<custName
                <<"\nCustomer Phone Number:\t" <<custPhoneNum
                <<"\nRented Video 01: " <<rentedVideoTitle[0]
                <<"\nRented Video 02: " <<rentedVideoTitle[1]
                <<"\nRented Video 03: " <<rentedVideoTitle[2];
    }
};

AvailableVideo* AvailableVideo::first = NULL;
AvailableVideo* AvailableVideo::last = NULL;
RentedVideo* RentedVideo::first = NULL;
RentedVideo* RentedVideo::last = NULL;
Customer* Customer::first = NULL;
Customer* Customer::last = NULL;
int intDecision;
char charDecision;
string title, name; 
void mainMenu();
void addNewVideo();
void addNewCustomer();
void viewAllVideos();
void checkOutVideo();
void checkInVideo();
void returnToMainMenu();

int main() {
    
    //creating objects to facilitate testing - actual program will retrieve all Video and Customer records from file
    AvailableVideo objVideo01("Movie 1", "Actor 1", "Actress 1", "Director 1", "Producer 1", "Production Company 1", 1);
    AvailableVideo objVideo02("Movie 2", "Actor 2", "Actress 2", "Director 2", "Producer 2", "Production Company 2", 2);
    AvailableVideo objVideo03("Movie 3", "Actor 3", "Actress 3", "Director 3", "Producer 3", "Production Company 3", 3);
    Customer objCust01("Customer 1", "1111-1111-1111");
    Customer objCust02("Customer 2", "2222-2222-2222");
    Customer objCust03("Customer 3", "3333-3333-3333");
    
    mainMenu();    
 
    return 0;
}

void mainMenu() { //Main Menu of the application
    system("CLS");
    cout<<"\nVIDEO RENTAL SYSTEM"
            <<"\n1 - to Add New Customer"
            <<"\n2 - to Add New Video"
            <<"\n3 - to Check-Out a Video"
            <<"\n4 - to Check-In a Video"
            <<"\n5 - to View All Videos"            
            <<"\n0 - to Exit the Application" 
            <<"\n\nPlease enter a decision: ";
    cin>>intDecision;
    switch (intDecision) {
        case 1: addNewCustomer(); break;
        case 2: addNewVideo(); break;
        case 3: checkOutVideo(); break;
        case 4: checkInVideo(); break;
        case 5: viewAllVideos(); break;        
        case 0: cout<<"\n\n";
                exit(0); break;       
        default: printf("\nDecision entered is invalid");
                returnToMainMenu(); 
                break;                
    }
    //returnToMainMenu();
}

void addNewVideo() { 
    system("CLS");
    cout<<"\nADDING A NEW VIDEO";
    AvailableVideo newVideo;
    newVideo.setDetails();
    cout<<"\nNew video successfully added";
    returnToMainMenu();
}

void addNewCustomer() {
    system("CLS");
    cout<<"\nADDING A NEW CUSTOMER";
    Customer newCustomer;
    newCustomer.setDetails();
    cout<<"\nNew customer successfully added";
    returnToMainMenu();
}

void viewAllVideos() { 
    AvailableVideo*  avblVideo = AvailableVideo::first;
    system("CLS");
    cout<<"\nVIEWING ALL VIDEOS";
    while (avblVideo != NULL) { 
       avblVideo->displayDetails();        
       avblVideo = avblVideo->next;
    }
    returnToMainMenu();
}

void checkOutVideo() {
    AvailableVideo* avblVideo = AvailableVideo::first;
    Customer*  customer = Customer::first;
    system("CLS");
    cout<<"\nCHECK-OUT A VIDEO"
            <<"\n\nEnter the title of the video to check-out: ";
    cin.ignore();
    getline(cin, title);
    while (avblVideo != NULL) {
        if (avblVideo->getVideoTitle() == title) {
           avblVideo->displayDetails();
           break; //assuming that title is a primary key and no 2 videos have same title, thus once video is found, break from loop
        }
        avblVideo = avblVideo->next;
    }
    if (avblVideo == NULL) 
        cout<<"\nVideo with the title '" <<title <<"' is not found.";
    else {
        cout<<"\nEnter 'Y' to check-out the video. Enter any other key to cancel check-out: ";
        cin>>charDecision;
        if (charDecision == 'Y' || charDecision == 'y') {
            if (avblVideo->getCopiesInStock() == 0) {
                cout<<"\nNo more copies in stock for the selected video. Check-Out unsuccessful.";
                //break;
                returnToMainMenu();
            }
            else {                
                cout<<"\nEnter customer's name: ";
                cin.ignore();
                getline(cin, name);
                //verify if customer is registered in the Customer File                
                while (customer != NULL) { 
                    if (customer->getCustName() == name) {
                        customer->displayDetails(); 
                        break; 
                    }
                    customer = customer->next;
                }
                if (customer == NULL) {
                    cout<<"\nCustomer '" <<name <<"' is not found in Customer File."
                            <<"\nTo check-out the video, please register customer from the Main Menu.";
                }
                else {
                    cout<<"\nEnter 'Y' to confirm check-out of the video to the customer above."
                            <<"\nEnter any other key to cancel check-out: ";
                    cin>>charDecision;
                    if (charDecision == 'Y' || charDecision == 'y') {
                        if (customer->checkAvblRentSlot() != 0) {
                            avblVideo->checkOutVideo();
                            customer->rentVideo(title);
                            RentedVideo newRentedVideo(avblVideo->getVideoTitle(), avblVideo->getMovieStar1(), avblVideo->getMovieStar2(), avblVideo->getMovieDirector(), avblVideo->getMovieProducer(), avblVideo->getMovieProductionCo(), customer->getCustName());
                            cout<<"\nVideo successfully checked-out.";
                        }
                        else
                            cout<<"\nCustomer have reached the maximum of 3 rented videos. Check-Out unsuccessful.";                        
                    }
                }
            }
        }
    }
    returnToMainMenu();
}

void checkInVideo() { 
    RentedVideo* checkedOutVideo = RentedVideo::first;    
    AvailableVideo* avblVideo = AvailableVideo::first;
    Customer* customer = Customer::first;
    
    system("CLS");
    cout<<"\nCHECK-IN A VIDEO"
            <<"\n\nEnter the title of the video to check-in: ";
    cin.ignore();
    getline(cin, title);
    while (checkedOutVideo != NULL) { 
        if (checkedOutVideo->getVideoTitle() == title) {
            checkedOutVideo->displayDetails();
            break; 
        }           
        checkedOutVideo = checkedOutVideo->next;
    }
    if (checkedOutVideo == NULL) 
        cout<<"\nVideo with the title '" <<title <<"' is not found in the Rented Videos List. Check-In unsuccessful.";
    else {
        cout<<"\nEnter 'Y' to confirm video check-in. Enter any other key to cancel check-in: ";
        cin>>charDecision;
        if (charDecision == 'Y' || charDecision == 'y') {
            while (avblVideo != NULL) { 
                if (avblVideo->getVideoTitle() == title) {
                   avblVideo->checkInVideo();                   
                   break; 
                }
                avblVideo = avblVideo->next;
            }
            if (avblVideo == NULL) {
                cout<<"\nVideo with the title '" <<title <<"' is not found in the Available Videos List."
                        <<"\nTo check-in the video, create new video record from the Main Menu."; 
            }
            
            //remove video from customer's rented video slot
            while (customer != NULL) { 
                if (customer->getCustName() == checkedOutVideo->getCustRentedTo()) {
                    customer->returnVideo(title);
                    
                    name = customer->getCustName(); 
                    break;
                }
                customer = customer->next;
            }
            if (customer == NULL) {
                cout<<"\nCustomer '" <<name <<"' is not found in Customer File."
                        <<"\nCheck-in will continue but re-register customer to enable future transactions.";                
            }            
            //remove video from Rented Videos List
            checkedOutVideo->removeRentedVideo(checkedOutVideo);
            cout<<"\nVideo successfully checked-in."<<endl; 

        }        
    }    
    returnToMainMenu();
}

void returnToMainMenu() {
    cout<<"\n\nEnter any key to return to Main Menu: ";
    cin>>charDecision;    
    mainMenu();
}

You must take care to use operator 'delete' only on objects that have been allocated using operator 'new', without any exceptions.

If you look into your code, you will see that this is NOT happening.
In the checkOutVideo() function you have; RentedVideo newRentedVideo(avblVideo->getVideoTitle() ...); i.e. the newRentedVideo object has not been allocated using new, instead it lives on stack only, and in the RentedVideo() constructor you assign the class' static pointer to this stack object. And later on, via checkInVideo() you eventually use the delete operator on that address, that is where the damage (silently in this case) occurs.

So you need to check all the code that it behaves well with regard to new/delete.

Maybe your allocation and deallocation of Customer and AvailableVideo classes is OK, I'm assuming here that you allocate those objects using new, since you load the data from file.

err...Sorry I don't quite understand your explanation. This is because my lecturer have not teach anything about the 'new' keyword yet. He has barely touched the 'classes and objects' topic too. I wrote all the codes with the help of online tutorials and some basic C and Java knowledge. I'm glad tho, that the problem has been identified.

So to solve the problem, I just have to make sure that I use the 'new' keyword everytime I declare an object? So that when I use the 'delete' keyword, there will be no problem? If you don't mind, can you provide a sample code?

Thanks for your help.

You can create objects on the stack : which exist for the lifetime of the function only, or on the heap which exist until you delete them. If you are assigning a pointer which outlives the function then you need to create the object on the heap; and you do this using the new pointer.

Problem solved!!! Adding the 'new' keyword everytime I declare an object in the main did it. I guess I shouldn't have programmed without fully understanding the concept first. But in my defense, the assignment is due in less than a month, even though the lecturer have not lectured on most of the topics. Thanks once again for all your help!

*happilly marks thread as SOLVED*

This question has already been answered. Start a new discussion instead.