A Console-Based
Multiple Choice Question


Programming a Multiple-Choice Question

 

Practical Learning: Introduction to Exceptions

  1. Create a new Console Application in a new folder named BibleQuiz
  2. Create a new C++ source file named Exercise
  3. Change the content of the file with the following:
     
    #include <algorithm>
    
    #include <vector>
    
    #include <functional>
    
    #include <iostream>
    
    using namespace std;
    
    #include <time.h>
    
    
    
    const int NbrOfQuestions = 22;
    
    int AskQuestion(int q, double &m);
    
    void Verify(int);
    
    
    
    int main(int argc, char* argv[])
    
    {
    
     	int i, n;
    
    	double Average, Sum = 0;
    
    	typedef vector<int> IntVector;
    
    	typedef IntVector::iterator IntVectorIt;
    
    
    
    	IntVector QuestNbr(NbrOfQuestions);
    
    	IntVectorIt Start, End;
    
    
    
    	for(i = 0; i < NbrOfQuestions; i++)
    
    		QuestNbr[i] = i;
    
    
    
    	Start = QuestNbr.begin();
    
    	End = QuestNbr.end();
    
    
    
    	srand(time(0));
    
        random_shuffle(Start, End);
    
    
    
    	cout << " - Multiple Choice Question -\n\n";
    
    	do {
    
    		cout << "How many questions (1-20)? ";
    
    		cin >> n;
    
    	} while(n < 1 || n > 20);
    
    
    
        system("cls");
    
    
    
    	for(i = 0; i < n; i++)
    
    	{
    
            system("cls");
    
    		cout << "Question " << i + 1 << ": ";
    
    		AskQuestion(QuestNbr[i], Sum);
    
            getchar();
    
    	}
    
    
    
        system("cls");
    
    	Average = (Sum / n) * 100;
    
    	cout << "Answers Overview\n";
    
    	cout << "Your answers were";
    
    
    
    	cout << "\nGood Answers: " << Sum;
    
    	cout << "\nAverage:      " << Average<< "%";
    
    
    
    	cout << "\n\n";
    
        return 0;
    
    }
    
    
    
    int AskQuestion(int q, double &m)
    
    {
    
    	char Answer[10];
    
    
    
    	switch(q)
    
    	{
    
    	case 0:
    
    		cout << "\nWho was Jesus Christ\'s mother ? ";
    
    		cout << "\n1. Leah"
    
    			 << "\n2. Marthe"
    
    			 << "\n3. Mary"
    
    			 << "\n4. salome"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '3')
    
    		{
    
    			cout << "\nGood Answer";
    
    			cout << "\nRef: Lk 1:26-31";
    
    			m += 1;
    
    		}
    
    		else
    
            {
    
                cout << "\nWrong Answer!";
    
    			Verify(0);
    
            }
    
    		break;
    
    
    
    	case 1:
    
    		cout << "\nHow many books are in the canonical Bible?";
    
    		cout << "\n1. 33"
    
    			 << "\n2. 44"
    
    			 << "\n3. 55"
    
    			 << "\n4. 66"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '4')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(1);
    
            }
    
    		break;
    
    
    
    	case 2:
    
    		cout << "\nWhat is the first book of the Bible ?";
    
    		cout << "\n1. Malachi"
    
    			 << "\n2. Isaiah"
    
    			 << "\n3. Revelation"
    
    			 << "\n4. Genesis"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '4')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(2);
    
            }
    
    		break;
    
    
    
    	case 3:
    
    		cout << "\nWhich one of Jacob\'s sons was Joseph\'s "
    
                 << "brother, his own mother\'s son ?";
    
    		cout << "\n1. Levi"
    
    			 << "\n2. Judah"
    
    			 << "\n3. Asher"
    
    			 << "\n4. Benjamin"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '4')
    
    		{
    
    			cout << "\nGood Answer";
    
    			cout << "\nRef: Ge 43:29";
    
    			m += 1;
    
    		}
    
    		else
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(3);
    
            }
    
    		break;
    
    
    
    	case 4:
    
    		cout << "\nWhat happened to Ananias and Sapphira after they lied "
    
                 << "about the money\nthey collected from selling the land ?";
    
    		cout << "\n1. They died"
    
    			 << "\n2. They got sick"
    
    			 << "\n3. They were abandoned by the other discples in the bush"
    
    			 << "\n4. They were drowned in the sea of Galilee"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '1')
    
    		{
    
    			cout << "\nGood Answer";
    
    			cout << "\nRef: Ac 5:1-11";
    
    			m += 1;
    
    		}
    
    		else          
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(4);
    
            }
    
    		break;
    
    
    
    	case 5:
    
    		cout << "\nHow many apostles did Jesus Christ have?";
    
    		cout << "\n1. 3"
    
    			 << "\n2. 6"
    
    			 << "\n3. 9"
    
    			 << "\n4. 12"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '4')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else            
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(5);
    
            }
    
    		break;
    
    
    
    	case 6:
    
    		cout << "\nWhat was the craftiest of the wild animals "
    
                 << "the Lord had made?";
    
    		cout << "\n1. The dragon"
    
    			 << "\n2. The serpent"
    
    			 << "\n3. The lion"
    
    			 << "\n4. The Dove"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '2')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else        
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(6);
    
            }
    
    		break;
    
    
    
    	case 7:
    
    		cout << "\nTo which disciple did Jesus promise the key\n"
    
                 << "of the kingdom of heaven ?";
    
    		cout << "\n1. John"
    
    			 << "\n2. Peter"
    
    			 << "\n3. Philip"
    
    			 << "\n4. Paul"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '2')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else        
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(7);
    
            }
    
    		break;
    
    
    
    	case 8:
    
    		cout << "\nHow does Paul address Timothy in 1 Timothy?";
    
    		cout << "\n1. My disciple"
    
    			 << "\n2. My brother"
    
    			 << "\n3. My son"
    
    			 << "\n4. My father"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '3')
    
    		{
    
    			cout << "\nGood Answer";
    
    			m += 1;
    
    		}
    
    		else              
    
            {
    
    			Verify(8);
    
                cout << "\nWrong Answer!";
    
            }
    
            cout << endl;
    
    		break;
    
    
    
    	case 9:
    
    		cout << "\nWho was the father of the 12 patriarchs?";
    
    		cout << "\n1. Noah"
    
    			 << "\n2. Abraham"
    
    			 << "\n3. Isaac"
    
    			 << "\n4. Jacob"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '4')
    
    		{
    
    			cout << "\nGood Answer";
    
    			m += 1;
    
    		}
    
    		else             
    
            {
    
                cout << "\nWrong Answer!";
    
    			Verify(9);
    
    
    
            }
    
            cout << endl;
    
    		break;
    
    
    
    	case 10:
    
    		cout << "\nWho was the firstborn son of Adam and Eve?";
    
    		cout << "\n1. Abel"
    
    			 << "\n2. Cain"
    
    			 << "\n3. Shem"
    
    			 << "\n4. Japhet"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '2')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else             
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(10);
    
            }
    
            cout << endl;
    
    		break;
    
    
    
    	case 11:
    
    		cout << "\nTo whom did James address his book?";
    
    		cout << "\n1. The 12 tribes"
    
    			 << "\n2. To those who have received faith"
    
    			 << "\n3. To the believers"
    
    			 << "\n4. To Epaphroditus"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '1')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else                
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(11);
    
            }
    
            cout << endl;
    
    		break;
    
    
    
    	case 12:
    
    		cout << "\nWhat did Micah from the hill country of Ephraim "
    
                 << "do of the\nyoung Levite who came to stay with him?";
    
    		cout << "\n1. He taught him to raise the sheep"
    
    			 << "\n2. He made him a priest"
    
    			 << "\n3. He killed him"
    
    			 << "\n4. He sent him away"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '2')
    
    		{
    
    			cout << "\nGood Answer\n";
    
    			m += 1;
    
    		}
    
    		else               
    
            {
    
                cout << "\nWrong Answer!";
    
    			Verify(12);
    
            }
    
            cout << endl;
    
    		break;
    
    
    
    	case 13: 
    
    		cout << "\nThe account of Noah starts on";
    
    		cout << "\n1. Ge 5:6"
    
    		     << "\n2. Ge 6:9"
    
    		     << "\n3. Ge 8:15"
    
    		     << "\n4. Ge 9:18"
    
    		     << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '2')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else            
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(13);
    
            }
    
    		break;
    
    
    
    	case 14:
    
    		cout << "\nIn the parable of the weed, what does the field represent?";
    
    		cout << "\n1. The Ocean"
    
    			 << "\n2. The world"
    
    			 << "\n3. The kingdom"
    
    			 << "\n4. The Earth"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '2')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else                
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(14);
    
            }
    
    		break;
    
    
    
    	case 15:
    
    		cout << "\nHow many people (including Noah) got in the ark he built?";
    
    		cout << "\n1. 6"
    
    			 << "\n2. 7"
    
    			 << "\n3. 8"
    
    			 << "\n4. 9"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '2')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else                
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(15);
    
            }
    
    		break;
    
    
    
    	case 16:
    
    		cout << "\nWho was John the Baptist\'s mother?";
    
    		cout << "\n1. Salome"
    
    			 << "\n2. mary"
    
    			 << "\n3. Elizabeth"
    
    			 << "\n4. Rebecah"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '3')
    
    		{
    
    			cout << "Good Answer\n";
    
    			m += 1;
    
    		}
    
    		else        
    
            {
    
                cout << "Wrong Answer!";
    
    			Verify(16);
    
            }
    
    		break;
    
    
    
    	case 17:
    
    		cout << "\nIn how many pieces did the young Levite whose"
    
                 << "concubine was raped by the men of Gibeah cut her?";
    
    		cout << "\n1. 3"
    
    			 << "\n2. 7"
    
    			 << "\n3. 12"
    
    			 << "\n4. 36"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '3')
    
    		{
    
    			cout << "\nGood Answer";
    
    			m += 1;
    
    		}
    
    		else
    
            {
    
                cout << "\nWrong Answer!";
    
    			Verify(17);
    
            }
    
    		break;
    
    
    
    	case 18:
    
    		cout << "\n\"If anyone would come after me, he must deny\n"
    
                 << "himself and take up his cross and follow me.\"";
    
            cout << "\nWho said or wrote it ?";
    
    		cout << "\n1. Moses"
    
    			 << "\n2. John the Baptist"
    
    			 << "\n3. Jesus Christ"
    
    			 << "\n4. Paul"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '3')
    
    		{
    
    			cout << "\nGood Answer";
    
    			m += 1;
    
    		}
    
    		else               
    
            {
    
                cout << "\nWrong Answer!";
    
    			Verify(18);
    
            }
    
    		break;
    
    
    
    	case 19:
    
    		cout << "\nHow old was Abraham when he died?";
    
    		cout << "\n1. 175"
    
    			 << "\n2. 230"
    
    			 << "\n3. 310"
    
    			 << "\n4. 425"
    
    			 << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '1')
    
    		{
    
    			cout << "\nGood Answer";
    
    			m += 1;
    
    		}
    
    		else
    
            {
    
                cout << "\nWrong Answer!";
    
    			Verify(19);
    
            }
    
    		break;
    
    
    
    	case 20:
    
    		cout << "\nWho said or wrote, \" You are the light of the world \"?";
    
    		cout << "\n1. Moses"
    
    		     << "\n2. Paul"
    
    		     << "\n3. Jesus"
    
    		     << "\n4. John"
    
    		     << "\nAnswer: ";
    
    		cin >> Answer;
    
    		if(Answer[0] == '3')
    
    		{
    
    			cout << "\nGood Answer";
    
    			m += 1;
    
    		}
    
    		else
    
            {
    
                cout << "\nWrong Answer!";
    
    			Verify(20);
    
            }
    
    		break;
    
    
    
        case 21:
    
    	cout << "\nFor how long did the rain fall on the earth\n"
    
    	     << "while Noah and his family were in the ark he built?";
    
    	cout << "\n1. 30 days"
    
    	     << "\n2. 40 days"
    
    	     << "\n3. 50 days"
    
    	     << "\n4. 60 days"
    
    	     << "\nAnswer: ";
    
    	cin >> Answer;
    
    	if(Answer[0] == '2')
    
    	{
    
    		cout << "\nGood Answer";
    
    		cout << "\nRef: Ge 7:12";
    
    		m += 1;
    
    	}
    
    	else             
    
    	{
    
    	    cout << "\nWrong Answer!";
    
    	    Verify(20);
    
    	}
    
    	break;
    
        }
    
    
    
        cout << "\nPress Enter for next question...";
    
        getchar();
    
    
    
        return Answer[q];
    
    }
    
    
    
    void Verify(int t)
    
    {
    
    	switch(t)
    
    	{
    
    	case 0:
    
    		cout << "\nLk 1:26-31";
    
            cout << "\nLuke gives the name of Jesus\' mother as Mary";
    
    		break;
    
    	case 1:
    
    		cout << "\nYou can get the right answer by counting the books";
    
    		break;
    
    	case 2:
    
    		cout << "\nYou can check the answer by opening the Bible";
    
    		break;
    
    	case 3:
    
    		cout << "\nRachel was Jacob\'s wife and gave birth to two sons";
    
            cout << "\nJoseph was born in Ge 30:22-24";
    
            cout << "\nBenjamin, was Rachel\'s second son";
    
    		break;
    
    	case 4:
    
    		cout << "\nYou can find the reference at Acts 5:1-11\n";
    
    		break;
    
    	case 5:
    
    		cout << "\nWhen on Earth, Jesus Christ had 12 apostles";
    
            cout << "\nYou can find a reference at Lk 6:13";
    
    		break;
    
    	case 6:
    
    		cout << "\nThe serpent was the craftiest animal";
    
            cout << "\nThe reference can be found at Ge 3:1";
    
    		break;
    
    	case 7:
    
    		cout << "\nPeter was promised the key to the kingdom of heaven";
    
            cout << "\nThis can be checked from Mt 16:19";
    
    		break;
    
    	case 8:
    
    		cout << "\nIn 1Tm 1:2, Paul addresses Timothy as \"My true son\"";
    
    		break;
    
    	case 9:
    
    		cout << "\nThe father of the 12 patriarchs was Jacob ";
    
            cout << "as indicated in Acts 7:8";
    
    		break;
    
    	case 10:
    
    		cout << "\nAccording to Ge 4:1, Cain was Adam\'s first son";
    
            cout << "\nAdam\'s second son was Abel, as found in Ge 4:2";
    
    		break;
    
    	case 11:
    
    		cout << "\nJames addressed his book to the 12 tribes";
    
            cout << "\nReference: James 1:1";
    
    		break;
    
    	case 12:
    
    		cout << "\nMicah made the young man his priest";
    
            cout << "\nReference: Judges 17:12";
    
    		break;
    
    	case 13:
    
    		cout << "\nThe story of Noah is in Genesis";
    
            cout << "\nThe reference can be found at Ge 6:9";
    
    		break;
    
    	case 14:
    
    		cout << "\nIn the parable of the weed, the field"
    
                 << "represents the world";
    
    		cout << "\nRef: Mt 13:38";
    
    		break;
    
    	case 15:
    
            cout << "\nNoah was married: 2 people";
    
            cout << "\nNoah had 3 sons and each was married: 6 people";
    
            cout << "\n2 + 6 means there were 8 people in the arch.";
    
    		cout << "\nRef: Ge 7:7";
    
    		break;
    
    	case 16:
    
    		cout << "\nElizabeth was John The Baptiste mother";
    
            cout << "\nRef: Lk 1:36-37";
    
    		break;
    
    	case 17:
    
    		cout << "\nThis story is accounted in Judges 19";
    
            cout << "\nRef: Jg 19:29";
    
    		break;
    
    	case 18:
    
    		cout << "\nJesus said that in Mt 16:24";
    
    		break;
    
    	case 19:
    
    		cout << "\nAbraham lived 175 years and died";
    
            cout << "\nThis is indicated in Ge 25:7";
    
    		break;
    
    	case 20:
    
    		cout << "\nThis is quote from The Sermon on the Mount";
    
    		cout << "\nRef: Mt 5:14"; 
    
    	case 21:
    
    		cout << "\nwhile Noah and his family were in the ark he built\n"
    
                 << "the \"rain fell on the earth 40 days and 40 nights.";
    
            cout << "\nRef: Ge 7:12";
    
    		break;
    
    	}
    
    }
  4. Test the program.
 

  Copyright © 2003 FunctionX