makig a Randomizes Quiz Help

This is a discussion on makig a Randomizes Quiz Help within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a simple quiz program. Well I am just a beginner at it. Here I have ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    makig a Randomizes Quiz Help

    I am trying to make a simple quiz program. Well I am just a beginner at it.
    Here I have deigned a simple quiz that gives the user 10 question and depending on their answer respond to them.


    Code:
    //Quiz program
    #include <iostream>
    #include <string>
    #include <time.h>
    
    using namespace std;
    	
    	//Arrays and Variables and Strings
    	int Q[10] = {};
    	float score = 0;
    	string correct = "This is the correct answer";
    	string wrong = "Sorry, this is the incorrect answer. The correct answer was ";   /*The correct option will have to be put after "<<"*/
    	string divider = "--------------------------------------------------------------------------------";
    	
    	//Main Program
    	int main ()
    {
    	//Timer Define
    		int elapTicks;
    		double elapMilli, elapSeconds, elapMinutes;
    		clock_t Begin, End;															//initialize Begin and End for the timer
    
    	//Instructions:
    	cout << "There will be 10 questions each having 4 options and ONLY 1 Correct answer." << endl;
    	cout << "To select the answer, type the correct option number and press ENTER/RETURN." << endl;
    	cout << "ONE MARK will be provided for a correct answer and HALF A MARK [0.5] will be deducted for a wrong one."<< endl;
    	cout << "No marks will be deducted for skipping a Question." << endl << "A QUESTION CAN BE SKIPPED BY ENTERING THE OPTION NUMBER AS 0" << endl;
    	cout << divider << endl << endl;
    	
    
    	//Timer Start
    	 Begin = clock() * CLK_TCK;
    	 for(int a=1; a<=10000; a++);
    
    	//Questions:
    			
    		//Question 1
    		cout << "1) First China War was fought between ____________" << endl;
    		cout << "1.China and Britain" << endl;
    		cout << "2.China and France" << endl;
    		cout << "3.China and Egypt" << endl;
    		cout << "4.China and Russia" << endl << endl;
    
    			//Question 1 Answer
    			cout << "Ans. ";
    			cin >> Q[0];
    			cout << endl << endl;
    			//Question 1 Evaluation:
    			if (Q[0] == 1)
    			{ 
    				cout << correct << endl << endl;
    				score = score + 1	;
    			}
    			
    			else if (Q[0] == 0)
    				cout << "You skipped the Question" << endl << endl;
    			
    			else 
    			{
    				cout << wrong << "1. CHINA AND BRITAIN" << endl << endl;
    				score = score - 0.5;
    			}
    
    			cout << divider << endl << endl;
    
    
    		//Question 2:
    		cout << "2)For the Olympics and World Tournaments, the dimensions of basketball court are____________" << endl;
    		cout << "1.26 m x 14 m" << endl;
    		cout << "2.28 m x 15 m" << endl;
    		cout << "3.27 m x 16 m" << endl;
    		cout << "4.28 m x 16 m" << endl << endl;
    
    			//Question 2 Answer
    			cout << "Ans. ";
    			cin >> Q[1];
    			cout << endl << endl;
    			//Question 2 Evaluation:
    			if (Q[1] == 2)
    			{ 
    				cout << correct << endl << endl;
    				score = score + 1;
    			}
    			
    			else if (Q[1] == 0)
    				cout << "You skipped the Question" << endl << endl;
    
    			else
    			{
    				cout << wrong << "2. 2.28 m x 15 m" << endl << endl;
    				score = score - 0.5;
    			}
    
    			cout << divider << endl << endl;
    
    
    		//Question 3:
    		cout << "3)Each year World Red Cross and Red Crescent Day is celebrated on" << endl;
    		cout << "1.May 8" << endl;
    		cout << "2.May 18" << endl;
    		cout << "3.June 8" << endl;
    		cout << "4.June 18" << endl << endl;
    
    			//Question 3 Answer
    			cout << "Ans. ";
    			cin >> Q[2];
    			cout << endl << endl;
    			//Question 4 Evaluation:
    			if (Q[2] == 1)
    			{ 
    				cout << correct << endl << endl;
    				score = score + 1;
    			}
    			
    			else if (Q[2] == 0)
    				cout << "You skipped the Question" << endl << endl;
    			
    			else
    			{
    				cout << wrong << "1. MAY 8" << endl << endl;
    				score = score - 0.5;
    			}
    
    
    			cout << divider << endl << endl;
    
    		//Question 4:
    		cout << "4)Friction can be reduced by changing from____________" << endl;
    		cout << "1.sliding to rolling" << endl;
    		cout << "2.rolling to sliding" << endl;
    		cout << "3.Kynetic to Dynamic" << endl;
    		cout << "4.dynamic to static" << endl << endl;
    
    			//Question 4 Answer
    			cout << "Ans. ";
    			cin >> Q[3];
    			cout << endl << endl;
    			//Question 4 Evaluation:
    			if (Q[3] == 1)
    			{ 
    				cout << correct << endl << endl;
    				score = score + 1;
    			}
    
    			else if (Q[3] == 0)
    				cout << "You skipped the Question" << endl << endl;
    			else 
    			{
    				cout << wrong << "1.SLIDING TO ROLLING" << endl << endl;
    				score = score - 0.5;
    			}
    
    			cout << divider << endl << endl;
    
    
    			//Question 5:
    		cout << "5)Georgia, Uzbekistan and Turkmenistan became the members of UNO in_____" << endl;
    		cout << "1.1991" << endl;
    		cout << "2.1992" << endl;
    		cout << "3.1993" << endl;
    		cout << "4.1994" << endl << endl;
    
    			//Question 5 Answer
    			cout << "Ans. ";
    			cin >> Q[4];
    			cout << endl << endl;
    			//Question 5 Evaluation:
    			if (Q[4] == 2)
    			{ 
    				cout << correct << endl << endl;
    				score = score + 1;
    			}
    			
    			else if (Q[4] == 0)
    				cout << "You skipped the Question" << endl << endl;
    			
    			else
    			{
    				cout << wrong << "2.1992" << endl << endl;
    				score = score - 0.5;
    			}
    
    			cout << divider << endl << endl;
    
    		//Question 6:
    		cout << "6)During World War II, when did Germany attack France?" << endl;
    		cout << "1.1940" << endl;
    		cout << "2.1941" << endl;
    		cout << "3.1942" << endl;
    		cout << "4.1993" << endl << endl;
    
    			//Question 6 Answer
    			cout << "Ans. ";
    			cin >> Q[5];
    			cout << endl << endl;
    			//Question 6 Evaluation:
    			if (Q[5] == 1)
    			{ 
    				cout << correct << endl << endl;
    				score = score + 1;
    			}
    			
    			else if (Q[5] == 0)
    				cout << "You skipped the Question" << endl << endl;
    			
    			else
    			{
    				cout << wrong << "2.1940" << endl << endl;
    				score = score - 0.5;
    			}
    
    			cout << divider << endl << endl;
    
    
    		//Question 7:
    		cout << "7)Frederick Sanger is a twice recipient of the Nobel Prize for ______" << endl;
    		cout << "1.Chemistry in 1958 and 1980" << endl;
    		cout << "2.Physics in 1956 and 1972" << endl;
    		cout << "3.Chemistry in 1954 and Peace in 1962" << endl;
    		cout << "4.Physics in 1903 and Chemistry in 1911" << endl << endl;
    
    			//Question 7 Answer
    			cout << "Ans. ";
    			cin >> Q[6];
    			cout << endl << endl;
    			//Question 7 Evaluation:
    			if (Q[6] == 1)
    			{ 
    				cout << correct << endl << endl;
    				score = score + 1;
    			}
    			
    			else if (Q[6] == 0)
    				cout << "You skipped the Question" << endl << endl;
    			
    			else
    			{
    				cout << wrong << "1.CHEMISTRY IN 1958 AND 1980" << endl << endl;
    				score = score - 0.5;
    			}
    
    			cout << divider << endl << endl;
    
    
    		//Question 8:
    		cout << "8)Eugenics is the study of" << endl;
    		cout << "1.altering human beings by changing their genetic components" << endl;
    		cout << "2.people of European origin" << endl;
    		cout << "3.different races of mankind" << endl;
    		cout << "4.genetic of plants" << endl << endl;
    
    			//Question 8 Answer
    			cout << "Ans. ";
    			cin >> Q[7];
    			cout << endl << endl;
    
    			//Question 8 Evaluation:
    			if (Q[7] == 1)
    			{ 
    				cout << correct << endl << endl;
    				score = score + 1;
    			}
    			
    			else if (Q[7] == 0)
    				cout << "You skipped the Question" << endl << endl;
    			
    			else
    			{
    				cout << wrong << "1.ALTERING HUMAN BEINGS BY CHANGING THEIR GENETIC COMPONENTS" << endl << endl;
    				score = score - 0.5;
    			}
    
    			cout << divider << endl << endl;
    
    
    		//Question 9:
    		cout << "9)Goa Shipyard Limited (GSL) was established in ____" << endl;
    		cout << "1.1955" << endl;
    		cout << "2.1956" << endl;
    		cout << "3.1957" << endl;
    		cout << "4.1958" << endl << endl;
    
    			//Question 9 Answer
    			cout << "Ans. ";
    			cin >> Q[8];
    			cout << endl << endl;
    			//Question 9 Evaluation:
    			if (Q[8] == 3)
    			{ 
    				cout << correct << endl << endl;
    				score = score + 1;
    			}
    			
    			else if (Q[8] == 0)
    				cout << "You skipped the Question" << endl << endl;
    			
    			else
    			{
    				cout << wrong << "3.1957" << endl << endl;
    				score = score - 0.5;
    			}
    
    			cout << divider << endl << endl;
    
    
    
    			//Question 10:
    		cout << "10)Escape velocity of a rocket fired from the earth towards the moon is a velocity to get rid of the _______" << endl;
    		cout << "1.Pressure of the atmosphere" << endl;
    		cout << "2.Moon's gravitational pull" << endl;
    		cout << "3.Centripetal force due to the earth's rotation" << endl;
    		cout << "4.Earth's gravitational pull" << endl << endl;
    
    			//Question 10 Answer
    			cout << "Ans. ";
    			cin >> Q[9];
    			cout << endl << endl;
    			//Question 10 Evaluation:
    			if (Q[9] == 4)
    			{ 
    				cout << correct << endl << endl;
    				score = score + 1;
    			}
    			
    			else if (Q[9] == 0)
    				cout << "You skipped the Question" << endl << endl;
    			
    			else
    			{
    				cout << wrong << "4.EARTH'S GRAVITATIONAL PULL" << endl << endl;
    				score = score - 0.5;
    			}
    
    			
    		//Timer stop:
    		End = clock() * CLK_TCK;        //stop the timer
    		//Thanks Note:
    			cout << "---------------------------------Test Completed---------------------------------"  << endl << endl;
    
    		//Scoring:
    
    		cout << "Your total score was " << score << " points!" << endl;
    		
    			
    		//Time Taken:
    			
    		elapTicks = End - Begin;        //the number of ticks from Begin to End
    		
    		elapMilli = elapTicks/1000;     //milliseconds from Begin to End
    		
    		elapSeconds = elapMilli/1000;   //seconds from Begin to End
    		
    		elapMinutes = elapSeconds/60;   //minutes from Begin to End
         
         
    		 if(elapSeconds < 1)
              cout<<"\n\nYou took "<<elapMilli<<" milliseconds.";
    
    		else if(elapSeconds == 1)
              cout<<"\n\nYou took  1 second.";
    		
    		else if(elapSeconds > 1 && elapSeconds < 60)
              cout<<"\n\nYou took  "<<elapSeconds<<" seconds.";
    		
    		else if(elapSeconds >= 60)     
              cout<<"\n\nYou took  "<<elapMinutes<<" minutes.";
    			 
    		
    	//Terminating the Program:
    	return 0;
    }
    I wanted to make the quiz random, like, I add 5 more questions to the list, and then the program choses any 10 of those 15 question and presents them to the user.
    I am using Visual C++ in windows. Can some one please help me?
    Thanks in advance.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can start by making "question" a function or class so that you do not have to repeat it over and over. This will make it easy to select random questions from an array.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Could you elaborately explain it? And I haven't learnt using classes yet.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    I studied Classes for a while and then tried to make the class question.

    I used the following code:
    Code:
    //Arrayrs, Variables and Strings
    	int x;	
    	int q;
    	int w;
    	int e;
    	int r;
    	int t;
    	int y;
    	int u;
    	int i;
    	int o;
    	int p;
    	float score = 0;
    	string correct = "This is the correct answer";
    	string wrong = "Sorry, this is the incorrect answer. The correct answer was ";   /*The correct option will have to be put after "<<"*/
    	string skip = "You skipped the Question";
    	string divider = "--------------------------------------------------------------------------------";
    
    //Qestion Class
    class Question 
    {
    public:
    	Question ();
    	//Constructor
    	~Question ();
    	//Destructor
    
    protected:
    	int a, b, c, d;
    	
    	int number (int a);
    	
    	string question (char);
    	
    	string opt1 (char);
    	
    	string opt2 (char);
    	
    	string opt3 (char);
    	
    	string opt4 (char);
    	
    	int correct_ans (int c);
    	int  ans(int c) 
    	{ 
    		if (b == c)
    			{
    				cout << correct;
    				score += 1;
    			}
    		else if ( b == 0 )
    		{
    			cout << skip;
    		}
    
    		else 
    		{
    			cout << wrong;
    			score -=0.5;
    		}
    	};
    But after I try to compile it, I encounter "fatal error C1075: end of file found before the left brace '{' at ..............

    I have made the bracket bold, the one mentioned in the error.

    Please tell me what to do now, and How to use this class in my program.
    Last edited by Nisheeth; 04-05-2011 at 09:32 AM. Reason: Correction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. Hi, Quiz C program
    By Eman in forum C Programming
    Replies: 0
    Last Post: 11-11-2009, 04:12 PM
  3. Try this quiz (just made it with C)
    By voltson4 in forum C Programming
    Replies: 1
    Last Post: 07-11-2003, 07:28 PM
  4. C programming language quiz
    By Trancongan in forum C Programming
    Replies: 7
    Last Post: 04-21-2002, 02:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21