473,769 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with pointers

143 New Member
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 2171
teddarr
143 New Member
Please, Anyone have any ideas?
Dec 6 '06 #2
horace1
1,510 Recognized Expert Top Contributor
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 Top Contributor
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 New Member
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
3852
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 contests (http://online-judge.uva.es/p/v1/127.html). The program I wrote seems to work fine, but it takes way too much memory to run. I am not that good at programming C++, unfortunately, so I can't seem to find my memory leak. As far as I can tell,...
3
1944
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 objects of my classes Person(superclass), Student(inherit Person), Teacher(inherit Person) in that array. The name will be the unique key. These classes are all working ok. I want to be able to add, remove, find etc. objects. To all of this, I...
3
8599
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 at bottom so any help will help. The problem is that after it allocates memory for the array of pointers it seems like it loops an extra time, giving it one more element. Thanks int **ptr1, *matrixInfo; int size;
2
1816
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 called the function wordcount, and then the function did everything including printing out the results. That worked. Now I want to make the function return an array of pointers to struct palabra so the calling function can manage the data as it...
1
1785
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 could help me out here. Below is the complete script. <SCRIPT language="JavaScript1.2"> var cursorpath="http://i68.photobucket.com/albums/i9/worklog_halcyon/Misc/pet1_lohi_dog.png" if (document.layers)
23
2551
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
1687
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
3517
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
2985
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 standard dynamic address book program. Adding, and listing work just fine. However, deleting, editing and viewing relies on member function retAddress. This function returns an array of pointers that are pointing to created objects. In action, all it...
5
1401
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 Edition. I am trying to learn it from Ivor Horton's Beginning Visual C++ 2005. My problem comes when we get to pointers. I just cannot seem to wrap my mind around the complexities involved. Is there some place, either a book or a website that...
0
9415
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10198
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9848
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8860
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7392
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.