While I can usually manage to complete my coding homework, I have a hard time understanding what parts of my program count as a function and which parts don't. This is important because my homework requires me to add the following to my programs:
All functions other than the main ( ) will have a comment box as defined below:
/*************************************************************************
Purpose: What the function does (not how) and any additional notes
needed to understand and use the function
Inputs: Each non-obvious parameter on a separate line with in-line comments.
Assumes: List of each non-obvious external variable, control, and so on.
Returns: Explanation of return value or result of function
Effects: List of each effected external variable, control, file, and so
on and the affect it has (only if this is not obvious)
*************************************************************************/
Here is a program I'm working on - I would greatly appreciate it if someone could point out to me where (at least a couple spots) this would be placed and what each part of the comment box would be.
My instructor hasn't given any examples or instruction on how or where to use this, so I'm at a loss.
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0)));
//possible words to jumble
const int NUM_WORDS = 5;
// create the array using an initilizer list
const string WORDS[] =
{
"wall",
"glasses",
"labored",
"persistent",
"jumble"
};
//random index number
int choice = (rand() % NUM_WORDS);
//word player must guess
string secretWord = WORDS[choice];
//jumbled version of word
string jumbled = secretWord;
//num characters in jumbled
size_t length = jumbled.size();
//mix up letters in jumbled
for (size_t i = 0; i < length; ++i)
{
//swap letter at index i with letter at random index
size_t randomIndex = (rand() % length); //rand num, 0 thru length - 1
char temp = jumbled[i];
jumbled[i] = jumbled[randomIndex];
jumbled[randomIndex] = temp;
}
//welcome player and explain game
cout << "\t\t\tWelcome to Word Jumble!";
cout << endl << endl;
cout << "Unscramble the letters to make a word.";
cout << endl;
cout << "Enter 'quit' to quit the game.";
cout << endl << endl;
cout << "The jumble is: " << jumbled;
//guessing loop
string guess; //player's guess
do
{
cout << endl << endl << "Your guess: ";
cin >> guess;
if ((guess != secretWord) && (guess != "quit"))
{
cout << "Sorry, that's not it.";
}
} while ((guess != secretWord) && (guess != "quit"));
//do loop can end without correct guess, so check guess
if (guess == secretWord)
cout << endl << "That's it! You guessed it!" << endl;
cout << endl << "Thanks for playing." << endl;
return 0;
}
|