Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Below is my code here i am trying to subtract itemnum in struct node with user input value (x1) ....but it is not working properly .



C++
struct node                                                   // main structure
{
        int itemcode;
        int itemnum;                             //itemnum
        char name[30];
        struct node *next;
}
struct node *create,*end,*start,*temp,*prev,*temp1,*start1,*end1;
int x,y,x1,num,total;


void orderitem( int x,int x1)         // I am passing item code and itemnum 
{


    if(isempty())                        // function to check head pointer is !null
    {

        printf("Empty, can't order now");
    }
    else
    {

        temp=start;
        while(temp->next!=NULL && temp->itemcode!= x)
        {
            prev=temp;
            temp = temp-> next;
        }

        if(temp->next==NULL&&temp->itemcode!=x)  // searching with x i.e item code

        {
            printf("Element %d is not present in the list\n",x);
            return;
        }

        else if(start->itemcode==x)              //if item code is at first 
        {
            start1->itemnum=x1;

            start->itemnum-=start1->itemnum;

            if(start->itemnum<=0)         //deletes start if itemnumber less than 0
            {         
                start=start->next;
            }
        }
        else if(end->itemcode==x)              //checks the item code with end  pos
        {                                             

            end1->itemnum=x1;
            end->itemnum-=end1->itemnum;
        }
        if(end->itemnum<=0)                       //deletes end 
        {   end=prev;
            end->next=NULL;

        }


        else
        {
                temp1->itemnum=x1;                 //storing user's item number in  
                                                      temp1

                temp->itemnum-=temp1->itemnum;       

                if(temp->itemnum<=0)
                    prev->next=temp->next;

           
        }

    }
}


What I have tried:

I have tried structure variable to hold the user input and minus it with item number in struct ....but it didn't work......I am new to data structures,....the code may not be that good....but it would be hugely appreciated if someone can tell me the procedure to minus the item number with user input value. thanks
Posted
Updated 14-Nov-16 10:56am
v6

Do yourself a favour and indent that code properly: it's pretty much unreadable as it is, and indenting makes it a lot easier to see what is going on. With the code jumping about left and right like a spider on illegal narcotics it's not even easy to find where the "orderitem" function starts and ends, much less what it does in the meantime! :)

We can't run your code under the same conditions you do - we have no idea what data you input or what exactly you expect to happen when you do. So it's going to be up to you to get started on "what is going on here?".
Fortunately, you almost certainly have an excellent tool to help you do this: it's called a Debugger (though we don't know which system you are using so we can;'t give you precise instructions on how to use it - but Google can, I'm sure).

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Member 12719168 15-Nov-16 20:20pm    
thanks man I learnt debugging, found it was a segmentation fault......I solved the problem successfully .....I wonder why the teachers never mentioned about debugging during class time ......https://github.com/Kishy-nivas/Linked-list-scenarios-/blob/master/SUPER%20MARKET%20scenario%20in%20c%20using%20linked%20list - my complete project.
OriginalGriff 16-Nov-16 4:21am    
Well done!
It's fun, the debugger, isn't it?
The only way to find what is wrong in your code is to use the debugger to see what it is really doing.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 12719168 15-Nov-16 20:22pm    
thanks man I learnt debugging, found it was a segmentation fault......I solved the problem successfully .....I wonder why the teachers never mentioned about debugging during class time,now I wonder how can someone answer these kind of questions ......https://github.com/Kishy-nivas/Linked-list-scenarios-/blob/master/SUPER%20MARKET%20scenario%20in%20c%20using%20linked%20list - my complete project.
Patrice T 15-Nov-16 21:08pm    
Nice to hear that you solved the problem thanks to the debugger.
Patrice T 15-Nov-16 21:08pm    
Nice to hear that you solved the problem thanks to the debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900