Confusing error in pointer program

Hello, everyone! I'm new here and I'm hoping for some help. I'm in a C++ programming class and my program is giving me trouble. I'd very much appreciate some input on what I'm doing wrong, but i definitely don't want anyone to tell me how to fix it. I'd like to figure it out so I can be ready for the next assignment and the next class in C++, which covers pointers more extensively. I've hit a wall the past couple days trying to figure this issue out.

The program calls for us to remove all occurrences of a search value besides the first, without using any arrays. My professor wants us to wait to move any of the numbers until the second occurrence of the value, which is why I used three loops. My program runs fine until I test over 12 numbers. 13 and higher fails to execute without an error message and I feel like I've tried everything to figure it out. The three functions are the only parts we were supposed to work on.

Pointers are a new concept for me and I'm enjoying it so far, but I feel I need some guidance with this issue. Thanks for your time!

Edit: Problem solved! Thanks for everyone's help!

Last edited on
DId you try to use debugger to see where problem is?

Also, where do you assign size2 in main()?

There is a bunch of stylistic errors and un-C++ approaches here too. And why are you using nothrow new? And without any error checking to boot?
I tried to use the debugger in bloodshed, but couldn't figure out what the issue was with it. main() was written by my professor and we were told not to touch it. My professor told us to use nothrow new in class. I had an if test for errors for it, but I removed it in desperation to solve this issue. nothrow new is a new function to me. I learned it a couple days ago and don't quite understand it entirely.
Last edited on
Main was written by my professor and we were told not to touch it

Bad luck for you, because I was able to recreate problem and cause of it was that size2 was either uninitialized (on first pass) or was less than size1 (on second and later passes if you enter larger number than last time).
Looks like I'll be headed into his office on Monday! I would have gone today, but he's not on campus on Fridays. I appreciate you taking the time to look over it and do what you can to help!
I've hit a wall the past couple days trying to figure this issue out.

I bit of advice: if you hit a wall and can't make progress for 30 minutes, seek help. You'll be much more productive that way and you'll get more sleep :).

Why do you have data1 and data2 in main? Since the assignment is to delete the characters in place, you could have just 1 array.

You'd removeSV code isn't handling the case where the number isn't found at all. Also, you're updating the size as you go. It would be much easier to update the size at the end of the function.

Basically what you want to do is:
- find the first occurence of SV
- find the next occurence of SV
- if you found a second occurence then
starting at the second occurence, walk down the array, copying other values down.
you're doing this part right with a source (i) and destination (data) pointers.
update size
I bit of advice: if you hit a wall and can't make progress for 30 minutes, seek help. You'll be much more productive that way and you'll get more sleep :).

That's great advice! I really enjoy working on problems and I can usually figure it out. I didn't spend the entire two days on the assignment, but a couple hours each night.

Why do you have data1 and data2 in main? Since the assignment is to delete the characters in place, you could have just 1 array.

That part is what the professor provided us with and we were told not to change it. My assignment was to do the two void and one value returning functions. I plan on asking my professor about my issues on Monday.

You'd removeSV code isn't handling the case where the number isn't found at all. Also, you're updating the size as you go. It would be much easier to update the size at the end of the function.

I'm not sure how exactly to update the size at the end of the function without keeping track of it as I go. Honestly, I didn't know there was another way. We don't have much feedback on our weekly programs, so I'm just working with what I know and what we learn in class. If it works, I continue to do it.
Last edited on
I'm not sure how exactly to update the size at the end of the function without keeping track of it as I go.

As you copy the elements, you have a pointer to the next location where you will copy to. Call this dst. At the end, the size of the buffer is dst-start where start is the beginning of the buffer.
Topic archived. No new replies allowed.