| | |
Sum in the linked list
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 56
Reputation:
Solved Threads: 1
Can anyone please help me to find the sum in the linked list. I have written codes to insert and remove numbers from lists but I have no idea how to calculate the sum.
C++ Syntax (Toggle Plain Text)
void List::insert( const double &value ) { ListNode *newPtr = getNewNode( value ); if ( isEmpty() ) firstPtr = lastPtr = newPtr; else { newPtr->nextPtr = firstPtr; firstPtr = newPtr; } }
•
•
Join Date: Mar 2008
Posts: 56
Reputation:
Solved Threads: 1
I came up with this codes here :-
comipler is showing me errors!
C++ Syntax (Toggle Plain Text)
int List::sumOfNodes(); { int sum = 0; Listnode *currentPtr = firstPtr; while( currentPtr ! = null) { sum = sum + currentPtr; currentPtr = currentPtr->nextPtr; } cout<<"The sum is "<<sum<<endl; }
comipler is showing me errors!
•
•
Join Date: Mar 2008
Posts: 44
Reputation:
Solved Threads: 3
•
•
•
•
I came up with this codes here :-
C++ Syntax (Toggle Plain Text)
int List::sumOfNodes(); { int sum = 0; Listnode *currentPtr = firstPtr; while( currentPtr ! = null) { sum = sum + currentPtr; currentPtr = currentPtr->nextPtr; } cout<<"The sum is "<<sum<<endl; }
comipler is showing me errors!
and paste the error messages too if possible.
•
•
Join Date: Mar 2008
Posts: 56
Reputation:
Solved Threads: 1
well that was my function and I got this error message
1>c:\users\owner\desktop\new folder\list.h(173) : error C2761: 'int List::sumOfNodes(void)' : member function redeclaration not allowed
1>c:\users\owner\desktop\new folder\list.h(174) : error C2447: '{' : missing function header (old-style formal list?)
I called this method in driver :_
listObject.sumOfNodes();
listObject is an object of type List.
am I doing something wrong in function?
1>c:\users\owner\desktop\new folder\list.h(173) : error C2761: 'int List::sumOfNodes(void)' : member function redeclaration not allowed
1>c:\users\owner\desktop\new folder\list.h(174) : error C2447: '{' : missing function header (old-style formal list?)
I called this method in driver :_
listObject.sumOfNodes();
listObject is an object of type List.
am I doing something wrong in function?
Here are a few errors I can see
remove the ';' at the end.
null to be written as NULL
currentPter is of type Listnode not int. You will have to do something like
also the function must return a value.
C++ Syntax (Toggle Plain Text)
int List::sumOfNodes();
C++ Syntax (Toggle Plain Text)
while( currentPtr ! = null)
null to be written as NULL
C++ Syntax (Toggle Plain Text)
sum = sum + currentPtr;
C++ Syntax (Toggle Plain Text)
sum=sum+currentPtr->value;
also the function must return a value.
There are 10 types of people in the world, those who understand binary and those who don't.
All generalizations are wrong. Even this one.
All generalizations are wrong. Even this one.
•
•
Join Date: Mar 2008
Posts: 56
Reputation:
Solved Threads: 1
here is my updated one but still giving me an error :-
C++ Syntax (Toggle Plain Text)
int List::sumOfNodes() { int sum = 0; ListNode *currentPtr = firstPtr; while( currentPtr != NULL) { sum = sum + currentPtr; currentPtr = currentPtr->nextPtr; } return sum; cout<<"The sum is "<<sum<<endl; }
•
•
Join Date: Mar 2008
Posts: 44
Reputation:
Solved Threads: 3
•
•
•
•
here is my updated one but still giving me an error :-
C++ Syntax (Toggle Plain Text)
int List::sumOfNodes() { int sum = 0; ListNode *currentPtr = firstPtr; while( currentPtr != NULL) { sum = sum + currentPtr; currentPtr = currentPtr->nextPtr; } return sum; cout<<"The sum is "<<sum<<endl; }
sum = sum + currentPtr;
should be
C++ Syntax (Toggle Plain Text)
sum=sum+currentPtr->value; //value=the variable you are using to store the numbers.
sum is int and currentPtr->value is an int. but currentPtr is of type ListNode.
after the return statement is encountered the codes after that are inaccessible.
swap the return and cout statements.
Last edited by rje7; Apr 30th, 2008 at 4:01 am.
![]() |
Similar Threads
- Recursive procedure (to display a linked list) (Pascal and Delphi)
- linked list errors (C++)
- sum of the linked list (C)
- sort linked list help please (C)
Other Threads in the C++ Forum
- Previous Thread: About Network programing?
- Next Thread: how do I?
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





