I have had this this assignment and I just don't know what is wrong with this function. Every other function works as intended. this Function should compare a card from a string to the last card in a each string in an array called piles and if the it is equal or smaller it adds it the end but the piles are empty until we add a card to it.
I can't run it, but in your while loop, print out the value of i each time, and see whether or not it goes to 51.
if i == 51, it's probably an error because piles[i+1] === piles[52], which is out of bounds.
If this is in fact the problem, you could try preventing i+1 from being out of bounds by only iterating up to lastocc - 1.
1 2 3 4
while(i < lastocc) // notice: < instead of <=
{
// same code here
}
I said to change the while loop condition only if it turned out i was hitting 51 at some point inside the loop. Is it? I still don't know whether it is.
I would use an actual debugger and set break points at lines in the function that you want to check if they're actually being traversed during runtime.
If you don't have a debugger, then put print statements (ex, cout << "hi! i = " << i << endl;) at different places in your code.
Have a mental image of what you think the code *should* be doing at each line, vs. what it's actually doing.
And show us the setup to how you are calling the function.
Did you try using a debugger or print statements yet?
Also, the string you're passing in, s, is an empty string. Is that intentional? Is piles[0] supposed to be set to an empty string? Nevermind I didn't see the cin >> s;
Have you actually tried? You should still be able to see print statements if it crashes. If not, put a pause right after each print statement. If you still aren't seeing any print statements, add print statements that happen earlier in the program. (You should be using a debugger, but w/e).
The point of is for you to visually see what is happening in your program, and know exactly where something goes wrong.
if (convertToValue("")<=top) always returns false, you will be in an endless loop with lastocc and i always equaling each other.
i didn't know the pause command, had to google it just now. your prediction was correct after the a new pile is made it just keeps looping with lastocc and i equaling each other. It never checks the next card. and it also checks every card twice for some reason ( i think this causing the issue since it checks the highest cards infinite times).
Okay good, so now you see where the error is happening. So now the problem is reworking your logic to prevent that.
When testing your code, you should always start small and build up from there.
I assume each token in your file is a card. Don't start with 52 cards in your file, that will be too complicated to debug.
Start small: what should happen if the file is just one card, say, King? What happens when the file has two cards, King, then Queen? What happens when it's Queen, then King?
What should the array look like after each card insertion, and what does the array actually look like?
For each test, know what you expect the output to be. In this case, that would be what your cards array looks like after all cards have been added.