Thread: Program compiles but doesn't run ~ attached debugging screen

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    17

    Program compiles but doesn't run ~ attached debugging screen

    Sorry for reposting my program because I couldn't modify my last post

    My program compiles but doesn't run,

    It would be great if you could help

    Here is the screen shot of debugging

    Program compiles but doesn't run ~ attached debugging screen-untitled-jpg

    //DynBag.cpp : Implementation File


    #include <stdexcept>
    #include <iostream>
    #include "DynBag.h"

    main :


    Code:
    int main ()
    {
        Bag b(2), b1(3), b2(2), b3(4);
    
    
        b.insert("Lidiecita");
        b.insert("Brendita");
        b.insert("Francisquito");
        b.insert("Lidiecita");
        b1.insert("Mayi");
        b1.insert("Eimy");
    
    
        b += b;
        b.print(cout);
    
    
        b3 = b + b1;
        b3.print(cout);
    
    
        b2 = b1;
        b2.print(cout);
    
    
        Bag b4 = b3;
        cout << b4;
    
    
    }
    DynBag.h


    Code:
    #ifndef DYNBAG_H
    #define DYNBAG_H
    
    
    #include <string>
    
    
    
    
    using namespace std;
    
    
    typedef string ItemType;
    
    
    class Bag
    {
    
    
    public:
    
    
        Bag (int InitialCapacity);
    
    
        Bag(const Bag& other);
    
    
        ~Bag();
    
    
        void insert(const ItemType& entry);
    
    
        void erase_one(const ItemType& target) throw (domain_error);
    
    
        int erase(const ItemType& target);
    
    
    
    
        Bag& operator=(const Bag& other);
    
    
        Bag& operator+=(const Bag& addend);
    
    
        void reserve(int new_capacity);
    
    
        int size() const;
    
    
        int count_duplicates (const ItemType& target) const;
    
    
        void print (ostream& out) const;
    
    
    private:
        ItemType *contents;
        int NumItems;
        int capacity;
    };
    
    
    Bag operator+ (const Bag& b1, const Bag& b2);
    
    
    ostream& operator<<(ostream&, const Bag& b1);
    
    
    
    
    #endif
    DynBag.cpp

    Code:
    #include <stdexcept>
    #include <iostream>
    #include "DynBag.h"
    
    
    
    
    Bag::Bag(int InitialCapacity) : NumItems(0)
    {
        capacity = InitialCapacity;
        contents = new ItemType[capacity];
    }
    
    
    Bag::Bag(const Bag& other) : NumItems(other.NumItems)
    {
        capacity = other.capacity;
        contents = new ItemType[capacity];
        for (int i=0; i < NumItems; ++i)
            contents[i] = other.contents[i];
    }
    
    
    Bag::~Bag() {}
    
    
    int Bag::size () const
    {
        return NumItems;
    }
    
    
    int Bag::count_duplicates (const ItemType& target) const
    {
        int answer = 0;
    
    
        for (int i = 0; i < NumItems; ++i)
            if (target == contents[i])
                ++answer;
        return answer;
    }
    
    
    void Bag::insert(const ItemType & entry)
    {
        if (NumItems >= capacity)
            reserve(capacity*2);
    
    
        contents[NumItems] = entry;
        ++NumItems;
    }
    
    
    void Bag::erase_one(const ItemType & target) throw(domain_error)
    {
        int i, j;
    
    
        for (i = 0; i < NumItems; ++i)
            if (target == contents[i]) break;
    
    
        if (i == NumItems) throw domain_error("Target Not Found");
    
    
        if (i < NumItems-1)
            for (j = i; j < NumItems-1; ++j)
                contents[j] = contents[j+1];
    
    
        --NumItems;
    
    
    }
    
    
    int Bag::erase(const ItemType & target)
    {
        int count = count_duplicates(target);
        int removed = 0;
    
    
        for ( ; removed < count; ++removed)
            erase_one(target);
    
    
        return(removed);
    
    
    }
    
    
    Bag& Bag::operator+=(const Bag& addend)
    {
    
    
        int i;
    
    
        for(i=0; i<addend.size(); i++)
        {
            insert(addend.contents[i]);
        }
    
    
        return *this;
    }
    
    
    Bag& Bag::operator =(const Bag& other)
    {
        if (&other == this) return *this;
    
    
        delete[] contents;
        NumItems = other.NumItems;
        capacity = other.capacity;
        contents = new ItemType[capacity];
        for (int i=0; i < NumItems; ++i)
            contents[i] = other.contents[i];
    
    
        return *this;
    }
    
    
    void Bag::reserve (int new_capacity)
    {
        if (new_capacity == capacity) return;
    
    
        if (new_capacity < NumItems) new_capacity = NumItems;
    
    
        ItemType *new_contents = new ItemType[new_capacity];
    
    
        for (int i=0; i < capacity; ++i)
            new_contents[i] = contents[i];
    
    
        delete[] contents;
        contents = new_contents;
        capacity = new_capacity;
    }
    
    
    void Bag::print(ostream& out) const
    {
        for (int i = 0; i < NumItems; i++)
            out << contents[i] <<"\t";
    
    
        out <<endl;
    
    
    }
    
    
    Bag operator+(const Bag &b1, const Bag &b2)
    {
        Bag b3(b1.size()+b2.size());
        b3 += b1;
        b3 += b2;
        return b3;
    
    
    }
    
    
    ostream& operator<<(ostream& o, const Bag& b1)
    {
    
    
        o << "[b1 contents here]";
        return o;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You need to check your operator+=, it never seems to return.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    it's likely not affecting the operation of your program, but it's not a good idea to have using namespace directives in the global namespace of header files.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>typedef string ItemType;
    Should be inside Bag since it's an implementation detail.

    I also would suggest you use std::vector instead of new and delete. Your class is leaking memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiles okay, but doesn't run.
    By karlawarla in forum C++ Programming
    Replies: 20
    Last Post: 11-23-2006, 04:03 PM
  2. Replies: 4
    Last Post: 04-26-2004, 06:31 PM
  3. Code compiles but doesn't run
    By dbyte in forum C Programming
    Replies: 5
    Last Post: 07-22-2003, 08:25 PM
  4. this code compiles, but doesn't work how it should
    By Leeman_s in forum C++ Programming
    Replies: 10
    Last Post: 09-10-2002, 05:31 PM