Nov 25, 2018 at 7:35pm UTC
I am new at programming and i'm having a hard time with this. in the void function begin it says there is an error
" 68:69: error: reference to type 'const std::__1::__vector_base, std::__1::allocator > >::value_type' (aka 'const std::__1::basic_string') could not bind to an lvalue of type 'int' for (int i=0; i < number_of_animals; i++) {chosen_animals.push_back(i)}; ^"
SOMEONE PLEASE HELP, IT'S URGENT. I submit it tonight.
#include <iostream>
#include <cstdlib>
#include <random>
#include <string>
#include <vector>
#include <sstream>
#include <chrono>
#include <ctime>
#include <set>
using namespace std;
//Global Variables used.
vector<string>animals;
int passes=0;
int fails=0;
string scrambled_words;
mt19937 prng( chrono::system_clock::now().time_since_epoch().count() );
int number_of_animals;
vector <string> chosen_animals;
vector <string> answers;
//Score Functions
void pass()
{
fails = 0;
passes += 1;
cout << "Correct!\n";
}
void fail()
{
passes = 0;
fails += 1;
cout << "Incorrect!\n";
}
//Function that decides level, this is an algorithm.
int dash_level()
{
return passes / 2;
}
//Functions that are used to begin game
void get_input()
{
string input;
cout<<"Type Quit to End Game."<<"Otherwise, Please Enter Animals:" <<endl;
getline(cin,input);
animals.push_back(input);
}
void begin()
{
chosen_animals.clear();
answers.clear();
shuffle( animals.begin(), animals.end(), prng );
int number_of_animals = uniform_int_distribution <int> ( 1, 3 )( prng );
for (int i=0; i< number_of_animals; ++i) chosen_animals.push_back(i);
shuffle(chosen_animals.begin(), chosen_animals.end(), prng);
for (int j=0; j < chosen_animals.size(); j++) scrambled_words.push_back(j);
}
//The Actual Game Function
void game()
{
begin();
cout << "\nWhat are " << number_of_animals << " animals in \"" << scrambled_words << "\"? ";
for (int n = 0; n < number_of_animals; n++)
{
string answer;
cin >> answer;
if (!cin||answer=="Quit") return;
answers.push_back( answer );
}
if (answers == chosen_animals) pass();
else fail();
}
//The Game
int main()
{
prng.discard( 10000 );
get_input();
while (cin) game();
}
Nov 25, 2018 at 7:52pm UTC
for (int i=0; i< number_of_animals; ++i) chosen_animals.push_back(i);
Why do you want to add an int to a vector of strings?
Nov 25, 2018 at 11:22pm UTC
And why all the horrible global variables?