What am I doing wrong here for the exersise: \"Write a program that plays the ga
ID: 3528923 • Letter: W
Question
What am I doing wrong here for the exersise: "Write a program that plays the game of Mad Lib. Your program should prompt the user to enter the following strings. Instructor name Your name A food a number 100-120 an adjective a color an animal -After the strings are input, they should be substituted into the story below.... Here is what I'm working on. #include #include #include using namespace std; int main() { string instructor,name,food,number,adjective,color,animal; string word, prompt; char space; ifstream in; int n; cout<<"Jie Chen:"; cin>>instructor; cout<<"Jeff Reinmann:"; cin>>name; cout<<"Enter a food:"; cin>>food; cout<<"Enter a number between 100 and 120:"; cin>>number; cout<<"Enter an adjective:"; cin>>adjective; cout<<"Enter a color:"; cin>>color; cout<<"Enter an animal:"; cin>>animal; in.open("Story.docx"); if(in.fail()) {cout<<"story file did not open please check again "; system("pause"); return 1; } in>>word; while(in) {if(word[0]=='[') {n=word.find(']',0); if(n>word; prompt=prompt+word.substr(0,word.length()-1); } if(prompt.compare("Instructor Name")==0 cout<<instructor; else if(prompt,compare("food")==0) cout<<food; else if(prompt.compare("color")==0) cout<<color; else if(prompt.compare("adjective")==0) cout<<adjective; else if(prompt.compare("animal")==0) cout<<animal; else if(prompt.compare("Your name")==0) cout<<name; else if(prompt.compare("number")==0) cout<<number; } else cout<<word; in.get(space); cout<<space; in>>word; } cout<<endl; system("pause"); return 0; } The errors are reported as : warning C4390: ';' : empty controlled statement found; is this the intent? warning C4018: '<' : signed/unsigned mismatch error C2146: syntax error : missing ')' before identifier 'cout' error C3861: 'compare': identifier not found error C2059: syntax error : '}' error C2143: syntax error : missing ';' before '}' error C2059: syntax error : '}'Explanation / Answer
From what you're saying it should be a very basic type of program.
Though I know how frustrating it can be to get started in programming so I'll help you out.
The game of Mad Libs revolves around taking in different words and then displaying them inside a story.
For example, asking someone for a noun, then saying "One day, a wizard turned you into a " + noun.
In this sense, it's fairly simple to implement in any programming language.
What you're going to do is declare the variables of what words you want the user to input. I assume you know about declaring variables. You're taking in words so you'll want to declare either char* variables or std::string variables. Either one can work just fine.
Next you'll want to input the variables you just declared. I assume in your intro class you've already covered std::cin. It is the standard source of command line input for C++ programming. But before each you'll need a prompt right? So for that I assume you know std::cout. For example:
cout << "Enter a noun:";
cin >> noun1;
It's as easy as that to get your variables declared.
Then the next step is just to display the variables along with the story you have, using cout yet again. Very simple.
Then to ask if they want to "play again", print a prompt with cout and input a variable. Then you'll need to use an IF statement to determine if they want to play again. This part is more advanced than just inputting and printing text, as you'll probably need to use a while loop to make the process start over again.
Programming is all about taking each step of a problem slowly, one by one. You just have to think about the flow of what you're trying to make, and put it in a logical order how I did above.
I'll help you out with an example that I'll type simple enough for a beginner to understand:
#include <iostream>
using namespace std;
int main(){
bool playagain = true;
while(playagain == true){
char* noun;
char* adjective;
cout << "Enter a noun:";
cin >> noun;
cout << "Enter an adjective:";
cin >> adjective;
cout << "You're not very " << adjective << " if you like to play with " << noun << "s." << endl;
char playagainchar;
cout << "Play again? (y or n): ";
cin >>playagainchar;
if(playagainchar != 'y') playagain = false;
}
}
cheers....:)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.