Function return issue

Hello!I have an issue with my result method that underlines red when I return out (in the Token_Taking.cpp). It says "the return value does not match function type.
The print_result method takes an ostream as its only argument and returns void. It first checks whether path is empty. If it is empty, then writes a message. If it is not empty, then it writes all the characters in the vector in order to generate a sequence.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Token_Taking.h
#ifndef TOKEN_TAKING_H
#define TOKEN_TAKING_H

using namespace std;

class Token_Taking {
public:

	Token_Taking(); //Constructor
	
	//Class method
	bool play(unsigned int);
	void result(ostream&) const;

private:

	const unsigned int target;
	unsigned int turns;
	vector<char> pattern; 
						
};
#endif 


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

const unsigned int target = 27;
Token_Taking::Token_Taking(): target(), turns(){
	vector<char> pattern;
}	

bool Token_Taking::play(unsigned int tokens) {

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

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

		pattern.push_back('B');
		tokens = tokens + (tokens / 2);
		turns -= 1;
		return true;
		
	}
}
	

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

	if (!pattern.size()) {
		out << "Error" << std::endl;

	}

	else {

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


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


using namespace std;

int main() {
	ifstream fin("input.txt");
	if (!fin) {
		cout << "Error" << endl;
		system("pause");
		return -1;
	}
	ofstream fout("output.txt");

	unsigned int target, turns;
	Game player;
	while (fin >> target>>turns) {
	
		player.play(turns);
		player.result(fout);
		
	}
	fin.close();
	fout.close();

	system("pause");
	return 0;
}
Last edited on
Game::print_result is a void function; i.e., by definition it returns nothing. Since out is passed by reference you don't need to return it.

You can simply delete the line that you say underlines as red.
Perfect! My program compiled correctly. By any chance, can you help me with my recursive method. I tested it and I got all "Dead End".
The expected sequence was:
AAABBA
Dead End
AAAAAAABBAB
ABABBA
Dead End

I'm not sure what else is missing, can you give me some insight?
Here is the input
75 6
72 2
57 11
57 6
57 5

I can clear you any detail!


You appear to have at least three variables called target - in main, as a global variable in game.cpp, and as a data member of Game.

Which one is used in Game::play I'm not sure - however it certainly won't be the one that you input in main().
Oh, I was not sure on how to initialize the constructor and how to pass the values in there.

In the main(), I named them like that because the first column of the input is the target values and the next column are the turn values.
Topic archived. No new replies allowed.