0

Hi. I have a working program that lets you put info about a coupon. and then later on you can search for coupons, display them or remove them from the linked list.
My problem is that, we are not supposed to perform input operations from our classes. But the way I have written my code, its the only way I can do it. So I need help in being able to input data from the main program and pass it to my class members. I have attached an example of this below:

This is my class:

void node::add_coupon()        
{
	char temp[201];
	char temp2[201];
	cout << "Adding a new coupon....."<<endl;
	cout << "Enter the name of the store coupon: ";
	cin.get(temp,200,'\n');
	cin.ignore();
    name = new char[strlen(temp)+1];
	strcpy(name,temp);
	cout << "Enter description: ";
    cin.get(temp2,200,'\n');
	cin.ignore();
    category = new char[strlen(temp2)+1];
	strcpy(category,temp2);
	cout << "Enter the coupon discount amount: $";
	cin>> discount;
	cin.ignore();
	cout << "Enter the spend amount before coupon discount can be applied: $";
	cin>> spend;
	cin.ignore();
	cout << "Enter the coupon expiration date in MM/DD/YYYY format: ";
	cout << "Enter MM-> ";
	cin >> mm;
	cin.ignore();
	cout << "Enter DD-> ";
	cin >> dd;
	cin.ignore();
	cout << "Enter YYYY-> ";
	cin >> yyyy;
}
class coupons
{
      public:
            
            node *head; // head pointer
            coupons();           //constructor
            ~coupons();          //destructor
            node *current;		 // Used to move along the list
            int add_coupon (); //function for adding coupon..how can i use it as int add_coupon(coupon&);
            //int search(char store[], coupon matching[], int& num_found);//how can i use this instead of my current search function??
            int remove_coupon (char store[]);///HOW WOULD I USE THIS FUNCTION INSTEAD OF MY BOOL remove_coupon();  ??
            bool remove_coupon();
            int display_alerts();
            int display_all(); //function for displaying coupons
            int search_store_coupon(); //search function
            
      private:
       
            int num_found;

};

here's my function implementation:

int coupons::add_coupon() 
{

    current = head;
	
    node* temp = new node;
    temp->add_coupon();
    temp->next = NULL;
    
    if(!head)
    head = temp;
    
    else if(strcmp(head->name,temp->name)>0)
    {
         temp->next = head;
         head = temp;
    }
    else
    {
        node *current = head->next;
        node *prev = head;
        while(current !=NULL && strcmp(temp->name, current->name)>0)
        {
             prev = current;
             current = current->next;
        }
        
    temp->next = current;
    prev->next = temp;
    }
    return 0;
}

int coupons::search_store_coupon()  
{
    node *temp;
    char n[10];
    int same;
    cout<<"Enter the name of the coupon store: ";
    cin>>n;
    temp=head;
    if(head==NULL)
    {
        cout<<" Empty List"<<endl;
        return 1; //
    }
     else
     {
        while (temp != NULL)
        {
        same=strcmp(n,temp->name);
        if(same==0)
        {
        cout<<" Searching successful!"<<endl;
        cout<<"The item you searched for is in the list!!!"<<endl;
        cout<< left << setw(5) << "Name: " << temp->name << setw(5) << " Description: "<< setw(5) << temp->category << " Discount $" << temp->discount << setw(5) <<" Rule $" << temp->spend << setw(5) << " Expires " <<temp->mm << "/" << temp->dd << "/"<< temp->yyyy; //display format 
        break; 
        }
        temp=temp->next;
        }
        if (temp == NULL) //It means unsuccessful search after going through list
        { 
        cout<<"Search UNSUCCESSFUL!!"<<endl;
        cout<<"The coupon is not in the list. SORRY :("<<endl;
        return 1;
        }
     }
     return 0;
}

bool coupons::remove_coupon()
{ 
     node *current, *previous;
     current = head;
     char item[10];
     cout<<"Enter the name of the coupon store: ";
     cin>>item;
     if(head==NULL)
     {
        cout<<" No coupons to remove"<<endl;
        return false; //
     }
     if ( strcmp(current->name,item) == 0) //if the target is the first node
     {
        head = current->next;
        delete(current);
        cout <<"Deleted"<< endl;
     }
     else
     {
         while ( current && ( strcmp(current->name, item) != 0 ) )
         {
            previous = current;
            current = current->next;
         }
     previous->next = current->next;
     cout << "Deleted"<<endl;
     delete(current);
     }
     return true; 
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Agni
2

in case you are wondering, why you haven't got any useful replies, let me tell you. The question is vague, you have not provided any main function so we don't know which functions you want to call, how is your input stored and why you are not able to pass it to your class functions, you have started your post with a node::add_coupons function and provided several other function implementations which do not seem related to your question.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.