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.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>
usingnamespace std;
int main()
{
srand(static_cast<unsignedint>(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));
constauto random_number{ rand() % lines.size() };
constauto word{ lines[random_number] };
constauto codes1{ newint[word.length()] {} };
for (size_t i = 0; i < word.length(); ++i)
if (isalpha(static_cast<unsignedchar>(word[i]))) {
codes1[i] = std::tolower(static_cast<unsignedchar>(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 (==).
#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 (unsignedchar ch : word)
if (isalpha(ch))
codes1.push_back(std::tolower(ch) - 'a' + 1);
return codes1;
}
int main()
{
std::srand(static_cast<unsignedint>(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!";
elsereturn (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));
constauto word {lines[rand() % lines.size()]};
constauto codes1 {encrypt(word)};
std::cout << "The clue is: ";
for (constauto& 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';
}
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!