So I have finished the UFO project and got it fully working, but I wanted to add a bit more to it and have been working on trying to create a pool of codewords to use from a function vector.
That is what I have but when executed, the program throws up the win condition instantly, and the answer only displays one underscore, but I want the answer to be the size of the codeword length.
I know it is something in the function but I can’t crack it.
Yes so the solutions function should assign a random word from the vector to codeword and then iterate through and generate the number of underscores for that index, so I would only have to change it once in the vector rather than change the number of underscores in the switch statement!
So, what you need is for the solutions function to return 2 values. One to be assigned to codeword, and one to be assigned to answer. That can be a little tricky. There a a few ways to do it. Probably the simplest would be to return an array or vector with 2 values. Another way would be to use a struct. You’ll have to google that to see how to set it up and use it. If you returned an array, you could initialize an empty array of size 2, assign the result of calling the function to the array:
Also your solutions function doesn’t need a switch at all. You can simply assign the codeword by it’s index from your vector of possible codewords, and then use a single for loop to assign the _'s. You probably also want to use += instead of = when assigning _'s, so that you add them on. You only get one _ because you repeatedly do answer = "_";. You are only reassigning the same value of a single _ each time instead of adding an additional one.
I modified your code a bit to make a working version of what you are attempting. I used a struct. If you really, really want to see it, you can go to this repl. I’d strongly suggest you try it on your own first using an array. Feel free to ask additional questions. I may not be able to get to them until tomorrow, but perhaps someone else will chime in.
So I have managed to create the code that does what I want it to do, however, this is in the body of main rather than in a function.
// Creates a vector of random solutions, and creates _'s in answer = to codeword length
std::vector<std::string> codewords{"chicken", "abduction", "cheese", "humans"};
srand(time(NULL));
int random = rand() % codewords.size();
codeword = codewords[random];
for (int i = 0; i < codeword.size(); i++) {
answer += "_";
I have tried several times to place this block of code in a function and then call the function but no matter how I write it I can’t seem to get the function to return the values correctly.
If you were to put codeword and answer in a vector, then return that vector to a vector variable in your main function, you could assign the elements to variables in your main function:
//something like:
std::vector<std::string> gameVals = solutions(); //assuming the code above is in a function called solutions
// the solutions function would have to return a vector such as {"chicken", "_______"}
std::string codeword = gameVals[0];
std::string answer = gameVals[1];
Ok so I have managed to get a working the function working, along with calling it, but I feel like I should be able to streamline it in the function a bit. Just let me know if there is a way to reduce the lines or if this is the only way to make it work
std::vector<std::string> solutions() {
std::vector<std::string> gameval;
std::string codeword, answer;
std::vector<std::string> codewords{"chicken", "abduction", "cheese", "humans"};
srand(time(NULL));
int random = rand() % codewords.size();
codeword = "";
codeword = codewords[random];
for (int i = 0; i < codeword.size(); i++) {
answer += "_";
}
gameval.push_back(codeword);
gameval.push_back(answer);
return gameval;
}
Yeah, you could reduce the number of lines a bit if you wanted to. You could, for example, declare your vector gameval right before you return it, and initialize it on the same line like so:
std::vector<std::string> solutions() {
std::string codeword, answer;
std::vector<std::string> codewords{"chicken", "abduction", "cheese", "humans"};
srand(time(NULL));
int random = rand() % codewords.size();
codeword = codewords[random];
for (int i = 0; i < codeword.size(); i++) {
answer += "_";
}
std::vector<std::string> gameval {codeword, answer};
return gameval;
}