I created a madLib Program for a class of mine using vectors. I used vectors because the person that was helping me said that it would be easier to use then cstring. I completed the assignment and got credit, but I would have got a better grade by using cstring rather then vectors, due to the fact that we have not covered vectors yet(thanks you understanding teacher).
Im in need of help, this may be a personal project, but I do know that I need a better understanding of cstring.
The program takes a text file and then converts the various symbols to the user input. I stated working on converting vectors to cstring, but I messed up and feel as if Im making things worse. I understand vectors a lot better than I do cstring. I started from the top down. here is a sample .txt file.
I feel like yelling <{> <web_site_name> <}> every time I <verb> the <#>
Internet . So many <plural_noun> to look for , so many <plural_noun>
to find . <#> <#> My mother asked me , <{> <proper_noun> , what is so
<adjective> about <#> the Web ? <}> <{> Well , <}> I said , <{> just
the other day I did a search for <noun> <#> by entering <[> <noun>
<boolean_operator> <noun> <]> in a search <#> engine . And it returned
<favorite_website> and <another_website> . It's <#> just so <adjective>
! <}> <#> <#> And then she knew what I meant . I felt so <emoticon> ! <#>
Im looking for a little guidance, especially in the "commandsAndPrompts()" function. Im getting it slowly but because im new to this, I have a hard time working it all out.
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
#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 wordNum) //int vector level
{
return (madLib[wordNum][0] == '<' &&
madLib[wordNum][strlen(madLib[wordNum]) - 1] == '>');
}
/*************************************************************
*
*
**************************************************************/
void changeFormat(char madLib, char prompt)
{
for (int i = 0; i < madLibPrompt[256]; i++)
{
char currentChar = madLibPrompt[i];
if (i == 0 || i == (int)madLibPrompt.size() - 1);
else if (i == 1)
{
currentChar -= 32;
prompt.push_back(currentChar);
}
else if (currentChar == '_')
prompt.push_back(' ');
else
prompt.push_back(currentChar);
}
}
/*************************************************************
*
*
**************************************************************/
void getMadLibPrompt(vector < char > &madLib)
{
vector < char > prompt;
char answer[256];
changeFormat(madLib, prompt);
cout << " \t";
for (int dis = 0; dis < (int)prompt.size(); dis++) //prompt[]
cout << prompt[dis];
cout << ": ";
cin.getline(answer, 256);
madLib.clear();
for (int i = 0; answer[i] != '\0'; i++)
madLib.push_back(answer[i]);
//next question
//
}
/*************************************************************
*
*
**************************************************************/
void commandsAndPrompts(char madLib[][32])
{
for (int i = 0; i < (int)madLib.size(); i++)
{
if (isBrackets(madLib, i))
{
vector<char> madLibCommandReplace;
switch (madLib[i][1])
{
case '#':
madLibCommandReplace.push_back('\n');
madLib[i] = madLibCommandReplace;
break;
case '{':
madLibCommandReplace.push_back(' ');
madLibCommandReplace.push_back('"');
madLib[i] = madLibCommandReplace;
break;
case '}':
madLibCommandReplace.push_back('"');
madLib[i] = madLibCommandReplace;
break;
case '[':
madLibCommandReplace.push_back(' ');
madLibCommandReplace.push_back('\'');
madLib[i] = madLibCommandReplace;
break;
case ']':
madLibCommandReplace.push_back('\'');
madLib[i] = madLibCommandReplace;
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])
{
cout << endl;
for (int i = 0; i < char madLib[][32]; i++)
for (int dis = 0; dis < char madLib[][32]; dis++)
cout << madLib[i][dis];
}
/*************************************************************
*
**************************************************************/
int main()
{
char ans;
do
{
char madLib[][32];
char fileName[256];
getFileName(fileName); //good
int numWords = readMadLib(fileName, madLib);
commandsAndPrompts(madLib);
setSpaces(madLib);
display(madLib);
char question[] = "Do you want to play again (y/n)? ";
cout << question;
cin >> ans;
}
while (ans == 'y');
cout << "Thanks for playing.\n";
cin.get();
return 0;
}
|