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;
}
}
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.