Printing Vector
I am not sure i am getting an error in line 43 under the '<<' before the printing of array guess_list."no operator "<<" matches these operands".
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
|
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
int main() {
string oneWord;
vector<string> word_list,guess_list;
int tries = 0;
string letter_guess;
int length_choice, choice_word_counter = 0,x=0;
cout << "Starting with 40437 words" << endl;
cout << "What length word do you want?" << endl;
ifstream infile;
infile.open("C:/Users/Nate Zegue/documents/dictionary.txt");
if (!(infile.is_open())){
cout << "Unable to open file ditionary.txt" << endl;
}
else {
cout << "" << endl;
}
cin >> length_choice;
while (infile >> oneWord) {
if (oneWord.length() == length_choice) {
word_list.push_back(oneWord);
choice_word_counter = choice_word_counter + 1;
}
x = x + 1;
}
cout << "There are " << choice_word_counter << " words" << endl;
while (tries <= 3) {
cout << "Guess a letter: " << endl;
cin >> letter_guess;
guess_list.push_back(letter_guess);
cout << "Letters used so far: " << guess_list;
for (int i = 0; i <= length_choice; i++) {
cout << " _ " ;
}
cout << endl;
tries = tries + 1;
}
system("PAUSE");
return 0;
}
|
Last edited on
There is no << operator defined for an std::ostream and an std::vector<std::string>.
In other words, it doesn't know how to print a vector.
If you want to be fancy, you could implement your own << operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <string>
#include <vector>
std::ostream& operator<<(std::ostream& os, const std::vector<std::string>& vec)
{
os << "{";
for (size_t i = 0; i < vec.size()-1; i++)
{
os << vec[i] << ", ";
}
os << vec.back() << "}";
return os;
}
int main()
{
std::vector<std::string> v {"tree", "banana", "password123"};
std::cout << v << std::endl;
}
|
Otherwise, just copy and modify lines 7-12 (change os to cout, etc.)
Last edited on
Topic archived. No new replies allowed.