Unhandled exception error

i'm doing a token taking game that involves a recursive function, I have some guidelines but I need another insight to figure out what's wrong.



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
//Token_Taking.cpp
#include "Token_Taking.h"


Token_Taking::Token_Taking(unsigned int maxTarget, unsigned int maxTurns): target(maxTarget), turns(maxTurns){}	

//The function only takes one argument which is the tokens
bool Token_Taking::play(unsigned int tokens) { 

	bool Game::play(unsigned int tokens) {

	if (tokens == target) { return true; }

	if (turns == 0 && tokens != target) { return false; }

	if (tokens % 2 == 1) {
		pattern.push_back('A');
		turns = turns - 1;
		if (play(tokens + 39)) { return true; }
		else {
			turns++;
			pattern.pop_back();

		}
	}

	if (tokens % 2 == 0) {
		pattern.push_back('B');
		turns -= 1;

		if (play(tokens / 2)) { return true; }
		
		else {
			turns++;
			pattern.pop_back();

		}
	}

}
	

void Token_Taking::result(ostream& out) const {

	if (!pattern.size()) {out << "You Lost" << std::endl;}

	else {

		for (size_t i = 0; i < pattern.size(); i++) {out << pattern.at(i);}
		out << std::endl;
	}
}

Last edited on
Have you tried using a debugger?
Nope.
But I think my problem is in the constructor. Am I initializing correct the arguments?
Last edited on
1
2
while (fin >> targetval>>turnsval) {
		player.play(targetval); 

Why aren't you using turnsval anywhere?

> Am I initializing correct the arguments?
From the description of what it should take, no.

This seems more like what needs to work.
1
2
3
while (fin >> targetval>>turnsval) {
    Token_Taking player(targetval,turnsval);
    player.play(); 

I tried it and it says that the constructor argument list doesn't match
1
2
Token_Taking player(targetval,turnsval); 
    player.play(); //Here it says that it has too few arguments 

Last edited on
limonconsal wrote:

Nope.
But I think my problem is in the constructor. Am I initializing correct the arguments?


Using a debugger would answer that question, as long as the code compiles.

I tried it and it says that the constructor argument list doesn't match


Post the actual compiler output, not your abbreviated interpretation of it.
Error (active) E0289 no instance of constructor "Token_Taking::Token_Taking" matches the argument list

You declare/define a zero-arg consructor.
Token_Taking(); //Constructor

But you are attempting to use this constructor as if it has two parameters.
Token_Taking player(targetval, turnsval);

If you want to pass those two variables as parameters, then change your constructor.
Token_Taking(int target, int turns);

1
2
3
4
Token_Taking::Token_Taking(int target, int turns)
 : target(target), turns(turns) {
   // (patterns is now an empty vector, already defined in the class.)
}	
Last edited on
Hello, I already fixed it in the program and edited the rest. Looks better but now I'm getting an unhandled exception in the play function.
I got this: Unhandled exception at 0x00D0718A

So far in my output file I got an "A"
Is it my recursive function lacking something?
Last edited on
Assuming that you designed this program and then coded this from the design - then you know how it's supposed to work. As it's not then the first thing to do is to use the debugger to debug the program - trace the execution path, watch the variable contents etc. When this deviates from that expected then you're found a problem - which is either with the coding or the design. Fix that. Then repeat this until the program works as expected.

Debugging is a skill which needs to be acquired and honed. Unfortunately this is very often either not taught or not taught well.
Last edited on
Is it my recursive function lacking something?

Probably. This is where a debugger can help, so you can step through your function and see where it is going awry. In general, recursion is more confusing that iteration, so I tend to avoid recursive functions except for tree-like structures (personally). I would prefer a while loop in this situation.

Lines 25 & 28: You have an if-else chain here. It's either going to enter one branch, or the other, and you have return statements in both branches. Therefore, lines 34 and beyond will never be hit. If there is vital logic within those lines, it will never happen.

I would also add checks before you pop_back to make sure your container has at least 1 element in it.
Last edited on
Alright, I will comment and test each line and see what happens! And no, the code is an homework. I solved the unhandled exception but my logic is still not generating the correct output. I must use recursive (which is very new to me) I will update the code.
Last edited on
Registered users can post here. Sign in or register to post.