Hi. I am kind of having trouble with some functions. Basically, I know how to write out functions using void and no return, but for this problem I am working on, I have to use int myfunction(something&) instead of void myfunction(). Can anyone please lead me in the right direction as to what I should be doing?? I have my code for my function below.
I want it such that, its int add_coupon(coupon&) instead of void add_coupon()
here is my code so far:

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.get();
	cout << "Enter YYYY-> ";
	cin >> yyyy;
}

 /*Function for creating my linked list. This creates the list in sorted order
   alphabetically based on the name of the coupon's store.
 */

void 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;
    }
}

Recommended Answers

All 6 Replies

Dude, you can use int in a way that 0 is returned if the function is carried out successfully, and 1 if it isn't, like this:

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.get();
	cout << "Enter YYYY-> ";
	cin >> yyyy;
}

 /*Function for creating my linked list. This creates the list in sorted order
   alphabetically based on the name of the coupon's store.
 */

int coupons::add_coupon() //changed to int!
{

    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;//This indicates success!!
}
//So to test whether the node was successfully created or not..
//When u want to call the function, call it this way..
coupons x;
int ret=x.add_coupons();
if(x!=0){
cout<<"The coupon was not added to the list!!"<<endl;
}
else{
cout<<"Coupon successfully added!!"<<endl;
}

Hope this helped!!

Thank You!!!! Pretty simple after all.

hi i am using classes. so this function is called using a class object. my question is, i am getting this error
no match for 'operator!=' in 'x != 0'
what does that mean?

no match for 'operator!=' in 'x != 0'
what does that mean?

Exactly what is says. It doesn't know how to compare your class 'x' to 0. I think you meant: ret != 0;

If you set it up like tkud recommended (his second approach), it looks like you might have wanted to compare if(ret !=0) , right?

However, a (somewhat) brief explanation of what the compiler was trying to tell you:
Your class must overload the != if you are going to use it to compare your objects directly like you would an int or bool. For example if you had a person class with age and you wanted to say two people objects were equal based on their age, you would overload == (and != as well just as the not of ==).
So saying person1 == person2 would be the same as saying person1.age == person2.age, you can compare the objects directly.
Look up operator overloading there's tons of other examples out there.

EDIT: LOL niek_e beat me to it

Ok I understand more. Thank You

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.