-3

My code is behaving really weird. It works sometimes and it crashes at other times.

When it crashes it says:

a problem caused the program to stop working correctly

My main:

int main() {
    start();
    Read();

    cout << "Enter the code of the course: ";
    cin >> coursecode;
    cout << "\n1111111\n";
    searchCourse(coursecode);
    cout << "\n222222\n";

    return 0;
}

I wrote two couts above and below my searchCourse function to see if the program compiles all the lines. It really does compile everything, and at the end it prints 222222 before crashing.

The start method just creates an array of BinaryTree objects then store the student data (they are read from text file) according to their courses.

start():

BinaryTree *a[10];

void start()
{
    for(int g=1;g<=10;g++)
    {
        a[g] = new BinaryTree(g);
    }
}

searchCourse():

void searchCourse(string code)
{
    for(int j=1;j<=count;j++)
    {
        if(a[j]->checkit(code)!=0)
        {
            a[j]->display();

            break;
        }
    }
}

Checkit() in BinaryTree.h:

bool checkit(string m)
{
    if (root==NULL)
        return false;
    else
        if(root->data.getCourse()==m)
            return true;
        else
            return false;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ibrahim
  • 573
  • 1
  • 6
  • 20
  • 1
    Please provide a [mcve]. – Ron Dec 19 '17 at 21:51
  • 1
    Arrays start at index `0` in `C++`, not 1. I would have suggested reading a few C++ books, but the program you're writing suggests you should have known basic things like this already. – PaulMcKenzie Dec 19 '17 at 21:59
  • @PaulMcKenzie i do remember our teacher starting arrays from 1 . specially in heap concept . i dont know if it has special cases in which it might be better to start it from 1 but i did not think it will make a big trouble doing so in my case. thanks for the reply – ibrahim Dec 19 '17 at 22:27
  • @ibrahim That teacher should be fired. – pepperjack Dec 19 '17 at 22:28
  • *i do remember our teacher starting arrays from 1 . specially in heap concept* -- That doesn't change the rules of C++. Array indices start at 0 and go up to `n-1`, where `n` is the total number of entries. Trying to write fake 1-based arrays in C++ usually leads to problems such as yours. – PaulMcKenzie Dec 19 '17 at 22:30

1 Answers1

0
BinaryTree *a[10];
for(int g=1;g<=10;g++)
{
    a[g] = new BinaryTree(g);
}

Would have a memory exception. You have an array of 10, and you are trying to access the 11th element (since you go until g<=10, and a[10] is the eleventh element). Use:

for(int g=0;g<10;g++)

instead. You may have to also do new BinaryTree(g+1); if Binary Tree starts at 1.

This is an error that is in other places in your code as well, like for(int j=1;j<=count;j++) (for(int j=0;j<count;j++) is probably what you want).

Arrays start at 0. Why does the indexing start with zero in 'C'?

pepperjack
  • 673
  • 6
  • 20
  • YES! I failed to see this.after changing it i ran it 10 times and its doing well,by now it should have crashed at least twice.Do you have any idea why it was not throwing exception everytime i ran it? thank you very much – ibrahim Dec 19 '17 at 22:07
  • 1
    @ibrahim There is no required behaviour for running out of bounds. The program does not have ti throw an exception. It does not have to crash. It can even look like it's working until suddenly and painfully it doesn't. Testing all array accesses for a boundary violation would be an unnecessary tax on the vast majority of programs that play nice and stay inside their bounds, so checks are only made if the programmer makes them. – user4581301 Dec 19 '17 at 22:11