So this is a homework assignment. It's a variation of the madlib assignment most students seem to have to do. We are not allowed to use String class.
(i don't know what that is yet, I just know most examples I've found online use it, and our instructions say we cant)
Program has to prompt user for a filename, read a file into a character array, seek out tokens <> and either correct the punctuation in the token, or prompt the user for what is in the token (ex. adjective, noun, plural noun.)
I know my program works up until the punctuation function I tried to get it to cout the array after it goes through the punctuation function, but it only have the first line of the array. Also, I've been working on the display function but nothing seems to work. I've tried running the madLib array in a loop and the responseArray in a loop inside the madLib loop but that didn't work....My question is
1. Can someone look over my punctuation function and tell me if there is anything wrong? I've been staring at it for a while and I'm just not seeing it.
2. can someone help me in any way understand how to substitute in user input into the original madLib array and then display it?
Thanks so much for any help you can give!!!!
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
|
#include <fstream>
#include <iostream>
#include <cctype>
using namespace std;
// prototype functions
void getFileName(char fileName[256]);
int readFile(char fileName[256], char madLib[1024]);
void askQuestions(char madLib[1024], char responseArray[][32]);
void getPunctuation(char madLib[1024]);
void display(char madLib[1024], char responseArray[][32]);
/**********************************************************************
* Main calls all the functions and is where program begins.
***********************************************************************/
int main()
{
char fileName[256];
getFileName(fileName);
char madLib[1024];
readFile(fileName, madLib);
char responseArray[256][32];
askQuestions(madLib, responseArray);
getPunctuation(madLib);
display(madLib, responseArray);
char yesOrNo;
bool playAgain = true;
while (playAgain)
{
cout << "Do you want to play again (y/n)? ";
cin >> yesOrNo;
if (yesOrNo == 'n')
{
playAgain = false;
cout << "Thank you for playing." << endl;
}
else if (yesOrNo == 'y')
playAgain = true;
else if (yesOrNo != 'y' || yesOrNo != 'n')
{
cout << "Invalid entry. Do you want to play again (y/n)?";
cin >> yesOrNo;
}
}
}
/**********************************************************************
* getFileName prompts the user for a file name and returns the name
* to main.
********************************************************************/
void getFileName(char fileName[])
{
cout << "Please enter the file name of the Mad Lib: ";
cin >> fileName;
}
/**********************************************************************
*readFile will open the file and save to a new array as it is edited.
*********************************************************************/
int readFile(char fileName[], char madLib[])
{
ifstream fin(fileName);
if (fin.fail())
{
cout << "Error reading file: " << fileName << endl;
return -1;
}
int i = 0;
while(i < 1024 && !fin.eof())
{
fin >> noskipws >> madLib[i++];
}
fin.close();
}
/**************************************************************
* askQuestions will prompt user for necessary input to complete
* mad lib.
*************************************************************/
void askQuestions(char madLib[], char responseArray[][32])
{
cin.ignore();
int found = 0;
for(int k = 0; madLib[k]; k++)
{
if(madLib[k] == '<' && isalpha(madLib[k+1]))
{
k++;
found = 1;
cout << "\t" << (char)toupper(madLib[k]);
}
else if (found == 1)
{
if (madLib[k] == '>')
{
cout << ": ";
cin.getline(responseArray[found], 256);
found = 0;
}
else if (madLib[k] == '_')
{
cout << " ";
}
else
{
cout << madLib[k];
}
}
}
}
/*********************************************************
*getPunctuation will find punctuation tokens and subsitute
*appropriate punctuation in where required.
*************************************************************/
void getPunctuation(char madLib[])
{
int found = 0;
for(int i = 0; madLib[i]; i++)
{
if (madLib[i] == '<' && !isalpha(madLib[i + 1]))
{
found = 1;
switch (madLib[i+1])
{
case '#':
madLib[i] = '\n';
madLib[i+1] = '\0';
break;
case '{':
madLib[i] = ' ';
madLib[i+1] = '\"';
madLib[i+2] = '\0';
break;
case '}':
madLib[i] = '\"';
madLib[i+1] = ' ';
madLib[i+2] = '\0';
break;
case '[':
madLib[i] = ' ';
madLib[i+1] = '\'';
madLib[i+2] = '\0';
break;
case ']':
madLib[i] = '\'';
madLib[i+1] = ' ';
madLib[i+2] = '\0';
break;
}
}
}
}
/***************************************************************
* void display will display the final mad lib
**************************************************************/
void display(char madLib[], char responseArray[][32])
{
|