\"COUT is done twice\" I\'m done programming the first part for my Mad Lib progr
ID: 3675112 • Letter: #
Question
"COUT is done twice"
I'm done programming the first part for my Mad Lib program, but when I run testBed to check my program, I keep getting as if there was two COUTs together when I'm trying to ask a question.
I know the problem is in my switch, but I'm not sure how to fix it.
this is the feed back I'm getting from testBed:
Started program
> Please enter the filename of the Mad Lib: madLibWeb.txt
> Web site name: Automobile Magazine.com
> Verb: Plural noun:
Exp: Verb:
drive
> Plural noun: cars
> Proper noun:
Exp: Plural noun:
motorcycles
> Adjective:
Exp: Proper noun:
New York
> Noun: Noun:
Exp: Adjective:
red
> Boolean operator:
Exp: Noun:
Porsche
> Noun: BMW
> Favorite website:
Exp: Boolean operator:
xor
> Another website:
Exp: Noun:
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_WORDS = 256;
const int MAX_WORD_SIZE = 32;
/**********************************************************************
* The function print the welcoming message and ask for the file
***********************************************************************/
void getFileName(char filename[])
{
cout << "Please enter the filename of the Mad Lib: ";
cin >> filename;
}
/**********************************************************************
* The function substitute the characters for the prompt and prompt
* the user
***********************************************************************/
void prompt(char array[])
{
cout << " " << (char)toupper(array[1]);
for (int i = 2; array[i] != '>'; i++)
{
if (array[i] == '_')
cout << " ";
else
cout << (char)tolower(array[i]);
}
cout << ": ";
cin >> array;
cin.ignore();
}
/**********************************************************************
* The function will open the file to read and call the prompt for
* what words the user will have to put in
***********************************************************************/
int readFile(char filename[], char array[][MAX_WORD_SIZE])
{
ifstream fin(filename);
if (fin.fail())
return -1;
int i = 0;
while (fin >> array[i])
{
if (array[i][0] == '<')
{
switch (array[i][1])
{
case '#':
case '{':
case '}':
case '[':
case ']':
break;
default:
cin.ignore(1, ' ');
cin.ignore();
prompt(array[i]);
}
}
}
fin.close();
return 0;
}
/**********************************************************************
* The function main will call the other functions and print a
* closing message
***********************************************************************/
int main ()
{
char fileName[256];
getFileName(fileName);
char array[MAX_WORDS][MAX_WORD_SIZE];
readFile(fileName, array);
cout << "Thank you for playing. ";
return 0;
}
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_WORDS = 256;
const int MAX_WORD_SIZE = 32;
/**********************************************************************
* The function print the welcoming message and ask for the file
***********************************************************************/
void getFileName(char filename[])
{
cout << "Please enter the filename of the Mad Lib: ";
cin >> filename;
}
/**********************************************************************
* The function substitute the characters for the prompt and prompt
* the user
***********************************************************************/
void prompt(char array[])
{
cout << " " << (char)toupper(array[1]);
for (int i = 2; array[i] != '>'; i++)
{
if (array[i] == '_')
cout << " ";
else
cout << (char)tolower(array[i]);
}
cout << ": ";
cin >> array;
cin.ignore();
}
/**********************************************************************
* The function will open the file to read and call the prompt for
* what words the user will have to put in
***********************************************************************/
int readFile(char filename[], char array[][MAX_WORD_SIZE])
{
ifstream fin(filename);
if (fin.fail())
return -1;
int i = 0;
while (fin >> array[i])
{
if (array[i][0] == '<')
{
switch (array[i][1])
{
case '#':
break;
case '{':
break;
case '}':
break;
case '[':
break;
case ']':
break;
default:
cin.ignore(1, ' ');
cin.ignore();
prompt(array[i]);
}
}
}
fin.close();
return 0;
}
/**********************************************************************
* The function main will call the other functions and print a
* closing message
***********************************************************************/
int main ()
{
char fileName[256];
getFileName(fileName);
char array[MAX_WORDS][MAX_WORD_SIZE];
readFile(fileName, array);
cout << "Thank you for playing. ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.