473,385 Members | 1,356 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,385 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 2140
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.