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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
//* Part 1 and 2 work perfectly fine (to view those, remove the first 2 "if" clauses and main: goto functions. I am having issues with part 3 *//
int main(int argc, char **argv)
{
string word, word2, masked, guess, guessmem;
int i, miss = 6, miss2;
srand (time(NULL));
if (argc == 1) //if user does not specify command line parameter
{
ifstream ifs;
ifs.open("hangmanwords.txt", ios::in);
if (ifs.fail())
{
cerr << argv[0] << ": " << argv[i] << " No such file or directory" << endl;
}
else
{
// Get length of file
ifs.seekg(0, ios::end);
const int length = ifs.tellg();
ifs.seekg(0, ios::beg);
int counter = 0;
int word3 = rand() % length;
while( !ifs.eof() )
{
ifs >> word2;
++counter;
if (counter == word3)
word = word2;
}
}
}
if (argc == 2) //if user specifies command line parameter
{
ifstream ifs;
ifs.open(argv[1], ios::in);
if (ifs.fail())
{
cerr << argv[0] << ": " << argv[i] << " No such file or directory" << endl;
}
else
{
// Get length of file
ifs.seekg(0, ios::end);
const int length = ifs.tellg();
ifs.seekg(0, ios::beg);
int counter = 0;
int word3 = rand() % length;
while( !ifs.eof() )
{
ifs >> word2;
++counter;
if (counter == word3)
word = word2;
}
}
}
cout << word << endl;
//cout <<"Please enter a word" << endl; (for manual input of word
//cin >> word;
masked = word;
//Assigns dashes to each letter
for (i = 0; i < masked.length(); i++)
{
masked[i] = '-';
cout << masked[i];
}
cout << endl << "misses left: " << miss << endl;
//First loop, tracks misses
for (miss = 6; miss > 0; miss--)
{
cout <<"Enter a letter: ";
cin >> guess;
cout << endl;
miss2 = miss;
//loop to check if we've already entered this letter before
for (i = 0; i < 26; i++)
{
if ((guessmem[i] == guess[0]) == 1)
{
cout << guess << " has already been entered";
miss++;
}
else;
guessmem[i] = guess[0];
cout << endl;
break;
}
//loop to "fill in" dashes in masked word
for (i = 0; i < masked.length(); i++)
{
if ((guess[0] == word[i]) == 1)
{
masked[i] = guess[0];
if (miss2 == miss)
miss++;
else;
}
else;
}
guessmem += guess;
//Checks if the entire word has been entered - breaks out of loop if so
if ((word == masked) == 1)
break;
else;
cout << masked << endl;
cout <<"Misses left: " << (miss-1) << endl;
}
if (miss > 0)
cout <<"Congratulations! " << word << " is the word! "<< endl;
else
cout <<"You lose. " << word << " is the word." << endl;
}
|