Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Skeleton Code: // wscramble.cpp // Word Scramble guessing game // Illustrates st

ID: 3674469 • Letter: S

Question

Skeleton Code:

// wscramble.cpp
// Word Scramble guessing game
// Illustrates string library functions, character arrays,
// arrays of pointers, etc.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

// Scramble the letters of this string randomly
void permute(char items[], int len);

// Define an array of strings (an array of char pointers, like argv)
const char* wordBank[] = {
"computer", "president", "trojan", "program", "coffee",
"library", "football", "popcorn", "science", "engineer"};

const int numWords = 10;

int main() {
srand(time(0));
char guess[80];

bool wordGuessed = false;
int numTurns = 10;

// Pick a random word from the wordBank
int target = rand() % numWords;
int targetLen = strlen(wordBank[target]);

// Make a dynamically-allocated copy of the word and scramble it
char* word = new char[targetLen+1];
strcpy(word, wordBank[target]);
permute(word, targetLen);
  
// An individual game continues until a word
// is guessed correctly or 10 turns have elapsed
while (!wordGuessed && numTurns > 0) {
cout << "Scrambled word: " << word << endl;
cout << "What do you guess the original word is? ";
cin >> guess;
wordGuessed = (strcmp(guess, wordBank[target]) == 0);
numTurns--;
}
if (wordGuessed) {
cout << "You win!" << endl;
}
else {
cout << "Too many turns...You lose!" << endl;
}
delete [] word;
return 0;
}

// Scramble the letters. See "Knuth shuffle" on Wikipedia.
void permute(char items[], int len) {
for (int i = len-1; i > 0; --i) {
int r = rand() % i;
char temp = items[i];
items[i] = items[r];
items[r] = temp;
}
}

4.2 Reading Word Bank from a File Your goal with scramble. cpp will be to alter it by deleting the "built-in" words list, instead using any list of words given in a file whose name the user will provide. For example $ . /scramble wo rdbank. txt # play game using this wo rd list $ . /scramble palabras. txt # play game in Spanish To get this to work, you should do the following. 1. 2. Include the appropriate file stream header file Delete the global wordBank array declaration and numWords declaration

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

// wscramble.cpp
// Word Scramble guessing game
// Illustrates string library functions, character arrays,
// arrays of pointers, etc.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;

// Scramble the letters of this string randomly
void permute(char items[], int len);

int main(int argc, char **argv) {
// check if user has entered enough number of arguments
if (argc < 2) {
   cout << "Error: Not enough number of argument entered!" << endl;
   return 0;
}

// create a stream to read file
ifstream fp(argv[1]);

// check if file is opened
if (!fp.is_open()) {
   cout << "Failed to open file!" << endl;
   return 0;
}

// read number of words to read
int numWords;
if (!(fp >> numWords)) {
   cout << "Error: Failed to read subsequent number of words!" << endl;
   return 0;
}

// allocate memory to array
char **wordBank;
wordBank = new char*[numWords];

// hold string to read in from file
char buffer[41];

for (int i = 0; i < numWords; ++i) {
   // read word from the file
   fp >> buffer;

   // create new array to hold this
   char *newWord = new char[strlen(buffer) + 1];

   // copy content from buffer to new memory location
   strcpy(newWord, buffer);

   // set pointer in wordBank
   wordBank[i] = newWord;
}

// close the file
fp.close();

srand(time(0));
char guess[80];
bool wordGuessed = false;
int numTurns = 10;

// Pick a random word from the wordBank
int target = rand() % numWords;
int targetLen = strlen(wordBank[target]);

// Make a dynamically-allocated copy of the word and scramble it
char* word = new char[targetLen+1];
strcpy(word, wordBank[target]);
permute(word, targetLen);
  
// An individual game continues until a word
// is guessed correctly or 10 turns have elapsed
while (!wordGuessed && numTurns > 0) {
cout << "Scrambled word: " << word << endl;
cout << "What do you guess the original word is? ";
cin >> guess;
wordGuessed = (strcmp(guess, wordBank[target]) == 0);
numTurns--;
}
if (wordGuessed) {
cout << "You win!" << endl;
}
else {
cout << "Too many turns...You lose!" << endl;
}
delete [] word;
return 0;
}
// Scramble the letters. See "Knuth shuffle" on Wikipedia.
void permute(char items[], int len) {
for (int i = len-1; i > 0; --i) {
int r = rand() % i;
char temp = items[i];
items[i] = items[r];
items[r] = temp;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote