[ad_1]
How this program should work is that it will ask the user to guess a randomly selected word. The user will do this by placing in letters that could be within that word. The code would randomly select 3 words, and, when the user guesses one of those words right, it will move on to the next word. If the user gets all 3 words right, then the program congratulates them and if they get it wrong then the program just ends. The program will check if the user is trying to put in a letter that they already used, and will stop them from doing so. The program will also check to see if the letter was right/wrong, and, if wrong, will give the user a warning and will increment the number of tries they have.
I have coded out pretty much everything that is needed, but I am running into an error where, after the first word, if the user was to type in one of the letters that were within the second word, the program would say that the word was wrong. For example, let's say the second word was CIA. I would type in the letters I and A into the console and it would accept it, but when I would type in C it would tell me that C isn't in the selected word and will increment me and if I was to try to type in C again it will prevent me from doing so. Tried debugging but I couldn't pinpoint the problem specifically.
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include <string>
#include<vector>
#include<ctime>
#include<algorithm>
#include<cctype>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
string user;
// Display Title of program to user
cout << "Keywords" << endl;
// Ask the recruit to login using thier name
cout << "Log in using your name" << endl;
// Hold the recruit's name in a var, and address them by it throughout the simulation.
cin >> user;
// Display an overview of what Keywords II is to the recruit
cout << " Hello " << user << ". This program is to test out your code breaking skills." << endl;
// Display an directions to the recruit on how to use Keywords
cout << "The program will load a random word, and it is your job to guess what that word is." << endl;
cout << "In order to guess a word, you must guess the letters that are in the word. Use your keyboard to type in the letter that you think is in the word" << endl;
cout << "Every time you guess a right letter, the program will congratulate you. Every time you guess wrong, however, you will lose a turn. When you lose too many turns, the program will end" << endl;
cout << "Remember, the aim is to guess enough right letters to make up the word that was randomly chosen for you." << endl;
// Create a collection of 10 words you had wrote down manually
vector<string>words;
words.push_back("PRESIDENT");
words.push_back("FILES");
words.push_back("INFORMATION");
words.push_back("COUNTRY");
words.push_back("SECURITY");
words.push_back("CIA");
words.push_back("SECRETS");
words.push_back("DATA");
words.push_back("SERVICE");
words.push_back("WANTED");
// Create an int var to count the number of simulations being run starting at 1
int tries = 3;
// Display the simulation # is staring to the recruit.
srand(static_cast<unsigned int>(time(0)));
random_shuffle(words.begin(), words.end());
// Pick new 3 random words from your collection as the secret code word the recruit has to guess.
const string word1 = words[0];
const string word2 = words[1];
const string word3 = words[2];
string soFar1(word1.size(), '-');
string soFar2(word2.size(), '-');
string soFar3(word3.size(), '-');
string used = "";
int game = 1;
// While recruit hasn’t made too many incorrect guesses and hasn’t guessed the secret word
//Start of first word
while ((tries != 0) && (soFar1 != word1) && (game == 1))
// Tell recruit how many incorrect guesses he or she has left
cout << "You have " << tries;
cout << " more tries until the program ends. Keep on at it!" << endl;
// Show recruit the letters he or she has guessed
cout << "The following letters were used:n" << used << endl;
// Show player how much of the secret word he or she has guessed
cout << "Here is the progress you are making with this word:" << soFar1 << endl;
// Get recruit's next guess
char guess;
cout << "Enter the letter that you want to use:";
cin >> guess;
guess = toupper(guess);
//While recruit has entered a letter that he or she has already guessed
while (used.find(guess) != string::npos)
cout << "The letter" << guess << "was already used. Use another letter." << endl;
//Get recruit ’s guess
cin >> guess;
guess = toupper(guess);
//Add the new guess to the group of used letters
used += guess;
//If the guess is in the secret word
if (word1.find(guess) != string::npos)
//Tell the recruit the guess is correct
cout << "The letter you used is in this word ";
// Update the word guessed so far with the new letter
for (int i = 0; i < word1.length(); ++i)
if (word1[i] == guess)
soFar1[i] = guess;
//Otherwise
else
//Tell the recruit the guess is incorrect
cout << "You guessed the wrong letter. " << guess << " is not in the word.n";
// Increment the number of incorrect guesses the recruit has made
--tries;
//End of first word
if (soFar1 == word1)
cout << "Congrats, you got that code right. Here comes the next one." << endl;
used = "";
++game;
//Start of second word
while ((tries != 0) && (soFar2 != word2)&&( game == 2))
// Tell recruit how many incorrect guesses he or she has left
cout << "You have " << tries;
cout << " more tries until the program ends. Keep on at it!" << endl;
// Show recruit the letters he or she has guessed
cout << "The following letters were used:n" << used << endl;
// Show player how much of the secret word he or she has guessed
cout << "Here is the progress you are making with this word:" << soFar2 << endl;
// Get recruit's next guess
char guess;
cout << "Enter the letter that you want to use:";
cin >> guess;
guess = toupper(guess);
//While recruit has entered a letter that he or she has already guessed
while (used.find(guess) != string::npos)
cout << "The letter" << guess << "was already used. Use another letter." << endl;
//Get recruit ’s guess
cin >> guess;
guess = toupper(guess);
//Add the new guess to the group of used letters
used += guess;
//If the guess is in the secret word
if (word1.find(guess) != string::npos)
//Tell the recruit the guess is correct
cout << "The letter you used is in this word ";
// Update the word guessed so far with the new letter
for (int i = 0; i < word2.length(); ++i)
if (word2[i] == guess)
soFar2[i] = guess;
//Otherwise
else
//Tell the recruit the guess is incorrect
cout << "You guessed the wrong letter. " << guess << " is not in the word.n";
// Increment the number of incorrect guesses the recruit has made
--tries;
//End of second word
if (soFar2 == word2)
cout << "Congrats, you got that code right. Here comes the next one." << endl;
used = "";
++game;
//Start of first word
while ((tries != 0) && (soFar3 != word3) && (game == 3))
// Tell recruit how many incorrect guesses he or she has left
cout << "You have " << tries;
cout << " more tries until the program ends. Keep on at it!" << endl;
// Show recruit the letters he or she has guessed
cout << "The following letters were used:n" << used << endl;
// Show player how much of the secret word he or she has guessed
cout << "Here is the progress you are making with this word:" << soFar3 << endl;
// Get recruit's next guess
char guess;
cout << "Enter the letter that you want to use:";
cin >> guess;
guess = toupper(guess);
//While recruit has entered a letter that he or she has already guessed
while (used.find(guess) != string::npos)
cout << "The letter" << guess << "was already used. Use another letter." << endl;
//Get recruit ’s guess
cin >> guess;
guess = toupper(guess);
//Add the new guess to the group of used letters
used += guess;
//If the guess is in the secret word
if (word1.find(guess) != string::npos)
//Tell the recruit the guess is correct
cout << "The letter you used is in this word ";
// Update the word guessed so far with the new letter
for (int i = 0; i < word1.length(); ++i)
if (word3[i] == guess)
soFar3[i] = guess;
//Otherwise
else
//Tell the recruit the guess is incorrect
cout << "You guessed the wrong letter. " << guess << " is not in the word.n";
// Increment the number of incorrect guesses the recruit has made
--tries;
//End of second word
// If the recruit has made too many incorrect guesses
if (tries == 0)
// Tell the recruit that he or she has failed the Keywords II course.
cout << "You have failed. Your director shall hear about this" << endl;
// Otherwise
else
// Congratulate the recruit on guessing the secret words
cout << "You guessed all words right. Congrats!" << endl;
cout << "n The words were: " << word1 << "n" << word2 << "n" << word3 << endl;
// Ask the recruit if they would like to run the simulation again
// If the recruit wants to run the simulation again
// Increment the number of simiulations ran counter
// Move program execution back up to // Display the simulation # is staring to the recruit.
// Otherwise
// Display End of Simulations to the recruit
// Pause the Simulation with press any key to continue
cin >> user;
return 0;
[ad_2]
لینک منبع