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: - #include <iostream>
-
#include <iomanip>
-
#include <fstream>
-
-
using namespace std;
-
-
ifstream filein;
-
-
struct nodetype; //forward, incomplete declaration
-
typedef nodetype *nodeptrtype;
-
-
struct nodetype
-
{
-
int num;
-
nodeptrtype ptr;
-
nodeptrtype sumptr;
-
};
-
//main()***************************************************
-
void main()
-
{
-
-
filein.open("E:INTFILE.DAT"); //opens external file that contains several (28) random integers
-
-
nodeptrtype sumptr;
-
nodeptrtype front, auxptr, endptr;
-
int numin;
-
int sum = 0;
-
-
-
//this segment creates a linked list by placing each new mode
-
//at the end of the list
-
front = new nodetype;
-
front->num = numin;
-
front->ptr = NULL;
-
endptr = front;
-
-
while(filein)
-
filein>>auxptr;
-
-
do
-
{
-
// cout<<"Enter an integer."; used for keyboard input
-
// cin>>numin;
-
-
if(cin);
-
{
-
auxptr = new nodetype;
-
auxptr->num = numin;
-
auxptr->ptr = NULL;
-
endptr->ptr = auxptr;
-
endptr = auxptr;
-
}
-
}while(cin);
-
-
-
-
//This segment traverses the linked list and displays the integers on one line
-
//trying to get a sum of the integers but it wont work
-
cout<<endl<<endl<<"Values in the first list: "<<endl;
-
auxptr = front;
-
while (auxptr != NULL)
-
{
-
cout<<auxptr->num<<" ";
-
auxptr = auxptr->ptr;
-
sum = sumptr + auxptr;
-
}
-
cout<<endl<<"The sum of the integers in the linked list is: "<<sum<<endl;
-
cout<<endl<<endl;
-
-
}
Any help will be appreciated.
4 2171
Please, Anyone have any ideas?
horace1 1,510
Recognized Expert Top Contributor
Please, Anyone have any ideas?
fixed a few things so it now compiles and executes -
#include <iostream>
-
#include <iomanip>
-
#include <fstream>
-
-
using namespace std;
-
-
ifstream filein;
-
-
struct nodetype; //forward, incomplete declaration
-
typedef nodetype *nodeptrtype;
-
-
struct nodetype
-
{
-
int num;
-
nodeptrtype ptr;
-
nodeptrtype sumptr;
-
};
-
//main()******************************************** *******
-
int main()
-
{
-
-
filein.open("E:INTFILE.DAT"); //opens external file that contains several (28) random integers
-
-
nodeptrtype sumptr;
-
nodeptrtype front, auxptr, endptr;
-
int numin;
-
int sum = 0;
-
-
-
//this segment creates a linked list by placing each new mode
-
//at the end of the list
-
front = new nodetype;
-
front->num = numin;
-
front->ptr = NULL;
-
endptr = front;
-
-
// while(filein) // ** removed ??
-
// filein>>auxptr; // ** removed
-
-
do
-
{
-
// cout<<"Enter an integer."; used for keyboard input
-
// cin>>numin;
-
-
if(cin>>numin); // ** read next number
-
{
-
auxptr = new nodetype;
-
auxptr->num = numin;
-
auxptr->ptr = NULL;
-
endptr->ptr = auxptr;
-
endptr = auxptr;
-
}
-
}while(cin.good()); // ** test if cin is OK
-
-
-
-
//This segment traverses the linked list and displays the integers on one line
-
//trying to get a sum of the integers but it wont work
-
cout<<endl<<endl<<"Values in the first list: "<<endl;
-
auxptr = front;
-
while (auxptr != NULL)
-
{
-
cout<<auxptr->num<<" ";
-
auxptr = auxptr->ptr;
-
sum = sum + auxptr->num; // ** fixed ??
-
}
-
cout<<endl<<"The sum of the integers in the linked list is: "<<sum<<endl;
-
cout<<endl<<endl;
-
-
}
-
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
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.... -
#include <iostream>
-
#include <iomanip>
-
#include <fstream>
-
-
using namespace std;
-
-
ifstream filein;
-
-
struct nodetype; //forward, incomplete declaration
-
typedef nodetype *nodeptrtype;
-
-
struct nodetype
-
{
-
int num;
-
nodeptrtype ptr;
-
nodeptrtype sumptr;
-
};
-
//main()******************************************** *******
-
int main()
-
{
-
-
filein.open("E:INTFILE.DAT"); //opens external file that contains several (28) random integers
-
-
nodeptrtype sumptr;
-
nodeptrtype front=NULL;
-
nodeptrtype auxptr, endptr;
-
int numin;
-
int sum = 0;
-
-
-
do
-
{
-
-
if(cin>>numin); // ** read next number
-
{
-
if(front==NULL)
-
{
-
front = new nodetype;
-
front->num = numin;
-
front->ptr = NULL;
-
endptr = front;
-
}
-
else
-
{
-
auxptr = new nodetype;
-
auxptr->num = numin;
-
auxptr->ptr = NULL;
-
endptr->ptr = auxptr;
-
endptr = auxptr;
-
}
-
}
-
}while(cin.good()); // ** test if cin is OK
-
-
-
-
//This segment traverses the linked list and displays the integers on one line
-
//trying to get a sum of the integers
-
cout<<endl<<endl<<"Values in the first list: "<<endl;
-
auxptr = front;
-
while (auxptr->ptr != NULL)
-
{
-
cout<<"fred";
-
cout<<auxptr->num<<" ";
-
auxptr = auxptr->ptr;
-
sum = sum + auxptr->num; // ** fixed ??
-
}
-
cout<<endl<<"The sum of the integers in the linked list is: "<<sum<<endl;
-
cout<<endl<<endl;
-
getchar();
-
}
-
-
Thanks, I'll work with that.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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,...
|
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...
|
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;
|
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...
|
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)
| |
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.
|
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;
|
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;
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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();...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |