Write a C program that implements a simple TVmovie trivia ga
Solution
Please edit the questions and answers. Not all of them are Sci-Fi.
Program code:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
//varaibles to hold the questions and answers
string questions[10];
string answers[10];
//This function will add the questions and answers intot he array
void setQuestionsAndAnswers()
{
questions[0] = \"In what U.S. state is warehouse 13 located? \";
answers[0] = \"south dakota\";
questions[1] = \"Who played the character Hermoine Granger in Harry Potter series?\";
answers[1] = \"emma watson\";
questions[2] = \"What is Orlando Bloom\'s character\'s name in Pirates of the Caribbean series?\";
answers[2] = \"william turner\";
questions[3] = \"In what movie did Julia Roberts play an unmarried woman who could not commit to one man ?\";
answers[3] = \"runaway bride\";
questions[4] = \"In \\\"Legends of the Fall\\\" which actor plays the elder brother?\";
answers[4] = \"aidan quinn\";
questions[5] = \"Who plays the main character in Hitch?\";
answers[5] = \"will smith\";
questions[6] = \"What is the name of Harry Potter\'s owl?\";
answers[6] = \"hedwig\";
questions[7] = \"Who stars alongside Jackie Chan in Rush Hour? \";
answers[7] = \"chris tucker\";
questions[8] = \"Which actor voiced Smaug, the Dragon in Hobbit series?\";
answers[8] = \"benedict cumberbatch\";
questions[9] = \"What is the name of Tony Stark\'s girl friend? \";
answers[9] = \"pepper pots\";
}
int main()
{
char isReady;
cout<<endl<<\"Welcome to the Sci-Fi Tv and Movie Trivia Game!\"<<endl<<endl<<\"Ready to play (Y/N)? \";
cin>>isReady;
cout<<endl<<\"--------------------------------------------------------------------------\"<<endl;
//checking if the user is ready
if(isReady == \'Y\' || isReady == \'y\')
{
//calling the function to add questions and answers into the array
setQuestionsAndAnswers();
int i=0;
char choice;
do
{
cin.ignore();
string answer = \"\";
cout<<endl<<questions[i]<<endl;
getline(cin,answer);
//converting all the characters of the user entered answer to lower case
char s[answer.size()];
strcpy(s,answer.c_str());
for(int j=0;s[j]!=\'\\0\';j++)
{
s[j]=tolower(s[j]);
}
answer = string(s);
//comparing the answer provided by the user with the answer in the array
if(answer.compare(answers[i]) == 0)
{
cout<<endl<<\"You are correct!\"<<endl<<endl<<\"Play again (Y/N)? \";
cin>>choice;
cout<<endl<<\"--------------------------------------------------------------------------\"<<endl;
i++;
}
else
{
cout<<endl<<\"Nice try but no\"<<endl<<endl<<\"Play again (Y/N)? \"<<endl;
cin>>choice;
cout<<endl<<\"--------------------------------------------------------------------------\"<<endl;
i++;
}
}while(choice ==\'y\' || choice ==\'Y\');
cout<<endl<<\"Thanks for playing the Sci-Fi Tv and Movie Trivia Game!\"<<endl;
cout<<\"Come again soon\"<<endl;
cout<<endl<<\"--------------------------------------------------------------------------\"<<endl;
}
else
{
cout<<endl<<\"May be another time...\"<<endl;
cout<<endl<<\"--------------------------------------------------------------------------\"<<endl;
cout<<endl<<\"Press any key to continue...\";
}
return 0;
}
OUTPUT:
Run #1
Welcome to the Sci-Fi Tv and Movie Trivia Game!
Ready to play (Y/N)? y
--------------------------------------------------------------------------
In what U.S. state is warehouse 13 located?
south dakota
You are correct!
Play again (Y/N)? y
--------------------------------------------------------------------------
Who played the character Hermoine Granger in Harry Potter series?
emma watson
You are correct!
Play again (Y/N)? n
--------------------------------------------------------------------------
Thanks for playing the Sci-Fi Tv and Movie Trivia Game!
Come again soon
--------------------------------------------------------------------------
Run #2
Welcome to the Sci-Fi Tv and Movie Trivia Game!
Ready to play (Y/N)? n
--------------------------------------------------------------------------
May be another time...
--------------------------------------------------------------------------
Press any key to continue...
Run #3
Welcome to the Sci-Fi Tv and Movie Trivia Game!
Ready to play (Y/N)? y
--------------------------------------------------------------------------
In what U.S. state is warehouse 13 located?
south dakota
You are correct!
Play again (Y/N)? n
--------------------------------------------------------------------------
Thanks for playing the Sci-Fi Tv and Movie Trivia Game!
Come again soon
--------------------------------------------------------------------------
Run #4
Welcome to the Sci-Fi Tv and Movie Trivia Game!
Ready to play (Y/N)? y
--------------------------------------------------------------------------
In what U.S. state is warehouse 13 located?
South Dakota
You are correct!
Play again (Y/N)? n
--------------------------------------------------------------------------
Thanks for playing the Sci-Fi Tv and Movie Trivia Game!
Come again soon
--------------------------------------------------------------------------
Alternative solution with algorithm header
Program Code:
/*
* MovieGame.cpp
*
* Created on: 09-Nov-2016
* Author: Kaju
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//varaibles to hold the questions and answers
string questions[10];
string answers[10];
//This function will add the questions and answers intot he array
void setQuestionsAndAnswers()
{
questions[0] = \"In what U.S. state is warehouse 13 located? \";
answers[0] = \"south dakota\";
questions[1] = \"Who played the character Hermoine Granger in Harry Potter series?\";
answers[1] = \"emma watson\";
questions[2] = \"What is Orlando Bloom\'s character\'s name in Pirates of the Caribbean series?\";
answers[2] = \"william turner\";
questions[3] = \"In what movie did Julia Roberts play an unmarried woman who could not commit to one man ?\";
answers[3] = \"runaway bride\";
questions[4] = \"In \\\"Legends of the Fall\\\" which actor plays the elder brother?\";
answers[4] = \"aidan quinn\";
questions[5] = \"Who plays the main character in Hitch?\";
answers[5] = \"will smith\";
questions[6] = \"What is the name of Harry Potter\'s owl?\";
answers[6] = \"hedwig\";
questions[7] = \"Who stars alongside Jackie Chan in Rush Hour? \";
answers[7] = \"chris tucker\";
questions[8] = \"Which actor voiced Smaug, the Dragon in Hobbit series?\";
answers[8] = \"benedict cumberbatch\";
questions[9] = \"What is the name of Tony Stark\'s girl friend? \";
answers[9] = \"pepper pots\";
}
int main()
{
char isReady;
cout<<endl<<\"Welcome to the Sci-Fi Tv and Movie Trivia Game!\"<<endl<<endl<<\"Ready to play (Y/N)? \";
cin>>isReady;
cout<<endl<<\"--------------------------------------------------------------------------\"<<endl;
//checking if the user is ready
if(isReady == \'Y\' || isReady == \'y\')
{
//calling the function to add questions and answers into the array
setQuestionsAndAnswers();
int i=0;
char choice;
do
{
cin.ignore();
string answer = \"\";
cout<<endl<<questions[i]<<endl;
getline(cin,answer);
//converting all the characters of the user entered answer to lower case
transform(answer.begin(), answer.end(), answer.begin(), ::tolower);
//comparing the answer provided by the user with the answer in the array
if(answer.compare(answers[i]) == 0)
{
cout<<endl<<\"You are correct!\"<<endl<<endl<<\"Play again (Y/N)? \";
cin>>choice;
cout<<endl<<\"--------------------------------------------------------------------------\"<<endl;
i++;
}
else
{
cout<<endl<<\"Nice try but no\"<<endl<<endl<<\"Play again (Y/N)? \"<<endl;
cin>>choice;
cout<<endl<<\"--------------------------------------------------------------------------\"<<endl;
i++;
}
}while(choice ==\'y\' || choice ==\'Y\');
cout<<endl<<\"Thanks for playing the Sci-Fi Tv and Movie Trivia Game!\"<<endl;
cout<<\"Come again soon\"<<endl;
cout<<endl<<\"--------------------------------------------------------------------------\"<<endl;
}
else
{
cout<<endl<<\"May be another time...\"<<endl;
cout<<endl<<\"--------------------------------------------------------------------------\"<<endl;
cout<<endl<<\"Press any key to continue...\";
}
return 0;
}






