Hello all,

I am working on a couple of programs and having similar problems with both, not sure what am doing wrong and would appreciate help as usual.

The first program should have a class which can store up to 10integers. Only one copy of any integer(no duplicate value. Program should be able to insert, erase,check and display integers.

My code is below, though program runs successfully - my output is wrong.
1.I can store duplicate numbers.
2. When it displays the number i check, i get multiples of the last number on the list.

Thank-you all for your help with this.

Regards

//This program is a class called unique-bag which will store up to 10 integers. 
//This class stores only one copy of any given integer (no duplicate value).
//This program is able to insert integers, erase integers, check integer, and display integers.

//header file

//#include<cstdlib>
//#include<cassert>
//uniquebag

class uniquebag
{
public:
   uniquebag ()
    {
		used = 0;
    }; // default constructor
   typedef int value_type;
   typedef std::size_t size_type;
   static const size_type CAPACITY = 10;
   size_type size () const { return used;}
   void insert(const value_type& entry);
   size_type erase(const value_type& target);
   bool erase_one(const value_type& target);
   void print_bag();
 private:
   value_type data[CAPACITY];
   size_type used;
   
}; // end class uniquebag

#include<iostream>
#include<cstdlib>
#include<cassert>
#include"uniquebag.h"
using namespace std;

void uniquebag::print_bag(){
	for (int i=0;i<=used;i++)
		cout << data[i] << ", ";
	cout << endl;
}
void uniquebag::insert(const value_type& entry)
{
	assert(size () < CAPACITY);
	data[used] = entry;
	++used;
}

uniquebag::size_type uniquebag::erase(const value_type& target)
{
	size_type index = 0;
	size_type many_removed = 0;

	while(index < used)
	{
		if (data[index] == target)	{
			--used;
			data[index] = data[used];
			++many_removed;
		}
		else
			++index;
	}
	return many_removed;
}

bool uniquebag::erase_one(const value_type& target)
{
	size_type index;
	index = 0;
	while ((index < used) && (data[index] != target))
		++index;
	if (index == used)
		return false;
	--used;
	data[index] = data[used];
	return true;
}

//main Program

#include<iostream>
#include<cstdlib>
#include<cassert>
#include"uniquebag.h"

using namespace std;
void get_num(uniquebag& num);
void check_num(uniquebag& num);

int main()
{
	uniquebag number;
	
    get_num(number);
	check_num(number);
	
	cout <<"thank-you for using this program" << endl;
	system("PAUSE");
    return 0;

}

void check_num(uniquebag& num)
{
	int user_input;
	cout << "Type number to remove from bag or negative number to exit:" << endl;
	cin >> user_input;
	while ( user_input > 0)
	{
	   
		if(num.erase_one(user_input))
			cout << "Yes, I've found that number and removed it." << endl;
		else
			cout << "No, that number does not occur!" << endl;
		cin >> user_input;
		num.print_bag();
	}
}

void get_num(uniquebag& num)
{
	int user_input;

	cout << "Type the number to store in the bag." << endl;
	cout << "Type a negative number when you are done:" << endl;
	cin >> user_input;
	while (user_input >= 0)
	{
		if(num.size() < num.CAPACITY)
			num.insert(user_input);
		else
			cout << "I have run out of room and can't add that number." << endl;
	    cin >> user_input;
		
	}
}

Here is one way to prevent duplicates

BTW: assert() does nothing when the program is compiled for release mode instead of debug mode.

void uniquebag::insert(const value_type& entry)
{
	assert(size () < CAPACITY);
    // check if the number already exists in the array
    bool found = false;
    for (size_type i=0; i < used; i++)
    {
        if( data[i] == entry)
        {
            found = true;
            break;
        }
    }
    if( found == false)
    {
	    data[used] = entry;
	    ++used;
    }
}

Thanks for the response Ancient Dragon - will work on my program now and post my new code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.