473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Help with pointers

143 100+
I am trying to write a program that will use pointers and dynamic variables to create a linked list. I need help reading the external file and placing those integers in the list. Then I need help arriving at the sum of those integers.

Here's what I've got so far:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. ifstream filein;
  8.  
  9. struct nodetype;    //forward, incomplete declaration
  10. typedef nodetype *nodeptrtype;
  11.  
  12. struct nodetype
  13. {
  14.     int num;
  15.     nodeptrtype ptr;
  16.     nodeptrtype sumptr;
  17. };
  18. //main()***************************************************
  19. void main()
  20. {
  21.  
  22.     filein.open("E:INTFILE.DAT");  //opens external file that contains several (28) random integers
  23.  
  24.     nodeptrtype sumptr;
  25.     nodeptrtype front, auxptr, endptr;
  26.     int numin;
  27.     int sum = 0;
  28.  
  29.  
  30. //this segment creates a linked list by placing each new mode
  31.     //at the end of the list
  32.         front = new nodetype;
  33.         front->num = numin;
  34.         front->ptr = NULL;
  35.         endptr = front;
  36.  
  37.     while(filein)
  38.         filein>>auxptr;
  39.  
  40.     do
  41.     {
  42. //        cout<<"Enter an integer.";  used for keyboard input
  43. //        cin>>numin;
  44.  
  45.         if(cin);
  46.         {
  47.             auxptr = new nodetype;
  48.             auxptr->num = numin;
  49.             auxptr->ptr = NULL;
  50.             endptr->ptr = auxptr;
  51.             endptr = auxptr;
  52.         }
  53.     }while(cin);
  54.  
  55.  
  56.  
  57. //This segment traverses the linked list and displays the integers on one line
  58.     //trying to get a sum of the integers but it wont work
  59.     cout<<endl<<endl<<"Values in the first list: "<<endl;
  60.     auxptr = front;
  61.     while (auxptr != NULL)
  62.     {
  63.         cout<<auxptr->num<<" ";
  64.         auxptr = auxptr->ptr;
  65.         sum = sumptr + auxptr;
  66.     }
  67.     cout<<endl<<"The sum of the integers in the linked list is: "<<sum<<endl;
  68.     cout<<endl<<endl;
  69.  
  70. }
Any help will be appreciated.
Dec 5 '06 #1
4 2142
teddarr
143 100+
Please, Anyone have any ideas?
Dec 6 '06 #2
horace1
1,510 Expert 1GB
Please, Anyone have any ideas?
fixed a few things so it now compiles and executes
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. ifstream filein;
  8.  
  9. struct nodetype;    //forward, incomplete declaration
  10. typedef nodetype *nodeptrtype;
  11.  
  12. struct nodetype
  13. {
  14.     int num;
  15.     nodeptrtype ptr;
  16.     nodeptrtype sumptr;
  17. };
  18. //main()********************************************  *******
  19. int main()
  20. {
  21.  
  22.     filein.open("E:INTFILE.DAT");  //opens external file that contains several (28) random integers
  23.  
  24.     nodeptrtype sumptr;
  25.     nodeptrtype front, auxptr, endptr;
  26.     int numin;
  27.     int sum = 0;
  28.  
  29.  
  30. //this segment creates a linked list by placing each new mode
  31.     //at the end of the list
  32.         front = new nodetype;
  33.         front->num = numin;
  34.         front->ptr = NULL;
  35.         endptr = front;
  36.  
  37. //    while(filein)               // ** removed ??
  38.     //    filein>>auxptr;         // ** removed
  39.  
  40.     do
  41.     {
  42. //        cout<<"Enter an integer.";  used for keyboard input
  43. //        cin>>numin;
  44.  
  45.         if(cin>>numin);   // ** read next number
  46.         {
  47.             auxptr = new nodetype;
  48.             auxptr->num = numin;
  49.             auxptr->ptr = NULL;
  50.             endptr->ptr = auxptr;
  51.             endptr = auxptr;
  52.         }
  53.     }while(cin.good());      // ** test if cin is OK
  54.  
  55.  
  56.  
  57. //This segment traverses the linked list and displays the integers on one line
  58.     //trying to get a sum of the integers but it wont work
  59.     cout<<endl<<endl<<"Values in the first list: "<<endl;
  60.     auxptr = front;
  61.     while (auxptr != NULL)
  62.     {
  63.         cout<<auxptr->num<<" ";
  64.         auxptr = auxptr->ptr;
  65.         sum = sum + auxptr->num;    // ** fixed ??
  66.     }
  67.     cout<<endl<<"The sum of the integers in the linked list is: "<<sum<<endl;
  68.     cout<<endl<<endl;
  69.  
  70. }
  71.  
it reads from the keyboard at the moment - you can change to read from your data file

when I ran the program entering 1 2 3 4 5 6 ctrl/z it gave
1
2
3
4
5
6
^Z
Values in the first list:
14 1 2 3 4 5 6 6

then crashed with a segmentation error - clearly still got some problems but can probably solve them now
Dec 6 '06 #3
DeMan
1,806 1GB
I think there are 2 problems with the code....
Firstly front is declared but never set to anything (hence the first strange output)
Secondly, [ctrl z] is a character and is added to the list, so we read too many elements.

I have messed around with(that is modified) it and have somethiong I think works....

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. ifstream filein;
  8.  
  9. struct nodetype;    //forward, incomplete declaration
  10. typedef nodetype *nodeptrtype;
  11.  
  12. struct nodetype
  13. {
  14.     int num;
  15.     nodeptrtype ptr;
  16.     nodeptrtype sumptr;
  17. };
  18. //main()********************************************    *******
  19. int main()
  20. {
  21.  
  22.     filein.open("E:INTFILE.DAT");  //opens external file that contains several (28) random integers
  23.  
  24.     nodeptrtype sumptr;
  25.     nodeptrtype front=NULL; 
  26.     nodeptrtype auxptr, endptr;
  27.     int numin;
  28.     int sum = 0;
  29.  
  30.  
  31.     do
  32.     {
  33.  
  34.         if(cin>>numin);   // ** read next number
  35.         {
  36.             if(front==NULL)
  37.             {
  38.               front = new nodetype;
  39.               front->num = numin;
  40.               front->ptr = NULL;
  41.               endptr = front;
  42.            } 
  43.            else
  44.            {   
  45.             auxptr = new nodetype;
  46.             auxptr->num = numin;
  47.             auxptr->ptr = NULL;
  48.             endptr->ptr = auxptr;
  49.             endptr = auxptr;
  50.           }
  51.         }
  52.     }while(cin.good());      // ** test if cin is OK
  53.  
  54.  
  55.  
  56. //This segment traverses the linked list and displays the integers on one line
  57.     //trying to get a sum of the integers 
  58.     cout<<endl<<endl<<"Values in the first list: "<<endl;
  59.     auxptr = front;
  60.     while (auxptr->ptr != NULL)
  61.     {
  62.           cout<<"fred";
  63.         cout<<auxptr->num<<" ";
  64.         auxptr = auxptr->ptr;
  65.         sum = sum + auxptr->num;    // ** fixed ??
  66.     }
  67.     cout<<endl<<"The sum of the integers in the linked list is: "<<sum<<endl;
  68.     cout<<endl<<endl;
  69.     getchar();
  70. }
  71.  
  72.  
Dec 6 '06 #4
teddarr
143 100+
Thanks, I'll work with that.
Dec 7 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
3
by: Josh | last post by:
Howdy i was recently given a program to do. I have to create a 2d matrix with pointers i have the whole idea down with pointers but there is a problem with one of them i have the code written down...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
1
by: ArcInversion | last post by:
Hi, I've been using a javascript script to create a dragon that flies across the page. Anyways, I'd like to make it so when you click the dragon it takes you to a new page. Was wondering if anyone...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
9
by: Andreas Vinther | last post by:
I have a small piece of code that compiles but does not perform like I want it to. This code works: ---------------- void *y0; void *y1; void *y2; void *y3;
6
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
5
by: BillGill | last post by:
Ok, I assume this has been asked many times, but I can't seem to come up with a good Google search to find it. I am trying to learn C++. Specifically I have Microsoft Visual C++ 2005 Express...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.