no operator "=" matches these operands

i keep going in circles with this lol. i need to create an if else loop, but the if statement is comparing the users input to a randomised line from a file, I've tried several different combinations but that just generates more errors, and I just keep ending up back at this standard code. any help would be appreciated.

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
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>
using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(nullptr)));

	cout << "the epic quiz\n\n\n";
	cout << "The objective of this round is to unscramble the coded word, you will be given the category as the clue and you have to type out what you belive the answer to be, with a capital letter\n\n";

	string Name;

	cout << "But First, Enter your first name: ";
	getline(cin, Name);

	cout << "Welcome contestant " << Name << ". Are you ready to begin the quiz? ";
	cout << "please type Yes or No (case sensitive): ";

	string respond;

	cin >> respond;
	if (respond == "Yes")
		cout << "\nGood luck!";
	else {
		cout << "Maybe next time!\n";
		return 0;
	}

	cout << "The first category is...\n";
	cout << "Premier League Football Teams!\n";

	vector<string> lines;
	ifstream file("Premier_League_Teams.txt");

	if (!file)
		return (std::cout << "Cannot open file\n"), 1;

	for (string line; getline(file, line); lines.push_back(line));

	const auto random_number{ rand() % lines.size() };
	const auto word{ lines[random_number] };
	const auto codes1{ new int[word.length()] {} };

	for (size_t i = 0; i < word.length(); ++i)
		if (isalpha(static_cast<unsigned char>(word[i]))) {
			codes1[i] = std::tolower(static_cast<unsigned char>(word[i])) - 'a' + 1;
			cout << codes1[i] << ' ';
		}

	delete[] codes1;
	string answer1;
	cout << "\nPlease input your answer: ";
	cin >> answer1;
	
	if (answer1 = codes1);
		cout << "Correct!\n";
	else 
		cout << "Incorrect :(\n";
}


this started as an assignment but now I just want it to work out of matter of principle because i lost marks when i submitted it because it wouldn't work the way i wanted it too lol
Well, depending on the compiler for auto the curly braces are interpreted as initializer list. So instead of using the braces you may use the assignment operator=. For instance on line 47 the operator[] expects an index not an initializer list.

I suggest however using the concrete types you want instead of the vague auto.

On line 61 you have an assignment (=) not a comparison (==).
Possibly (compiles but not tried):

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>

auto encrypt(const std::string& word)
{
	std::vector<int> codes1;

	for (unsigned char ch : word)
		if (isalpha(ch))
			codes1.push_back(std::tolower(ch) - 'a' + 1);

	return codes1;
}

int main()
{
	std::srand(static_cast<unsigned int>(time(nullptr)));

	std::cout << "the epic quiz\n\n\n";
	std::cout << "The objective of this round is to unscramble the coded word, you will be given the category as the clue and you have to type out what you belive the answer to be, with a capital letter\n\n";

	std::string Name;

	std::cout << "But First, Enter your first name: ";
	std::getline(std::cin, Name);

	std::cout << "Welcome contestant " << Name << ". Are you ready to begin the quiz? ";
	std::cout << "please type Yes or No (case sensitive): ";

	std::string respond;

	std::cin >> respond;

	if (respond == "Yes")
		std::cout << "\nGood luck!";
	else
		return (std::cout << "Maybe next time!\n"), 0;

	std::cout << "The first category is...\n";
	std::cout << "Premier League Football Teams!\n";

	std::vector<std::string> lines;
	std::ifstream file("Premier_League_Teams.txt");

	if (!file)
		return (std::cout << "Cannot open file\n"), 1;

	for (std::string line; std::getline(file, line); lines.push_back(line));

	const auto word {lines[rand() % lines.size()]};
	const auto codes1 {encrypt(word)};

	std::cout << "The clue is: ";

	for (const auto& c : codes1)
		std::cout << c << ' ';

	std::string answer1;

	std::cout << "\n\nPlease input your answer: ";
	std::cin >> answer1;

	std::cout << (answer1 == word ? "Correct!" : "Incorrect : (") << '\n';
}


PS Updated.
Last edited on
what does the encrypt function do? im not familiar with this one
and using == instead just gives the same error for some reason
Also on line 61, remove the semicolon. The compiler should warn about this:

61:23: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
63:2: error: 'else' without a previous 'if'


I removed all the other errors to post here.

On line 48 try to avoid using new, just use a std::vector instead, it already puts it's data on the heap.
It's just a re-write of L50 - 54 of the original code.

L13 is the range-for. ch becomes all the characters in word in a loop. See https://en.cppreference.com/w/cpp/language/range-for

L14/15 If ch is a letter than store the encrypted version in the vector.
that was brilliant advice, im down to just two errors now but still not solved my original issue.

now its saying that
"no operator "==" matches these operands" line 61 E0349
and
"binary '==': no operator found which takes a left-handed operand of type 'std::string' (or there is no acceptable conversion)
You're getting this error with the original code as you're comparing std::string with int* - and you can't. Also in the original code you're deleting codes1 and then using it!

Try for L61 in the original code:

 
if (answer1 == word)


Last edited on
that worked perfectly. thank you very much
Topic archived. No new replies allowed.