HAving trouble with my pointer arithmetic, any ideas appreciated
My orgional code

``````Activity p[51];

for ( int i = 1; i <= n; ++i ) {
pert >> p[i].i >> p[i].j >> p[i].pt
>> p[i].prt >> p[i].ot;
pert.getline(p[i].desc,21);``````

converted but wrong

``````Activity **p =  new Activity*[51];

for (  i = 1; i <= n; ++i ) {
if (p[i].i == NULL)
break;
pert >> (*p+i)->i >> *(p+i)->j >> *(p+i)->pt
>> *(p+i)->prt >> *(p+i)->ot;
pert.getline(*(p+i)->desc,21);

}``````

my struct

``````struct Activity {
int i,               // Beginning node number.
j;               // Ending node number.
float  pt,           // Pessimistic time.
prt,          // Most probable time.
ot;           // Optimistic time.
char desc[21];       // Activity description.
};``````

if you want the whole code let me know

## All 14 Replies

Why are you starting your array at 1 instead of 0? Also, how big is n?

started the array at 1 becaused that was a requirement of the assigmnent. n is the number of activities and in the test case its 11. I'm not sure i can dynamically allocate for n without alot of copying.

Can you indicate what the 'problem' is? Is it when you attempt to print the contents of the array?

Can you indicate what the 'problem' is? Is it when you attempt to print the contents of the array?

I can't seem to figure out the syntax of a pointer to an array of pointers to structs. In the origional code it works fine but when i try to transform it to pointer arithmetic the syntax is wrong.
I am trying to read each line as a new activity and then have all the activities in an array.

Post the code where you are trying to utilize/print out the array to show where the problem manifests itself..

``````Activity **p = new Activity*[51];

for ( int i = 1; i <= n; ++i ) {
pert >> (*(p+i))->i >> (*(p+i))->j >> (*(p+i))->pt
>> (*(p+i))->prt >> (*(p+i))->ot;
pert.getline((*(p+i))->desc,21);
}``````

``````Activity **p = new Activity*[51];

for ( int i = 1; i <= n; ++i ) {
pert >> (*(p+i))->i >> (*(p+i))->j >> (*(p+i))->pt
>> (*(p+i))->prt >> (*(p+i))->ot;
pert.getline((*(p+i))->desc,21);
}``````

This is how i implemented it

``````for (  i = 1; i <= n; ++i ) {
cout<< "test5" << endl;
if ((*(p+i))->i == NULL){
cout<< "test4" << endl;
break;
}
cout<< "test4" << endl;

pert >> (*(p+i))->i >> (*(p+i))->j >> (*(p+i))->pt
>> (*(p+i))->prt >> (*(p+i))->ot;
pert.getline((*(p+i))->desc,21);
cout << (*(p+i))->i << endl;
}``````

Can you tell me why im getting a seg fault?

Either n is bigger than 50, which is the last accessible index in your array, or you aren't allocating memory to each Activity pointer in the array. You probably want to allocate the initial memory like this:

``````Activity **p = new Activity*[51];

for (int i = 0; i < 51; i++)
p[i] = new Activity;``````

Then you release the memory like this:

``````for (int i = 0; i < 51; i++)
delete [] p[i];
delete p;``````

Unless you do this, you'll have to initialize each pointer to an existing object before you try to dereference it.

ok i adjusted some things as that doesnt seem to be what i wanted, but my code still wont read in the text file and then allow me to print it out as needed.

``````Activity *p =  new Activity [51];

for (i = 1; i <= n; ++i )
{
cout<< "test5" << endl;
if ((p+i)->i == NULL)
{
cout<< "test6" << endl;
break;
}
cout<< "test7" << endl;
pert >> (p+i)->i >> (p+i)->j >> (p+i)->pt
>> (p+i)->prt >> (p+i)->ot;
pert.getline((p+i)->desc,21);
cout << (p+i)->i << endl;
}``````

the first line of the text file reads
1 2 1 2 4 ORD.FIXTURES

but when i do
cout << (p+i)->i << endl;
i get a 0
and the final out put is just garbage ( if you want the final out put code i can post it but its large)

Dude, you're gonna have to give us something complete. A full program that doesn't work, and a chunk of the file that you're using so that we can actually test this and answer your question completely without all the back and forth.

Dude, you're gonna have to give us something complete. A full program that doesn't work, and a chunk of the file that you're using so that we can actually test this and answer your question completely without all the back and forth.

okies sorry i was hoping it was a quick and easy answer

MAIN :

``````#include "pert.h"
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cerrno>
#include <cstring>

using namespace std;

int main(int argc, char *argv[])//argc is the argument coutn argv is the the test.txt
{

int n,              // Number of activities.
nn;  // Largest node number.
//Activity p[51];
//Activity **p =  new Activity*[51]; seems to work
Activity *p =  new Activity [51];

// Activity *p =  new Activity [51];
float expt[51];
float stdd[51],
bucket[51],
es[51],
ef[51],
ls[51],
lf[51],
totsl[51],
rem[1],//so that it is able to be changed
totmax[1];//see rem
char job[21];
int i;
ifstream pert;

cout<< "test1" << endl;
totmax[0] = 0.0;
// Correct number of arguments?
if ( argc != 2 ) {
cerr << "usage: " << argv[0] << " filename\n";
return 1;
}

cout<< "test2" << endl;
// Open file name given on command line.
pert.open(argv[1]);
if ( pert.fail() ) {
perror(argv[0]);
return 2;
}

cout<< "test3" << endl;
// Read the number of activities (n) and highest node number (nn)
//pert >> n >> nn;
if ( pert.eof() )
{
cerr << argv[1] << ": is empty\n";
return 3;
}

cout<< "test4" << endl;

//for ( i = 0; i < 9; i++)
//p[i] = new Activity;
cout<< "test4.5" << endl;
for (i = 1; i <= n; ++i )
{
cout<< "test5" << endl;
if ((p+i)->i == NULL)
{
cout<< "test6" << endl;
break;
}
cout<< "test7" << endl;
pert >> (p+i)->i >> (p+i)->j >> (p+i)->pt
>> (p+i)->prt >> (p+i)->ot;
pert.getline((p+i)->desc,21);
cout << (p+i)->i << endl;
}

pert.close();
int x;
n = i -1;

for ( nn = 0 ,  x = 1; x < i ; ++x)
{
if ((p+i)->i > nn)
nn = (p+i)->i;
}
for ( x = 1 ; x < i ; ++x)
{
if ((p+i)->j > nn)
nn = (p+i)->j;
}
nn = 9;
n =11;
cout << (p+i)->ot << endl;//not printing anything
/***********************************************************************************************
BEGIN FUNCTIONS
***********************************************************************************************/
// Calculate expected times and standard deviation.
expect_t(stdd, expt, p ,  n);

// Clear out node buckets.
clear_bucket(bucket, nn);

// Calculate early start using forward scan rule.
early_start(rem, p, es, bucket, expt, n);

// Calculate early finish time.
early_finish(es, n, totmax, expt, ef);

// Calculate late start time using reverse scan rule.
late_start(n, p, ls, bucket, rem, expt);//

// Calculate late finish time.
late_fin(ls, expt, lf, n);

// Calculate total slack.
slack(n, ls, es, totsl);

// Calculate critical path.

// Print Report
pert_out(p, expt,  stdd,  es,  ef,  lf,  ls,  totsl, n, ask);

return 0;
}``````

pert.h

``````//header file for malt and main
// contains the struct for both
#ifndef PERT_H
#define PERT_H
struct Activity {
int i,               // Beginning node number.
j;               // Ending node number.
float  pt,           // Pessimistic time.
prt,          // Most probable time.
ot;           // Optimistic time.
char desc[21];       // Activity description.
};
//time stucture
/*struct today{
*/
/*
preconditions:
n = the number of  activities
ls = array of late start ls[0] is undefined ls[n] is the last position in the array
es = early start array es[0] is undefined es[n] is the last position in the array
totsl = total slack each array position coresponds with each activity
*/
float* slack(int n, float ls[], float es[], float totsl[]);
/*
postconditions:
undefined if any of the variables are not what are defined in preconditions
returns totsle
*/
/*
preconditions:
ls = array of late start ls[0] is undefined ls[n] is the last position in the array
expt = array of expected finish time
lf = array of the latest finish times
n = the number of  activities
*/
float* late_fin(float ls[], float expt[], float lf[], int n);
/*
postconditions:
undefined if any of the variables are not what are defined in preconditions
returns lf
*/
/*
preconditions:
n = the number of  activities
p = struct containing all pertinent information for the activity
bucket = value holding array
rem = variable holder
expt = array of exptected finish time
*/
float* late_start(int n, struct Activity p[],float ls[], float bucket[], float rem[], float expt[]);
/*
postconditions:
undefined if any of the variables are not what are defined in preconditions
returns rem

*/
/*
preconditions:
totmax = variable holder
bucket = value holding array
int nn = largest node number
*/
float* load_bucket(float totmax[], float bucket[], int nn);
/*
postconditions:
undefined if any of the variables are not what are defined in preconditions
returns bucket
*/
/*
preconditions:
es = early start array es[0] is undefined es[n] is the last position in the array
n = the number of  activities
rem = variable holder
expt = array of expected finish time
ef = array of early finish times

*/
float* early_finish(float es[], int  n, float totmax[], float expt[], float ef[]);
/*
postconditions:
undefined if any of the variables are not what are defined in preconditions
returns totmax
*/
/*
preconditions:
rem = variable holder
activity = is the struct containing the information from the file
es = early start array es[0] is undefined es[n] is the last position in the array
bucket = value holding array
expt = array of expected finish time
n = the number of  activities

*/
float* early_start(float rem[], struct Activity p[] , float es[], float bucket[], float expt[], int n);
/*
postconditions:
undefined if any of the variables are not what are defined in preconditions
returns rem
*/
/*
preconditions:
bucket = value holding array
int nn = largest node number
*/
float* clear_bucket(float bucket[], int nn);
/*
postconditions:
undefined if any of the variables are not what are defined in preconditions
returns bucket
*/
/*
preconditions:
array = array to be printed
*/
void prnt_array(char* array);
/*
postconditions:
undefined if any of the variables are not what are defined in preconditions
returns nothing

*/
/*
preconditions:
ask = array that holds '*' in position for critical path marking
totsl = total slack each array position coresponds with each activity
n = the number of  activities

*/
char* critical_path(char ask[], float totsl[] , int n);
/*
postconditions:
undefined if any of the variables are not what are defined in preconditions
returns an array of '*' if the corresponding job is critical

*/
/*
preconditions:
stdd = array of standard deviation
expt = array of expected finish time
activity = is the struct containing the information from the file
n = the number of  activities

*/
float* expect_t(float stdd[], float expt[], struct Activity p[] , int n);
/*
postconditions:
undefined if any of the variables are not what are defined in preconditions
returns expt
*/
/*
preconditions:
*/
int pert_out(struct Activity *p, float expt[], float stdd[], float es[], float ef[], float lf[], float ls[], float totsl[], int n, char ask[]);
/*
postconditions
*/
/*
preconditions:
*/
int date_time(void);
/*
postconditions
*/
#endif``````

pert_out.cpp

``````#include "pert.h"
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>

using namespace std;

int pert_out(struct Activity *p, float expt[], float stdd[], float es[], float ef[], float lf[], float ls[], float totsl[], int n, char ask[])
{
//cout << "12345678901234567890123456789012345678901234567890123456789012345678901234567890" << endl;

//system("CLS");
cout << setw(44) << "PERT ANALYSIS"<< endl << endl;
date_time();
cout << endl;
cout << " ACTIVITY    JOB       EXP.      STD    EARLY   EARLY    LATE    ";
cout << "LATE   TOTAL  C" ;

cout << endl;

cout << "  I   J  DESCRIPTION   TIME      DEV    START  FINISH   START  FINISH   SLACK  P";
cout << endl;
cout << endl;
cout << fixed;
//cout << "n = "<<  n <<   endl;
for ( int i = 1; i <= n; ++i ){
char job[21];

//i and j out put
cout << "";
cout.width(3);
cout <<  (p+i)->i;
cout << " ";
cout.width(3) ;
cout <<  (p+i)->j;
cout.fill(' ');
cout << " ";

//converting job to a printable array
for(int x = 1 ; x <= 11; ++x)
{
job[x] = (p+i)->desc[x];
}
prnt_array(job);

//exp time
cout << "  ";
cout << setprecision(1);
cout.width(6);
cout << expt+i;
//std dev
cout << "   ";
cout << setprecision(2);
//cout.fill('');
cout.width(6);
cout << stdd[i];

//early start
cout << "   ";
cout << setprecision(2);
cout.width(6);
cout << es[i];

//early finish
cout << "   ";
cout << setprecision(1);
cout.width(5);
cout << ef[i];

//late start

cout << "   ";
cout << setprecision(1);
cout.width(5);
cout << fixed;
cout << ls[i];
//late finish
cout << "   ";
cout << setprecision(1);
cout.width(5);
cout << lf[i];
//total slack
cout << "   ";
cout << setprecision(1);
cout.width(5);
cout << totsl[i];
cout << "  ";

cout << endl;

}
return 0;
}``````

The rest of the functions just do math on the arrays but as you can see from main there is alot of them. If you want them i will add them on.

``````1 2 1 2 4 ORD.FIXTURES
1 4 1 2 3 EXCAVATION
1 7 3 4 5 ORD. TREES SHRUBS
2 3 3 4 5 MANUF. FURNIT.
3 6 1 2 2 DELIVER FURNIT.
4 5 4 5 6 ERECT STRUC. STL
4 8 1 3 5 BACK FILLING
5 6 3 5 6 MASONRY WORK
6 9 1 5 7 INSTALL FURNIT.
7 8 2 3 4 FINAL GRADING
8 9 4 5 7 PLT TREES,SHRUBS``````

The point of the assignment is to make it so that the file can be dealt with using pointers, in the origional code everything worked as it was supposed. But there was no pointers used (example in my origional post.

Here's a trick for fixing problems like this one. Cut out all of the fluff from your program so that it's absolute bare-bones, but still has the problem. Errors are easier to see when you don't have a lot of code all around them diverting your attention. There are three rules to follow:

1) Use a single source file. No headers except the required standard ones.
2) Output is irrelevant unless the problem is with formatting. Remove all pretty-print functionality.
3) Hardcode everything. User input is nice, but it bulks up a program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.