Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 107,398 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,179 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Logic Error

 
Reply to this topicStart new topic

Logic Error, there is something wrong with my link list

qaz1134
post 16 Jul, 2008 - 10:39 AM
Post #1


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 47


My Contributions


hi guys i need your help
my program run and compiles but the output is not correct and i really could not locate the error
the program should create a list and insert a node (which properly works) but while the user inputs a value to be it should add the value to the sum of NULL
for example:
the output goes like this
1.create list
2.display list
3.insert node
4 exit

1 // user input

Enter data for the new node:

1 // user input
(then return)

1.create list
2.display list
3.insert node
4 exit

2 // user input

1

1.create list
2.display list
3.insert node
4 exit

3 // user input after display

Enter data wich the insertion is to be done

1 // user input

Insert a data for the node

2 // user input

1.create list
2.display list
3.insert node
4 exit

2 // user input

1
2
This should have a sum of 3 which is not shown and i dont know why???

the code is here
i have an attachment file (Link.exe)
CODE

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node {
int data;
struct node *next;
};

void createlist(struct node **first,int n)
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
*first=p;
p->data=n;
p->next=NULL;
}

void display(struct node *first)
{
struct node *p=first;
if(p==NULL)
  {
   puts("Link list is Empty");
  }
else
  {
   while(p!=NULL)
    {
     printf("\n%d\t%d\t%d\n",p,p->data,p->next);
     p=p->next;
    }
  }
}

void insertnode(struct node **first, int n)
{
int d, sum;
sum=0;
struct node *q;
struct node *p=*first;
q=(struct node*)malloc(sizeof(struct node));
puts("\nEnter data for the inserted node");
scanf("%d",&d);
sum=n+sum;
printf("sum of the nodes is:%d",sum);
while(p!=NULL)
  {
   if(p->data==n)
    {
     q->data=d;
     q->next=p->next;
     p->next=q;
     return;
    }
   p=p->next;
  }
}

void main()
{
struct node *first=NULL;
int ch,n;
do
{
  puts("\nEnter choice\n1.create list\n2.display list");
  puts("3.INsert a node\n4.exit\n");
  scanf("%d",&ch);
  switch(ch)
   {
    case 1: if(first==NULL)
        {
         puts("Enter Data for the new node:");
         scanf("%d",&n);
         createlist(&first,n);
        }
    break;

    case 2: display(first);
    break;

    case 3: puts("\nEnter data of the node after wich the inserion is to be done:");
        scanf("%d",&n);
        printf("\n");
        insertnode(&first,n);
        printf("\n");
        break;
   }
}while(ch!=4);
}


please help me crying.gif
User is offlineProfile CardPM

Go to the top of the page


skaoth
post 16 Jul, 2008 - 11:46 AM
Post #2


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 319



Thanked 5 times

Dream Kudos: 100
My Contributions


My question to you is what are you trying to sum up? If it is the entire list then this is not being done. In your insertnode() function you ave a sum variable but you are only adding 2 values which are the initial value of sum itself (0 in your case) and the value of the passed in variable n.


If you are looking up to sum up the entire list don't do any of your summing code inside of insertnode() this is just confusing. Write a new function that loops through the entire list and add up the values.
User is offlineProfile CardPM

Go to the top of the page

kapax
post 16 Jul, 2008 - 12:43 PM
Post #3


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 33



Thanked 1 times
My Contributions


I don't understand what you mean, too. Your code works well, but I don't see where you are trying to sum up values (I haven't searched, honestly). You could make another function, which returns sum.
User is offlineProfile CardPM

Go to the top of the page

qaz1134
post 17 Jul, 2008 - 05:01 AM
Post #4


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 47


My Contributions


QUOTE(skaoth @ 16 Jul, 2008 - 11:46 AM) *

My question to you is what are you trying to sum up? If it is the entire list then this is not being done. In your insertnode() function you ave a sum variable but you are only adding 2 values which are the initial value of sum itself (0 in your case) and the value of the passed in variable n.


If you are looking up to sum up the entire list don't do any of your summing code inside of insertnode() this is just confusing. Write a new function that loops through the entire list and add up the values.





but what am i trying to do is that when a user inputs a value automatically is will sum up the added value to the sum of the previous node ....

and also those are the only avalable variable you could sum up
else
none...
User is offlineProfile CardPM

Go to the top of the page

My_code
post 17 Jul, 2008 - 07:07 AM
Post #5


New D.I.C Head

*
Joined: 10 Jul, 2008
Posts: 13

QUOTE(qaz1134 @ 16 Jul, 2008 - 10:39 AM) *

hi guys i need your help
my program run and compiles but the output is not correct and i really could not locate the error
the program should create a list and insert a node (which properly works) but while the user inputs a value to be it should add the value to the sum of NULL
for example:
the output goes like this
1.create list
2.display list
3.insert node
4 exit

1 // user input

Enter data for the new node:

1 // user input
(then return)

1.create list
2.display list
3.insert node
4 exit

2 // user input

1

1.create list
2.display list
3.insert node
4 exit

3 // user input after display

Enter data wich the insertion is to be done

1 // user input

Insert a data for the node

2 // user input

1.create list
2.display list
3.insert node
4 exit

2 // user input

1
2
This should have a sum of 3 which is not shown and i dont know why???

the code is here
i have an attachment file (Link.exe)
CODE

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node {
int data;
struct node *next;
};

void createlist(struct node **first,int n)
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
*first=p;
p->data=n;
p->next=NULL;
}

void display(struct node *first)
{
struct node *p=first;
if(p==NULL)
  {
   puts("Link list is Empty");
  }
else
  {
   while(p!=NULL)
    {
     printf("\n%d\t%d\t%d\n",p,p->data,p->next);
     p=p->next;
    }
  }
}

void insertnode(struct node **first, int n)
{
int d, sum;
sum=0;
struct node *q;
struct node *p=*first;
q=(struct node*)malloc(sizeof(struct node));
puts("\nEnter data for the inserted node");
scanf("%d",&d);
sum=n+sum;
printf("sum of the nodes is:%d",sum);
while(p!=NULL)
  {
   if(p->data==n)
    {
     q->data=d;
     q->next=p->next;
     p->next=q;
     return;
    }
   p=p->next;
  }
}

void main()
{
struct node *first=NULL;
int ch,n;
do
{
  puts("\nEnter choice\n1.create list\n2.display list");
  puts("3.INsert a node\n4.exit\n");
  scanf("%d",&ch);
  switch(ch)
   {
    case 1: if(first==NULL)
        {
         puts("Enter Data for the new node:");
         scanf("%d",&n);
         createlist(&first,n);
        }
    break;

    case 2: display(first);
    break;

    case 3: puts("\nEnter data of the node after wich the inserion is to be done:");
        scanf("%d",&n);
        printf("\n");
        insertnode(&first,n);
        printf("\n");
        break;
   }
}while(ch!=4);
}


please help me crying.gif





Each time when you call insertnode you are setting sum to zero.
Please check the logic once again.



User is offlineProfile CardPM

Go to the top of the page

qaz1134
post 20 Jul, 2008 - 05:17 AM
Post #6


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 47


My Contributions


QUOTE(My_code @ 17 Jul, 2008 - 07:07 AM) *

QUOTE(qaz1134 @ 16 Jul, 2008 - 10:39 AM) *

hi guys i need your help
my program run and compiles but the output is not correct and i really could not locate the error
the program should create a list and insert a node (which properly works) but while the user inputs a value to be it should add the value to the sum of NULL
for example:
the output goes like this
1.create list
2.display list
3.insert node
4 exit

1 // user input

Enter data for the new node:

1 // user input
(then return)

1.create list
2.display list
3.insert node
4 exit

2 // user input

1

1.create list
2.display list
3.insert node
4 exit

3 // user input after display

Enter data wich the insertion is to be done

1 // user input

Insert a data for the node

2 // user input

1.create list
2.display list
3.insert node
4 exit

2 // user input

1
2
This should have a sum of 3 which is not shown and i dont know why???

the code is here
i have an attachment file (Link.exe)
CODE

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node {
int data;
struct node *next;
};

void createlist(struct node **first,int n)
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
*first=p;
p->data=n;
p->next=NULL;
}

void display(struct node *first)
{
struct node *p=first;
if(p==NULL)
  {
   puts("Link list is Empty");
  }
else
  {
   while(p!=NULL)
    {
     printf("\n%d\t%d\t%d\n",p,p->data,p->next);
     p=p->next;
    }
  }
}

void insertnode(struct node **first, int n)
{
int d, sum;
sum=0;
struct node *q;
struct node *p=*first;
q=(struct node*)malloc(sizeof(struct node));
puts("\nEnter data for the inserted node");
scanf("%d",&d);
sum=n+sum;
printf("sum of the nodes is:%d",sum);
while(p!=NULL)
  {
   if(p->data==n)
    {
     q->data=d;
     q->next=p->next;
     p->next=q;
     return;
    }
   p=p->next;
  }
}

void main()
{
struct node *first=NULL;
int ch,n;
do
{
  puts("\nEnter choice\n1.create list\n2.display list");
  puts("3.INsert a node\n4.exit\n");
  scanf("%d",&ch);
  switch(ch)
   {
    case 1: if(first==NULL)
        {
         puts("Enter Data for the new node:");
         scanf("%d",&n);
         createlist(&first,n);
        }
    break;

    case 2: display(first);
    break;

    case 3: puts("\nEnter data of the node after wich the inserion is to be done:");
        scanf("%d",&n);
        printf("\n");
        insertnode(&first,n);
        printf("\n");
        break;
   }
}while(ch!=4);
}


please help me crying.gif





Each time when you call insertnode you are setting sum to zero.
Please check the logic once again.



so sorry but nothing happens unsure.gif
i try to removed it but still nthing
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/28/08 04:19PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month