Ok, I've been trying to find and solve the problem with this source code by using the ddd debuger and I just can't. Here is the code.

//lab1.cc, a driver for list.cc
//CSCI 1120
//Will Briggs


#include <iostream.h>
#include "list.h"


main ()
{
Item Letters [MAX_ITEMS]; int num_letters = 0;


insert (Letters, num_letters, 'A');
insert (Letters, num_letters, 'D');
insert (Letters, num_letters, 'R');
insert (Letters, num_letters, 'E');
insert (Letters, num_letters, 'T');
insert (Letters, num_letters, 'M');
insert (Letters, num_letters, 'V');
insert (Letters, num_letters, 'P');
insert (Letters, num_letters, 'Q');
insert (Letters, num_letters, 'X');


print  (Letters, num_letters);
}


//list.cc, containing functions for operations on an ordered list
//CSCI 1120
//Will Briggs


#include <iostream.h>
#include "list.h"



void insert (Item List[], int& howmany, Item thing)
{
if (howmany == MAX_ITEMS)
cerr << "Error in insert: can't add " << thing
<< ", list is full!" << endl;
else
{
int where = INITIAL_VALUE;


while (thing > List[where] && where < howmany) where++;
//go till at or past position
//of thing OR at end of list


for (int i = howmany; i > where; i--)        //shift all subsequent items
List  = List[i-1];                      //down list by 1


List[where] = thing;                         //finally, add thing in place
howmany++;
}
}


int search (Item List[], int howmany, Item thing)
{
int i = 0;


while (thing > List && i < howmany-1) i++; //go till at or past position
//of thing OR at end of list


if (List == thing) return i; else return NOT_FOUND;
}


void print  (Item List[], int  howmany)
{
for (int i = 0; i < howmany; i++) cout << List << endl;
cout << endl;
}

I know that the two variables that are in red have a wrong value but I can't seem to find the way to change that, if that is the problem.
If someone could give me a hand or a hint with this that would be great.

Thanks a lot.

Give an example of the output you're getting, preferrably at each call to insert. An initial test shows everything to work just fine, but I had to guess on the contents of list.h.

This is what comes put when I try to run the program in the debuger.

Program received signal SIGSEGV, Segmentation fault.
0x080487eb in insert (List=0xbffffb78 "(some numbers and signs), howmany=0xbffffb74, thing=65 'A') at list.cc:18

The error is pointing to this line:

while (thing > List[where] && where < howmany) where++;

For starters, List[where] has an indeterminate value regardless of the value of where because you never initialized Letters. That alone probably won't cause a segmentation fault, so let's see what INITIAL_VALUE is defined as. Most likely that's your problem. I had defined INITIAL_VALUE to be 0. But before you do anything else, initialize that array:

Item Letters [MAX_ITEMS];

for (int i = 0; i < MAX_ITEMS; i++)
  letters[i] = Item();

>List=0xbffffb78 "(some numbers and signs)
The array is uninitialized, that's understandable.

>howmany=0xbffffb74
Your debugger is probably viewing a reference variable as a pointer. The address looks consistent enough to be valid, so we'll assume that it's correct until we figure out the seg fault.

Item Letters [MAX_ITEMS];
for (int i = 0; i < MAX_ITEMS; i++)
letters = Item();

this do I put it in the lab.cc source code where the main is or in the list.cc where the while loop starts, where the problem is? and where do I initialize the INITIAL_VALUE?

>this do I put it in the lab.cc source code where the main is or in the list.cc
>where the while loop starts, where the problem is?
Which of those declares the Letters array? :rolleyes:

>and where do I initialize the INITIAL_VALUE?
Presumably it's already initialized in list.h. If not, that's your problem.

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.