In: Computer Science
C++ PROJECT
Create a sudoku game that involves the following concepts:
STL library. (Maps, Sets, Lists, Stacks and Queues), with Iterators
and Algorithms
Show especially Especially Algorithm/Iterators/Containers in the
STL
Minimum of 900 lines
Include Full documentation
// File Name: MapQuiz.cpp
#include <iostream>
#include <fstream>
#include <locale>
#include <vector>
#include <map>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function prototypes
void split(const string&, char,
vector<string>&);
void getMappings(map<string, string> &);
void quiz(map<string, string>, int);
// main function definition
int main()
{
// Creates a constant for 5 questions
const int NUM_QUESTIONS = 5;
// Declares a map for capitals
map<string, string> capitals;
// Calls the function to build the map.
getMappings(capitals);
// Calls the function to fun the quiz.
quiz(capitals, NUM_QUESTIONS);
system("pause");
return 0;
}// End of main function
//**************************************************************
// The split function splits s into tokens, using delim as the
delimiter. *
// The tokens are added to the tokens vector. *
// (See Chapter 10 in your textbook for more information about this
function.) *
//**************************************************************
void split(const string& s, char delim,
vector<string>& tokens)
{
// To store the city and capital after split
string state = "", capital = "";
// Finds the delimiter in the string and store the delimiter index
position
int pos = s.find(delim);
// Extract the city from the string s, from zero index position to
the delimiter index position
state = s.substr (0, pos);
// Extract the capital from the string s, from delimiter index
position to the end position of the string s
capital = s.substr(pos + 1);
// Adds the city and capital to the vector
tokens.push_back(state);
tokens.push_back(capital);
}// End of function
//**************************************************************
// The getMappings function reads the states and capitals
// from a file named StateCapitals.txt and stores them as
// key-value pairs in the capitals parameter, that is passed by
reference.
//**************************************************************
void getMappings(map<string, string> &capitals)
{
// ifstream object declared
ifstream rFile;
// Declares a vector
vector<string> tokens;
// To store data read from file
string data;
// Counter for city and capital
int c = 0;
// Opens the file for reading
rFile.open("StateCapitals.txt");
// Check that file can be opened or not
// is_open() function returns true if a file is open and associated
with this stream object.
// Otherwise returns false.
if(!rFile.is_open())
{
// Displays error message
cout<<"\n Error: Unable to open the file! ";
return;
}// End of if condition
// Loops till end of file
while(!rFile.eof())
{
// Reads one line of data from file and stores it in body
getline(rFile, data);
// Calls the function to split the city and capital by the
delimiter ':'
split(data, ':', tokens);
// Adds the city and capital from the vector to map
capitals.insert(pair<string, string>(tokens[c], tokens[c +
1]));
// Increase the counter by two for pair of city and capital
c+=2;
}// End of while loop
}// End of function
//**************************************************************
// The quiz function to play the quiz for 5 questions.
// Generates a random number and ask the capital name for the
random question generated from map.
// Compares the map capital with the state to check for correct
answer.
// Keeps the counter for correct and incorrect answer
//**************************************************************
void quiz(map<string, string> capitals, int
numQuestions)
{
// Generates iterator for map
map<string, string>::iterator it;
// To store the state name entered by the user
string state;
// Lower and higher range of random number
int low = 1, high = 28;
// Counter for random question
int c = 0;
// Counter for question answer
int correct = 0, incorrect = 0;
cout<<"\n I'm going to ask you for the capitals of 5
states."<<endl;
srand((unsigned)time(0));
// Loops till number of questions (5)
for(int x = 0; x < numQuestions; x++)
{
// Generates a random number
int question = low + (rand() % high);
// Iterates till end of map
for ( it = capitals.begin(); it != capitals.end(); it++ )
{
// Increase the counter by one
c++;
// Checks if the question number generated randomly is equals to
the question counter
if(c == question)
// Come out of the loop
break;
}// End of for loop
// Asks the question by taking the state name as the first value of
the iterator
cout<<"\n What is the capital of "<<it->first;
getline(cin, state);
state = " " + state;
// Compares the capital entered by the user and the capital stored
in map second
if(state.compare(it->second) == 0)
{
// Increase the correct counter by one
correct++;
// Display the message
cout<<"Correct"<<endl;
}// End of if condition
// Otherwise wrong
else
{
// Displays the message
cout<<"Sorry, that's incorrect."<<endl;
// Increase the wrong counter by one
incorrect++;
}// End of else
// Reset the question counter to zero for next question
c = 0;
}// End of for loop
// Displays correct and wrong count
cout<<"\n =======================================";
cout<<"\n End of the quiz.";
cout<<"\n Correct answers: "<<correct;
cout<<"\n Incorrect answers: "<<incorrect;
cout<<"\n =======================================\n";
}// End of main function
Sample Output:
I'm going to ask you for the capitals of 5 states.
What is the capital of West Bengal Gangtok
Sorry, that's incorrect.
What is the capital of Rajasthan Jaipur
Correct
What is the capital of Orissa Bhubaneswar
Correct
What is the capital of Punjab Chandigarh
Correct
What is the capital of Uttar Pradesh Dehradun
Sorry, that's incorrect.
=======================================
End of the quiz.
Correct answers: 3
Incorrect answers: 2
=======================================
Press any key to continue . . .
stateCapitals.txt file contents
Andhra Pradesh : Hyderabad
Arunachal Pradesh : Itanagar
Assam : Dispur
Bihar : Patna
Go : Panaji
Gujarat : Gandhinagar
Haryana : Chandigarh
Himachal Pradesh : Shimla
Karnataka : Bangalooru
Kerala : Thiruvananthapuram
Madhya Pradesh : Bhopal
Maharashtra : Mumbai
Manipur : Imphal
Meghalaya : Shillong
Mizoram : Aizawl
Nagaland : Kohima
Orissa : Bhubaneswar
Punjab : Chandigarh
Rajasthan : Jaipur
Sikkim : Gangtok
Tamil Nadu : Chennai
Tripura : Agartala
Uttar Pradesh : Lucknow
West Bengal : Kolkata
Chhattisgarh : Raipur
Uttarakhand : Dehradun
Jharkhand : Ranchi
Telangana : Hyderabad