I have been working on this for two weeks now and it has been progressing slowly. I am currently working on getting the the remove function of my program to 1. first check to see if there is a product 2. list the products 3. allow the user to select the product. 4. alow the user to delete a product. I think what I basically need to do is in Case2: create my list and check for my product there. Then I need to do a call to Remove function, which I will basically copy the add function with the exception of size -1 instead of size +1... at least those are my current thoughts. what would be the best way , must use the dynamic memory array and I am sort of stumped as to what to do next. Ideas to get me going with the remove again?
/************************************************************************
Name: Project 1
Purpose: Practice use of pointers in a actual program and
evaluation of the current skill set.
Description: dynamic array
************************************************************************/
#include <iostream>
#include <string>
using namespace std;
/***************************
Prototypes
***************************/
void pause ();
int menu ();
string *addProduct (string *sptr, string newProduct, int size);
string *removeProduct (string *dptr, int i, int size);
double *addPrice (double *sptr, double newPrice, int size);
double *removePrice (double *dptr, int i, int size);
/***************************************
This function is the point of entry for
the program.
input parameters: None
output Parameters: int - indication of the state of the program on close
****************************************/
int main()
{
int cont = 2;
string *products;
double *prices;
int size =0;
do
{// start do
int choice=menu ();
switch (choice)
{ // Open switch statement
case 1:
//Get the information from the user for the product
char s[30];
cout<<"\n\nEnter the product name: ";
cin.getline(s,30);
double price;
cout<<"\n\nEnter the product Price: ";
cin>>price;
cin.ignore(80,'\n');
products = addProduct(products,s, size);
prices = addPrice(prices,price, size);
size++;
for (int i=0; i<size; i++)
cout<<i<<": "<<*(products+i)<<" "<<*(prices+i)<<endl;
pause();
break;
case 2:
cout<<"\n\n Removed Called\n\n";
break;
case 3:
cout<<"\n\n Sort Called\n\n";
break;
case 4: cont=0;
break;
} // Close switch statement
}while (cont ==2);
return 0;
}// STOP MAIN
void pause ()
{
cout << "Press enter to continue " << endl;
cin.ignore (80, '\n');
}
int menu ()
{ // start int menu prototype
int c = -1;
while (c<1 || c>4)
{ // start menu
system ("cls");
cout << " 1. Add\n\n\n"
<< " 2. Omit\n\n\n"
<< " 3. Sort\n\n\n"
<< " 4. Exit\n\n\n"
<< " Please make a selection from the menu: \n\n";
cin >> c;
cin.ignore (80, '\n');
}//end of while loop
return c;
}// close int menu prototype
string *addProduct (string *sptr, string newProduct, int size)
{
//Create a new memory location and store the memory location in the pointer.
string *ptr = new string[size+1];
//Move the products to the new memory location
for (int i=0; i<size; i++)
*(ptr+i)=*(sptr+i);
//add the new product to the new memory location
*(ptr+size)=newProduct;
//if the size is not 0 then delete the old memory location
if (size>0)
delete sptr;
//Return the new memory location pointer
return ptr;
}//stop void add prototype
string *removeProduct (string *dptr, int i, int size)
{
cout << " Remove ";
pause();
}//stop void omit prototype
double *addPrice (double *dptr, double newPrice, int size)
{
//Create a new memory location and store the memory location in the pointer.
double *ptr = new double[size+1];
//Move the prices to the new memory location
for (int i=0; i<size; i++)
*(ptr+i)=*(dptr+i);
//add the new price to the new memory location
*(ptr+size)=newPrice;
//if the size is not 0 then delete the old memory location
if (size>0)
delete dptr;
//Return the new memory location pointer
return ptr;
}
double *removePrice (double *dptr, int i, int size)
{}