C++:thinking: Demo
Demonstration
So for my demonstration I made a small quiz application using C++ in this blog I will explain how I created this small application.
Code
So in this section ill go through segments of the code and explain what is going on in that section.
So for this section of code I create two String vectors one called questions and the other called answers. Vectors are very similar to arrays where they use contiguous storage locations for their elements. But a major difference between vectors and arrays are that vectors can grow dynamically compared to an array.
This section of code is used to fill the vectors I start off by creating a String for question which is named quest and answers which is ans. The ifstream is an input stream that is used to read from the Questions.txt where I have the questions to the quiz written. After reading the file all the lines are run through a loop where each question in recording and inserted into the vector. And the same thing happens for the answers and then we close each input.
This small piece of code is used to pick a random question from Questions.txt
This function is used to create the grading of the quiz to check whether the answers were correct or not. First off we create two int's for the index and one for the correct answers. So we use a for loop which iterates 5 times and with each iteration getIndex() is used which retrieves a random question of the Questions.txt and then we use getline to get the input from the user and then its ran through an if statement to check whether its correct or not if it is correct we increment correct.
And for the final segment we use this to create the interface for the quiz application where we use fillVector() to generate the questions and answers and then we begin outputing the interface for the quiz.
So after writing all the code we need to compile it and you can only do this if you followed my installation instructions in my previous blog because you require mingw64 to actually compile the code. But after you compile the file you'll have a exe file to run and when ran you'll see this window.
To compile you simply navigate to the folder the files are in and type in "g++ quiz.cpp -o quiz" in the command prompt to compile the c++ file.
After running that line of code a exe file called "quiz.exe" and you can either run the exe by opening it or you can type "quiz.exe" in the command prompt to open the exe
And to begin the quiz we push 1
And now we have begun the quiz and the first question is given and the quiz is 5 questions so after we finish we'll return to the menu where we can take the quiz again.
Here is the full code
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include<vector>
#include <string>
#include <cstring>
using namespace std;
vector<string> questions;
vector<string> answers;
void quiz();
int getIndex();
void fillVector();
string getUserIn();
void fillVector(){
string quest;
string ans;
ifstream myquest ("Questions.txt");
if(myquest.is_open()){
while(getline (myquest, quest)){
questions.push_back(quest);
}
}
myquest.close();
ifstream myans("Answers.txt");
if(myans.is_open()){
while (getline(myans,ans)){
answers.push_back(ans);
}
}
myans.close();
}
int getIndex(){
srand (time(NULL));
int index = rand()%20;
return index;
}
void quiz(){
int index = 0;
int correct = 0;
string input;
string asked = "";
for(int i = 0; i< 5; i ++){
cout<< "\nQuestion " << to_string(i +1 ) << ": ";
index = getIndex();
if(asked.find(to_string(index)) != std::string::npos){
while(asked.find(to_string(index)) != std::string::npos){
index = getIndex();
}
}
cout<< questions[index] << '\n';
getline(cin >> ws, input);
if(input.compare(answers[index]) == 0){
correct++;
}
asked+= to_string(index);
}
cout<<"\nYour Results Were: " << to_string(correct) <<"/" << "5\n";
if(correct < 3){
cout<< "\nYikes, are you sure you're a MCU fan?\n\n";
}
else if (correct == 3){
cout<< "\nNot good, but not bad either!\n\n";
}
else if(correct == 4){
cout<<"\nYou know your stuff! You're a real fan:)\n\n";
}
else if(correct == 5){
cout<<"\nYou're a true believer... Touch grass though please.\n\n";
}
}
int main(){
fillVector();
while(1){
cout<< "Welcome to the MCU (Marvel Cinematic Universe) Quiz Game!\n\n";
cout<<"1. Start\n";
cout<<"2. Quit\n";
string input;
cin >> input;
if(input == "1"){
quiz();
}
else if(input == "2"){
exit(1);
}
else{
cout<< "Hmmm... that input is invalid, try again!\n";
}
}
return 0;
}







Comments
Post a Comment