crashes or doesn't work as intended

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.

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
  void addToPiles(string piles[], int &lastocc, string card)
{
	if (lastocc==-1)
	{
		piles[0]=card;
		lastocc=0;
    }
    else
    { 
	int top;
	int i=0;
	while (i<=lastocc)
	{
		cout<<piles[i]<<endl;
		top=getTopCard(piles[i]);
		cout<<top<<endl;
		//exit (0);
		if (convertToValue(card)<=top)
		{
			cout<<piles[i]<<endl;
			piles[i]=piles[i]+" "+card;
			cout<<piles[i]<<"done"<<endl;
			//exit (0);
		}
		else if(piles[i+1].length()==0)
		{
			piles[i+1]=card;
			lastocc++;
		}
		i++;
	}
}
}
Last edited on
How long is the array piles? Why not a std::vector<std::string> ?

Why the commented exit(0) ?
i was checking where the loop messes up. it messes up if it goes to else. and the array is 52 strings long.
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
}
Last edited on
1
2
3
4
while (i<=lastocc and lastocc<=51)
{
//same code
}

I added this second criteria for the loop but If I remove the <= in i<=lastocc the loop will not start since i and lastocc are equal to zero.
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.
Last edited on
here is my main function
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
int main()
{
	string s;
	string x;
	int i=-1;
	int num=0;
	string piles[52];
	x=getUserInput(x);
    ifstream myfile;
    myfile.open(x.c_str());
    if (!myfile)
		errorMsg(x,true);
	else
	{
		myfile>>s;
		while (myfile)
		{
			addToPiles(piles, i, s);
			myfile>>s;
		}
	}
	cout<<"Resulting piles:"<<endl;
	while (piles[num].length()>0)
	{
		
		cout<<"   Pile  "<<num<<":  "<<piles[num]<<endl;
		num++;
	}
}
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;
Last edited on
I can't since it crashes as soon as enter a file in.
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.
Last edited on
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).
Last edited on
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.
Last edited on
Thank you so much. I followed your advise and managed to find all the small errors and fix them.
Topic archived. No new replies allowed.