Hello All,

Back again...Working on a program which requires that the user should be able to interact with the program.

The objective of the program is this:

A class called unique-bag which will store integers using dynamic array.
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.

I sort of have an idea what needs to be done but not sure how to implement it. My program should display my menu sfter it executes any part of my code. For example - if the user selects 'I' to insert, once that task is completed, it should display the menu again to insert,print or delete.

My code is below, hope someone can point me in the right direction as usual. Also, based on the initial requirement of this program - i would appreciate any correction or criticism on the structure, composition or design of my code.

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

using namespace std;

void displaySelection();
void get_num(uniquebag& num);
void check_num(uniquebag& num);


//main program
int main()
{
	uniquebag number;


		
	displaySelection();
    get_num(number);
	check_num(number);

	cout << "Thank-You for using this program" << endl;
	
	system("PAUSE");
    return 0;

}
void displaySelection()
{
		int user_value;	
	char choice;

		cout << "Please select what operation you would like to perform" << endl;

		cout << fixed <<showpoint <<setprecision(2);
		cout <<"\n	        Selection\n";
		cout << "------------------------------\n";
		cout << "I)Please select (I) to insert\n";
		cout << "D)Please select (D) to delete\n";
		cout << "P)Please select (P) to print\n\n";
		cout << "Enter your choice: ";
		cin >> choice;

		while ( choice != 'i' && choice != 'I' &&
				choice != 'd' && choice != 'D' &&
				choice != 'p' && choice != 'P')
		{
			cout << "Invalid entry" << endl;
			cout << "Enter I or D or P: " << endl;
			cin >> choice;
		}


			switch (choice)
			{
			
			case 'i':
			case 'I': cout << "Enter how many number to insert" << endl;
					  cin >> user_value;
					  //cout << "This item has been inserted in the bag \n";
			     	  
				break;
			
			case 'd':
			case 'D': cout << "This item has been deleted \n";
			     	  
				break;

			case 'p':
			case 'P': cout << "These are the items in the bag \n";
			     	 
				break;
		}
}
void check_num(uniquebag& num)
{
	int user_input;

	cout << "Type the numbers again. Press enter after each number:" << endl;
	
	while (num.size() > 0)
	{
	   cin >> user_input;
		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;
		
		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.DEFAULT_CAPACITY)
			num.insert(user_input);
		else
			cout << "I have run out of room and can't add that number." << endl;
			cin >> user_input;
		
	}
}

//uniquebag.h

#include<cstdlib>
#include<cassert>


using namespace std;

class uniquebag
{
public:
 
   typedef int value_type;									
   typedef std::size_t size_type;							
   static const size_type DEFAULT_CAPACITY = 10;
   uniquebag(size_type intial_capacity = DEFAULT_CAPACITY);
   ~uniquebag();
   void reserve(size_type new_capacity);
   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 operator +=(const uniquebag& addend);
   void operator = (const uniquebag& source);
   size_type count(const value_type& target) const;	
   void print_bag(); 
   void displaySelection();
   
 private:
   value_type *data;
   size_type used;
   size_type capacity;
   
};

//implementation file
#include<iostream>
#include<cstdlib>
#include<cassert>
#include<algorithm>
#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 () < DEFAULT_CAPACITY);
    bool found = false;
    for (size_type i=0; i < used; i++)
    {
        if( data[i] == entry)
        {
            found = true;
			cout << "item already in the bag: " << endl;
            break;
        }
    }
    if( found == false)
    {
	    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;
}

uniquebag::uniquebag(size_type initial_capacity)
{
	data = new value_type[initial_capacity];
	capacity = initial_capacity;
	used = 0;
}

uniquebag::~uniquebag()
{
	delete [] data;
}

/*void uniquebag::reserve(size_type new_capacity)
{
	value_type *larger_array;
	if (new_capacity == capacity)
		return;

	if (new_capacity < used)
		new_capacity = used;

	larger_array = new value_type[new_capacity];
	copy(data, data + used, larger_array);
	delete [ ] data;
	data = larger_array;
	capacity = new_capacity;

}*/

As far as the menu, it might be easier to put the switch statement in main. You could have another method that displays the options and gets/validates the menu choice and returns it back to main. If you add another menu option, such as Q for quit, then you could use a while loop to loop around the switch statement. If you have the method that returns the valid menu choice, then your switch statement could use 3 cases instead of 6, since it would always be a valid choice.

why not use a vector? it has functions for every objective so you don't have to remake everything

when storing the vector you can probably do something like this:

for(size_type i=0; i < myvector.size(); i++) {
   if(insertednumber == myvector[i])
      break;

   if(i == myvector.size()-1)
      myvector.push_back(insertednumber)
}

as for your current code's structure, i think that your functions should do exactly as you label them, instead of handling so much input inside of the actual functions (perhaps in main())

This article has been dead for over six months. Start a new discussion instead.