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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
void getFileName(char fileName[])
{
cout << "Please enter the filename of the Mad Lib: ";
cin.getline(fileName, 256);
}
/*************************************************************
*
*
**************************************************************/
int readMadLib(char fileName[], char madLib[][32])
{
ifstream inStream(fileName);
if (!inStream.fail())
{
int i = 0;
while (inStream >> madLib[i])
{
i++;
}
inStream.close();
return i;
}
return 0;
}
/*************************************************************
*
*
**************************************************************/
bool isBrackets(char madLib[][32], int numWords)
{
return (madLib[numWords][0] == '<' &&
madLib[numWords][strlen(madLib[numWords]) - 1] == '>');
}
/*************************************************************
*
*
**************************************************************/
void getMadLibPrompt(char madLib[][32], int i)
{
char prompt[256];
for(int j = 0; j < 32; j++)
{
if (madLib[i][j] == '_')
madLib[i][j] = ' ';
else if (madLib[i][j] == '>')
madLib[i][j] = '\0';
}
madLib[i][1] = toupper(madLib[i][1]);
for(int j = 0;j < 32; j++)
{
prompt[j] = madLib[i][j + 1];
}
cout << " \t";
cout << prompt;
cout << ": ";
cin.getline(madLib[i], 256);
}
/*************************************************************
*
*
**************************************************************/
void commandsAndPrompts(char madLib[][32], int numWords)
{
for (int i = 0; i < (int)numWords; i++)
{
if (isBrackets(madLib, i))
{
switch (madLib[i][1])
{
case '#':
strcpy(madLib[i], "\n");
break;
case '{':
strcpy(madLib[i], " \"");
break;
case '}':
strcpy(madLib[i], "\"");
break;
case '[':
strcpy(madLib[i], " \'");
break;
case ']':
strcpy(madLib[i], "\'");
break;
default:
getMadLibPrompt(madLib, i);
break;
}
}
}
}
/*************************************************************
*
*
**************************************************************/
void setSpaces(char madLib[][32])
{
// vector <char> quoteAndTickPushBack;
for (int i = 0; i < char madLib[][32]; i++)
{
if (madLib[i][0] == '\n');
else if (madLib[i][1] == '"' || madLib[i][1] == '\'')
{
if (madLib[i - 1][0] == '"')
{
quoteAndTickPushBack.clear();
quoteAndTickPushBack.push_back('"');
madLib[i - 1] = quoteAndTickPushBack;
}
else if (madLib[i - 1][0] == '\'')
{
quoteAndTickPushBack.clear();
quoteAndTickPushBack.push_back('\'');
madLib[i - 1] = quoteAndTickPushBack;
}
}
else
{
switch((madLib[i + 1][0]))
{
case '\n':
case '"':
case '!':
case ' ':
case '\'':
case ')':
case ',':
case '.':
case ';':
case '?':
case ']':
case '`':
case '}':
break;
default:
madLib[i].push_back(' ');
}
}
}
}
/*************************************************************
*
*
**************************************************************/
void display(char madLib[][32],int numWords)
{
cout << endl;
for (int i = 0; i < numWords; i++)
cout << madLib[i];
}
/*************************************************************
*
*
**************************************************************/
int main()
{
char ans;
do
{
char madLib[1024][32];
char fileName[256];
getFileName(fileName); //good
int numWords = readMadLib(fileName, madLib); //good
commandsAndPrompts(madLib, numWords);
// setSpaces(madLib);
display(madLib, numWords);
char question[] = "Do you want to play again (y/n)? ";
cout << question;
cin >> ans;
cin.ignore();
}
while (ans == 'y');
cout << "Thanks for playing.\n";
return 0;
}
|