Thread: The compiler stopped working

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    The compiler stopped working

    Hello guys. Do any of you know why my compiler stopped working when user try to input "Enter your choice based on the number" for the 2nd time. The 1st time it worked correctly. I got the correct output but for the 2nd time the compiler stopped. Is there anything wrong with my coding? By the way, I'm using Code Blocks as my compiler. Thanks in advance.

    The output I must get:

    Would you like to order a pizza [y/n]:
    y
    [1] - BBQ Chicken
    [2] - Island Delight
    [3] - Hawaiian Delight
    Enter your choice based on the number:
    2
    You've selected Island Delight
    Please enter size [R / L]:
    L
    Please enter quantity:
    2
    Thank you for ordering
    ----Your pizza details-----
    Pizza : Island Delight
    Quantity : 2
    Price perpizza : RM 33.30
    Total payment : RM 66.60
    Would you like to order another pizza [y/n]:
    y
    [1] - BBQ Chicken
    [2] - Island Delight
    [3] - Hawaiian Delight
    Enter your choice based on the number:
    1
    You've selected BBQ Chicken
    Please enter size [R / L]:
    r
    Please enter quantity:
    2
    Thank you for ordering
    ----Your pizza details-----
    Pizza : BBQ Chicken
    Quantity : 2
    Price perpizza : RM 25.00
    Total payment : RM 50.00
    Would you like to order another pizza [y/n]:
    n
    Press any key to continue

    Code:
    
    
    Code:
    #include <iostream>
    using namespace std;
    
    
    class Pizza
    {
    private:
        string name;
        char size;
        int quantity;
        float price, totalprice;
    
    
    public:
        int choice;
    
    
        void menu()
        {
            cout << "\n[1] - BBQ Chicken" << endl;
            cout << "[2] - Island Delight" << endl;
            cout << "[3] - Hawaiian Delight" << endl;
        }
    
    
        void setSelection()
        {
            cout << "\nEnter your choice based on the number: " << endl;
            cin >> choice;
    
    
            if (choice == 1)
            {
                name = "BBQ Chicken";
            }
    
    
            else if (choice == 2)
            {
                name = "Island Delight";
            }
    
    
            else
            {
                name = "Hawaiian Delight";
            }
    
    
        }
    
    
        void setSizeQuantity()
        {
            cout << "\nYou've selected " << name << endl;
            cout << "\nPlease enter size [R/L]" << endl;
            cin >> size;
            cout << "\nPlease enter quantity" << endl;
            cin >> quantity;
    
    
            if (choice == 1)
            {
                if (size == 'R')
                {
                    price = 25.00;
                    totalprice = quantity * price;
                }
    
    
                else
                {
                    price = 35.30;
                    totalprice = quantity * price;
                }
            }
    
    
            if (choice == 2)
            {
                if (size == 'R')
                {
                    price = 22.00;
                    totalprice = quantity * price;
                }
    
    
                else
                {
                    price = 33.30;
                    totalprice = quantity * price;
                }
            }
    
    
            if (choice == 3)
            {
                if (size == 'R')
                {
                    price = 24.50;
                    totalprice = quantity * price;
                }
    
    
                else
                {
                    price = 36.60;
                    totalprice = quantity * price;
                }
            }
        }
    
    
        void display()
        {
            cout << "\nThank You for ordering" << endl;
            cout << "\n----Your pizza details----" << endl;
            cout << "\nPizza : " << name << endl;
            cout << "\nQuantity : " << quantity << endl;
            cout << "\nPrice perpizza : RM " << price << endl;
            cout << "\nTotal Payment : RM " << totalprice << endl;
        }
    };
    
    
    int main()
    {
        char user;
    
    
        Pizza *PizzaHut = new Pizza();
    
    
        cout << "\nWould you like to order a pizza [y/n]: " << endl;
        cin >> user;
    
    
        while (user == 'y')
        {
                if (user == 'y')
                {
                   PizzaHut -> menu();
                   PizzaHut -> setSelection();
                   PizzaHut -> setSizeQuantity();
                   PizzaHut -> display();
    
    
                   delete PizzaHut;
                }
    
    
                 cout << "\nWould you like to order a pizza [y/n]: " << endl;
                 cin >> user;
        }
        return 0;
    }




    Last edited by akif13; 08-01-2013 at 05:17 PM.

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    It's not the compiler that has stopped working, it is your program. The compiler interprets your code and generates the corresponding instructions that allow your computer to execute the code. When the compilation has finished, the compiler is done and is no longer involved.

    Something went terribly wrong when you pasted in your program, and it is completely unreadable. See if you can fix it, then it'll be much easier to offer some help.
    If the program fails sporadically and in a random fashion, it usually means that you have undefined behaviour or an overflow somewhere in your program. Bugs such as these might go unnoticed if the stars align, and then suddenly crash your program on the subsequent execution.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    10
    If I remove line 148, I will get the correct output for the 2nd time and so on. But the question asked me to delete object inside line 140. What should I do?

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by akif13 View Post
    If I remove line 148, I will get the correct output for the 2nd time and so on. But the question asked me to delete object inside line 140. What should I do?
    You should only delete an object after you're done with it. You're deleting Pizzahut within a loop, what happens is that during the first iteration the object is present and everything works, then you delete it and start the next iteration, wherein you're trying to access an object that has been deleted. Move the delete out of the loop, and it should work. That being said there is no reason to allocate the Pizza object on the heap in this program.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you delete an object and later expect to use that object (in later iterations of that loop, in your case), you need to recreate that object (or a similar object). It won't magically recreate itself, and attempting to use an object after destroying it would explain your problem.

    As Neo1 said, the problem is not with the compiler. It is with your program. The compiler simply does what it is told to do by the source code you give it. If the source code is faulty, you are responsible, not the compiler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O stopped working.
    By Andersonsacanno in forum C Programming
    Replies: 10
    Last Post: 11-14-2012, 07:45 AM
  2. ****.exe stopped working (Second project i have)
    By asediugeneral in forum C Programming
    Replies: 3
    Last Post: 09-03-2012, 01:25 PM
  3. ****.exe has stopped working
    By kawaikx15 in forum C Programming
    Replies: 10
    Last Post: 11-19-2011, 07:38 AM
  4. .exe has stopped working
    By bluesky16 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2011, 12:58 PM
  5. vshost.exe has stopped working
    By Marty21 in forum C# Programming
    Replies: 1
    Last Post: 06-08-2009, 10:41 AM