Programme builds fine, shuts down when used.

The point of the function is to see if the array (*Pointer points to it) has any 3's in it. If so, they should be deleted, the rest of the array has to be shoven forward and the endings which don't have any values by then turned into 0's.

This is where it shuts down:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cstdlib>

using namespace std;

void CheckArray(int* pointer, const int lengte)
{
    int x, z;
    int* kopie;
    int* kPointer;

    pointer--;

    for(int a = 0; a != lengte; a++)
    {
        pointer++;

        if (*pointer == 3)
        {
            x++;
            kPointer = pointer;
            z = a;

            for(; z != lengte - 1; z++)
            {
            kopie = kPointer + 1;
            *kPointer = *kopie;
            kPointer++;
            }


        }

    }

    for(; lengte - 1 - x != lengte - 1 ; x--)
    {
    kopie = pointer - x;
    *kopie = 0;
    }

}


Does anyone see something wrong with this?
Add cin.ignore(); to the end of it.

The problem is that, once it gets to the end, the program thinks that it's done. So it closes.

By saying adding cin.ignore();, you're telling the program to wait for the user to enter something, then ignore whatever the user entered. This effectively makes your program a "Press Enter to exit" deal.

Note: You want to add cin.ignore(); just before your return 0; at the end of the int main() code block, not inside of your function there.
Last edited on
No, I mean I get an error , it has to shut down unexpectedly because of an error.
And the error happens in this piece of code.
There has to be something wrong with it...

But thanks for your advice :)
Oh! Hah, I'm sorry.

Could you tell me what this loop is supposed to do?
1
2
3
4
5
for(; lengte - 1 - x != lengte - 1 ; x--)
    {
    kopie = pointer - x;
    *kopie = 0;
    }


Is it just to clear the array?

I wonder if this would work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdlib>

using namespace std;

void CheckArray(int* pointer, const int lengte)
{
    int x, z;
    int* kopie;
    int* kPointer;

    pointer--;

    for(int a = 0; a != lengte; a++)
    {
        pointer++;

        if (*pointer == 3)
        {
            x++;
            kPointer = pointer;
            z = a;

            for(; z != lengte - 1; z++)
            {
            kopie = kPointer + 1;
            *kPointer = *kopie;
            kPointer++;
            }


        }

    }

    kopie = pointer;
    for(; lengte - 1 - x != lengte - 1 ; x--)
    {
    kopie--;
    *kopie = 0;
    }

}


I'm a newbie, so there's a good chance that I'm wrong, but I'm not sure if directly substracting an integer from a pointer like this works out well. The above code uses the decrement operator instead, to see if that might work.
Last edited on
I'll try it. And that one is supposed to fill the last ones with 0's.
Nope doesn't work...
Thanks Chemical Engineer! I found it, you indeed can't subtract integers from pointers !
I found a fix! :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cstdlib>

using namespace std;

void CheckArray(int* pointer, const int lengte)
{
    int x, z;
    int* kopie;
    int* kPointer;

    pointer--;

    for(int a = 0; a <= lengte; a++)
    {
        pointer++;

        if (*pointer == 3)
        {
            x++;
            kPointer = pointer;
            z = a;

            for(; z != lengte - 1; z++)
            {
            kopie = ++kPointer ;
            kPointer--;
            *kPointer = *kopie;
            kPointer++;
            }


        }

    }

    int* pointer2;

    for(; lengte - x <= lengte ; x--)
    {
        pointer2 = pointer;
        int r = 0;
        while( r < x)
        {
            pointer2--;
            r++;
        }
    kopie = pointer2;
    *kopie = 0;
    }

}
Now i have one REALLY weird issue.
When I make a int variable and place it in the if check, the prog crashes...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstdlib>

using namespace std;

void CheckArray(int* pointer, const int lengte)
{
    int x, z;
    int* kopie;
    int* kPointer;
    int number = 4;


    pointer--;

    for(int a = 0; a <= lengte; a++)
    {
        pointer++;

        if (*pointer == number)
        {
            x++;
            kPointer = pointer;
            z = a;

            for(; z != lengte - 1; z++)
            {
            kopie = ++kPointer ;
            kPointer--;
            *kPointer = *kopie;
            kPointer++;
            }


        }

    }

    int* pointer2;

    for(; lengte - x <= lengte ; x--)
    {
        pointer2 = pointer;
        int r = 0;
        while( r < x)
        {
            pointer2--;
            r++;
        }
    kopie = pointer2;
    *kopie = 0;
    }

}
Last edited on
Are you sure you are passing a valid pointer?
You are using x, but it never starts with a valid value.

Try to use smaller functions and reuse them: (I replace x for count)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void CheckArray(int* pointer, const int lengte, const int number)
{
    int count = 0;

    for(int a = 0; a < lengte; a++, pointer++)
    {
        if (*pointer == number)
        {
            count++;
            popfront(pointer, lengte-a); //erase first cell
        }
    }
    fill(pointer-count, pointer, 0); //range [ )
}

Last edited on
You could treat pointer as an array (int * const pointer).
Changing *pointer == number to pointer[a] == number
Can you threat a pointer as an array? :O

And yes, I'm sure I pass a valid pointer...
I did in main:

int arr[lengte] = {1, 2, 3, 6, 2, 2, 3, 4, 8, 5};
int* P_arr = arr;

So it should be pointing to arr[0] right?

If i replace int number with just an integer the programme works...
Last edited on
Can you threat a pointer as an array? :O

And yes, I'm sure I pass a valid pointer...
I did in main:

int arr[lengte] = {1, 2, 3, 6, 2, 2, 3, 4, 8, 5};
int* P_arr = arr;

So it should be pointing to arr[0] right?
yes
Last edited on
It still doesn't work when i change the if statement from a hardcoded integer to a variable...
I'm a newbie myself and am not sure if the following is a good way to do this... I already see how the logic can be improved and additional bounds checking be added, but hope it illustrates my approach well enough.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void CheckArray(int* pointer, const int length)
{
	int number = 4;

	int a = 0;
	int b = 0;
	// Find and compact all numbers found
	while(a < length)
	{
		while (pointer[b] == number)
		{
			++b;
		}

		if (b < length)
		{
			pointer[a] = pointer[b];
			++a;
			++b;
		}
		else
			break;
	}

	// Fill with zeroes
	while (a < length)
	{
		pointer[a] = 0;
		++a;
	}	
}


In your main function, you may use the array pointer directly CheckArray (arr, length). Not sure why you want to declare another pointer int* P_arr.
Topic archived. No new replies allowed.