Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Quiz Making & Participating Program in C (http://www.go4expert.com/articles/quiz-participating-program-c-t3102/)

rai_gandalf 22Feb2007 02:09

Quiz Making & Participating Program in C
 
1 Attachment(s)
I am attaching a ZIP file that contains a Quiz Making and Participating program. You MUST donwload the ZIP and run the exe. The source code given below would not be sufficient enough.

As of Right now, only the participating side is completed - Files are created for individual teams and all their information is recorded in them (some of them in Encrypted form). Two files are created per team:
1. T<NO>_INFO.txt => Stores the Information about the Teams and also the Scorecard
2. T<NO>_ANS.txt => Stores in Encrypted Form the Answers given by the team :cool:

Time-keeping and Question-Skipping has not been provided. I am not sure about Time-keeping .... how do I achieve it :confused: - Tried doing something with timer.h but didnt work - so I need help in this regard. As for option of Question Skipping, will work on it later.

The Quiz as of now has a fixed number of questions (40 to be precise), but as Admin functions are shaped/developed, you can expect number of questions to be Admin controlled. Questions are stored in TXT files of the format: QNUM_<NO>.txt
Questions are selected randomly from the pool of 40 questions and the appropriate file opened and the question displayed. User Response is then recorded in the ANS file in Encrypted Format. At the end of the quiz, INFO File is appended with the Scorecard.

From the Admin side, user-authentication is achieved. The Admin has an option to empty the "teamlist.txt" file, which keeps a track of which all team numbers have already been used up by teams who have participated in the Quiz, thus avoiding erroneous over-writes.
Remaining Admin Options/Controls are currently under development.

"adminlist.txt" has the list of Administrators and contains the User Name and Password (in ecrypted form). Just for demo purposes, I have added Shabbir, Coderzone, Pradeep as Administrators and I will PM the actual un-encrypted password to Shabbir.

Please do go through both Admin & Participant side & temme what u thought bout the prog and lemme know if I have missed something. :)

Ciao,
Rajiv


SOURCE CODE:
Code: C

/* QUIZ MAKING & PARTICIPATING SOFTWARE IN C */
/*
   Programmed By :
   Rajiv A Iyer
   SIES GST.
   contact: rai_maximus@yahoo.co.in
*/


/* SOURCE CODE : */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <process.h>

/* MACROS : */
#define ERR_IP    "\n\n\aINVALID INPUT!! \a Please Try Again!!\a\n\n"
#define Feat_NA   "\n\n\aFeature Unavailable!! \a It is currently under Development!!\a\n\n"
#define UNAUTH    "\n\n\aUNAUTHORIZED USER!! \a USER NAME OR PASSWORD INVALID!!\a\n\n"

#define DEFSIZE 50
#define TOT_NUM_QUEST 40
#define MAX_NUM_PART_PER_TEAM 2
#define CRYPT_SCHEME1 1
#define CRYPT_SCHEME2 2
#define SEQUENTIAL_QNA_MODE 1
#define RANDOM_QNA_MODE 2

/* Symbolic Constants */
const int RESET=-1;
const int CORRECT=1,WRONG=2,INDETERMINATE=3;

/* Global Variables */
// File Pointers & File Name Variables
FILE *partinfofile,*partansfile;
FILE *queinfofile,*quefile,*solutionfile;
FILE *adminlistfile,*teamlistfile;
char partinfo_filename[DEFSIZE],partans_filename[DEFSIZE];
char queinfo_filename[DEFSIZE],que_filename[DEFSIZE];

// User Choice & Attribute Capture Variables
int teamnum;

// User-Time-Tracking Variables
char quiz_start_time[DEFSIZE],quiz_end_time[DEFSIZE];

// Administrator-Controlled Variables
int time_limit;
int encryption_scheme;
int file_name_scheme;

/* Function Declarations */
// Basic Entry Functions
void login_participant(void);
void login_admin(void);

// Participant Related Functions
void getTeamInfo(void);
bool teamnumAvail(int);
void getQuestion(int,int);
void tallyAnswers(void);

// Administrator Related Functions
bool authenticateAdmin(char[]);
void addAdmin(void);
void changeSettings(void);

// Groundwork/Common Functions
char* int_to_str(int);

// Encryption & Decryption Functions
char* encryptstr(char[],int);
char* decryptstr(char[],int);


/* Main Function */
void main()
  {
  int mainchoice;

  do{
  clrscr();
  printf("MAIN MENU :\n");
  printf("LogIn as :\n");
  printf("1. Quiz Administrator\n2. Quiz Participant\n3. EXIT\n\n");
  printf("Enter Choice :  ");
  scanf("%d",&mainchoice);

  switch(mainchoice)
    {
    case 1:
      login_admin();
      break;
    case 2:
      login_participant();
      break;
    case 3:
      break;
    default:
      puts(ERR_IP);
    }
  getch();
  }while(mainchoice!=3);
  }


void login_participant(void)
  {
  int i;
  int j;
  int qna_mode,curqnum,*qseq;

  /* Get Basic Team Information & Write it onto corresponding INFO File */
  getTeamInfo();

  /* Seed the Random Generator */
  randomize();

  /* Setting Mode of Questions - Sequential or Random */
  qna_mode=RANDOM_QNA_MODE;

  /* Allocating Memory For 'TOT_NUM_QUEST' number of elements & Initializing Them */
  qseq=(int*)malloc(TOT_NUM_QUEST*sizeof(int));
  for(i=0;i<TOT_NUM_QUEST;i++)
    qseq[i]=RESET;

  /* Computing Participant Answer File Name */
  strcpy(partans_filename,"T");
  strcat(partans_filename,int_to_str(teamnum));
  strcat(partans_filename,"_ANS.txt");

  /* Opening the Participant's Answer Booklet/File in Write Mode */
  partansfile=fopen(partans_filename,"w");

  clrscr();
  printf("\n\n\nFORMALITIES DONE! QUIZ BEGINS!");
  printf("\n\n\nPLEASE NOTE: KEEP CAPS LOCK ON! Quiz Answers are CASE SENSITIVE!");
  printf("\n\n\nPRESS ANY KEY TO START! .....");
  getch();
  clrscr();

  /* Start of Quiz - Record Current Time in quiz_start_time */
  _strtime(quiz_start_time);

  /* Iterating For As Many Questions */
  for(i=0;i<TOT_NUM_QUEST;i++)
    {
    if(qna_mode==SEQUENTIAL_QNA_MODE)
        curqnum=i+1;
    else if(qna_mode==RANDOM_QNA_MODE)
      {
      do{
        curqnum=random(TOT_NUM_QUEST+1);
        for(j=0;j<i;j++)
          if(qseq[j]==curqnum)
            break;
        }while(curqnum==0 || j!=i);
      }


    /* Question is Selected. Therefore Setting 'qseq[i]' */
    qseq[i]=curqnum;

    /* Obtaining Question File Name */
    strcpy(que_filename,"QNUM_");
    strcat(que_filename,int_to_str(curqnum));
    strcat(que_filename,".txt");

    /* Put Forth Question From File to User & Record his/her Response in another file */
    getQuestion(i+1,curqnum);
    }

    /* End of Quiz - So Recording the Time */
    _strtime(quiz_end_time);

    /* Closing Participant's Answer Booklet/File */
    fclose(partansfile);

    /* Scrutinize & Tally Answers & Give Score */
    tallyAnswers();
  }

void getTeamInfo(void)
  {
  int i;
  int numavail;
  // User Choice & Attribute Capture Variables
  int numpart;
  char **partnames,**partmobnums;
  char partcollege[DEFSIZE];

  do{
  clrscr();
  printf("Welcome to the Quiz! Some Formalities :\n\n");

  printf("Enter your Team Number :  ");
  scanf("%d",&teamnum);

  printf("Enter Number of Participants in Team :  ");
  scanf("%d",&numpart);

  if(teamnum<=0 || numpart<=0 || numpart>MAX_NUM_PART_PER_TEAM)
    {
    puts(ERR_IP);
    getch();
    }
  else if((numavail=teamnumAvail(teamnum))==false)
    {
    printf("\n\n\aTeam Number Already Selected! Choose Another Team Number!");
    getch();
    }
  else
    {
    teamlistfile=fopen("teamlist.txt","a");
    fprintf(teamlistfile,"%d\t",teamnum);
    fclose(teamlistfile);
    }
  }while(teamnum<=0 || numpart<=0 || numpart>=3 || numavail==false);

  partnames=(char**)malloc(numpart*sizeof(char));
  partmobnums=(char**)malloc(numpart*sizeof(char));
  printf("Enter Name of each participant :  \n");
  for(i=0;i<numpart;i++)
    {
    partnames[i]=(char*)malloc(DEFSIZE*2*sizeof(char));
    partmobnums[i]=(char*)malloc(DEFSIZE*sizeof(char));
    printf("\nFor Participant #%d :\n",i+1);
    printf("Enter Name & Year :  ");
    flushall();
    gets(partnames[i]);
    printf("Enter Mobile Numbers :  ");
    flushall();
    gets(partmobnums[i]);
    }

  printf("\n\nEnter Name of College :  ");
  flushall();gets(partcollege);

  /* Computing Participant INFO File Name */
  strcpy(partinfo_filename,"T");
  strcat(partinfo_filename,int_to_str(teamnum));
  strcat(partinfo_filename,"_INFO.txt");

  /* Writing Participant Information Into INFO File */
  partinfofile=fopen(partinfo_filename,"w");
  fprintf(partinfofile,"Team Number :  %d\n",teamnum);

  fprintf(partinfofile,"Team Members :\n");
  for(i=0;i<numpart;i++)
    {
    fprintf(partinfofile,"Member #%d :\n",i+1);
    fprintf(partinfofile,"Member Name :  %s\nPhone Number :  %s\n\n",partnames[i],partmobnums[i]);
    }
  fprintf(partinfofile,"\nCollege :  %s",partcollege);
  fclose(partinfofile);
  }

bool teamnumAvail(int teamnum)
  {
  int tnum;

  teamlistfile=fopen("teamlist.txt","r");
  while(!feof(teamlistfile))
    {
    fscanf(teamlistfile,"%d",&tnum);
    if(teamnum==tnum)
      {
      fclose(teamlistfile);
      return false;
      }
    }
  fclose(teamlistfile);
  return true;
  }

void getQuestion(int qno,int curqnum)
  {
  char c,response[DEFSIZE];
  clrscr();

  printf("Question Number          :  %d\n",qno);
  printf("Question Pool Number     :  %d\n\n",curqnum);
  printf("Quiz Start Time          :  %s\n",quiz_start_time);

  printf("\nQUESTION NUMBER #%d IS :\n",qno);
  /* Opening the Question File in Read Mode */
  quefile=fopen(que_filename,"r");

  if(quefile==NULL)
    {
    printf("\n\n\aQUEFILE OPENING ERROR!!");
    getch();
    exit(1) ;
    }

  while(!feof(quefile))
    {
    c=getc(quefile);
    printf("%c",c);
    }

  printf("\n");

  /* Closing File */
  fclose(quefile);

  printf("\n\n\nYour Response :  ");
  flushall();
  //gets(response);
  scanf("%s",response);

  /* Recording the Participant's Response */
  if(qno>1)
    fprintf(partansfile,"\n");
  fprintf(partansfile,"%d\t%d\t%s",qno,curqnum,encryptstr(response,CRYPT_SCHEME1));
  }

void tallyAnswers(void)
  {
  int qno,curqnum,solqno;
  char response[DEFSIZE],solution[DEFSIZE];
  int foundflag;
  int answer_status[TOT_NUM_QUEST];
  int correctans,wrongans,indeterminateans;

  // Initializing Counter variables to zero
  correctans=wrongans=indeterminateans=0;

  // Opening & Modifying Participant Information File so as to reflect the Scorecard
  partinfofile=fopen(partinfo_filename,"a");
  // Writing Start & End Time
  fprintf(partinfofile,"\n\nStart Time :  %s\nEnd Time :  %s\n",quiz_start_time,quiz_end_time);
  fprintf(partinfofile,"\n\nDETAILED SCORECARD:\n");

  /* Opening Participant Answer Booklet AND Admin Set Solution Booklet in READ Mode */
  partansfile=fopen(partans_filename,"r");
  solutionfile=fopen("encsol.txt","r");

  while(!feof(solutionfile))
    {
    // Reading the Solution of Questions Sequentially from Solution Booklet
    fscanf(solutionfile,"%d %s",&solqno,solution);

    // Initiate Search in Participant Answer File for solution of the concerned Question
    foundflag=false;
    while(!feof(partansfile))
      {
      fscanf(partansfile,"%d %d %s",&qno,&curqnum,response);

      if(solqno==curqnum)
        {
        foundflag=true;
        rewind(partansfile);
        break;
        }
      }
    if(foundflag==false)
      {
      printf("\n\n\aDATA NOT FOUND IN FILE!!\a\n\n");
      getch();
      fclose(partansfile);
      fclose(solutionfile);
      exit(1);
      }
    else
      {
      if(strlen(solution)==1)
        {
        if(/*strlen(response)==1 && */strcmp(decryptstr(response,CRYPT_SCHEME1),decryptstr(solution,CRYPT_SCHEME2))==0)
          {
          answer_status[curqnum]=CORRECT;
          correctans++;
          fprintf(partinfofile,"%d\t1\tCORRECT\n",solqno);
          }
        else
          {
          answer_status[curqnum]=WRONG;
          wrongans++;
          fprintf(partinfofile,"%d\t0\tWRONG\n",solqno);
          }
        }
      else
        {
        if(strlen(response)==1)
          {
          answer_status[curqnum]=WRONG;
          wrongans++;
          fprintf(partinfofile,"%d\t0\tWRONG\n",solqno);
          }
        else
          {
          answer_status[curqnum]=INDETERMINATE;
          indeterminateans++;
          fprintf(partinfofile,"%d\t2\tINDETERMINATE\n",solqno);
          }
        }
      }
    }

    clrscr();
    printf("SCORECARD:\n\n");
    printf("Total Correct Answers       :  %d\n",correctans);
    printf("Total Wrong Answers         :  %d\n",wrongans);
    printf("Total Indeterminate Answers :  %d\n",indeterminateans);
    getch();

    /* Writing the Final Scorecard onto Participant Info File */
    fprintf(partinfofile,"\n\nScore-card Summary:\n");
    fprintf(partinfofile,"\nQuestions Correctly Answered :  %d\nQuestions Wrongly Answered :  %d\nQuestions with Indeterminate Answers :  %d",correctans,wrongans,indeterminateans);
    /* Closing the Participant Information File */
    fclose(partinfofile);
  }

// Administrator Functions Begin
void login_admin(void)
  {
  int choice;
  char admin_username[DEFSIZE];

  if(authenticateAdmin(admin_username)==true)
    {
    clrscr();
    printf("Welcome Admin :  %s!!",admin_username);
    getch();

    do{
      clrscr();
      printf("ADMIN MENU :\n");
      printf("1. CHANGE USERNAME\n2. CHANGE PASSWORD\n3. CHANGE TIME-LIMIT\n");
      printf("4. CHANGE NUMBER OF QUESTIONS\n5. ADD/REMOVE QUESTION(S)\n6. EDIT QUESTION(S)\n");
      printf("7. ADD/REMOVE ANSWER-SHEET\n8. EDIT ANSWER(S)\n8. EDIT ENCRYPTION SCHEME\n");
      printf("9. CLEAR TEAM-LIST FILE\n10. LOG-OUT & RETURN TO MAIN MENU\n11. LOG-OUT & EXIT\n");
      printf("\n\nEnter Choice :  ");
      scanf("%d",&choice);

      switch(choice)
        {
        case 1:
          //break;
        case 2:
          //break;
        case 3:
          //break;
        case 4:
          //break;
        case 5:
          //break;
        case 6:
          //break;
        case 7:
          //break;
        case 8:
          puts(Feat_NA);
          //break;
        case 9:
          /* Opening & Closing Team-List in WRITE mode */
          teamlistfile=fopen("teamlist.txt","w");
          fclose(teamlistfile);
          printf("\n\n\nTEAM-LIST FILE CLEARED!!");
          break;
        case 10:
          break;
        case 11:
          exit(0);
        default:
          puts(ERR_IP);
        }
      getch();
      }while(choice!=10);
    }
  else
    return;
  }
bool authenticateAdmin(char admin_username[DEFSIZE])
  {
  int i,j,x,y,foundflag=false;
  char c;
  char admin_password[DEFSIZE];
  char curusername[DEFSIZE],curpassword[DEFSIZE];

  clrscr();
  printf("Welcome to the Quiz Administrator Log-In Screen! Firstly, Authentication is Required!");
  printf("\nTo Proceed, Please Enter the following :\n\n");

  printf("Enter USER NAME :  ");
  flushall();
  gets(admin_username);
  printf("Enter Password :  ");
  x=20;y=6;
  gotoxy(x,y);
  for(i=0,j=0;;i++,j++)
    {
    flushall();
    c=getch();
    if(c=='\n' || c==13)
      break;
    else
      admin_password[i]=c;
    gotoxy(x+j,y);
    putchar('*');
    }
  admin_password[i]='\0';

  /* Opening File containing Authorized Administrator List */
  adminlistfile=fopen("adminlist.txt","r");

  while(!feof(adminlistfile))
    {
    fscanf(adminlistfile,"%s %s",curusername,curpassword);
    if(strcmp(admin_username,curusername)==0 && strcmp(admin_password,decryptstr(curpassword,CRYPT_SCHEME2))==0)
      {
      foundflag=true;
      break;
      }
    }

  /* Closing Admin List File */
  fclose(adminlistfile);

  if(foundflag==false)
    {
    clrscr();
    puts(UNAUTH);
    getch();
    return false;
    }
  else
    return true;
  }


// Groundwork/Common Functions
char* int_to_str(int num)
  {
  char temp[DEFSIZE];
  if(num<=9)
    {
    temp[0]=char(num+'0');
    temp[1]='\0';
    }
  else if(num<=99)
    {
    temp[0]=char('0'+num/10);
    temp[1]=char('0'+num%10);
    temp[2]='\0';
    }
  else
    {
    printf("\n\n\aFile Number Limit Breached!! Program Terminates!!\a\n\n");
    getch();
    exit(1);
    }
  return temp;
  }

// Encryption & Decryption Functions
char* encryptstr(char str[],int scheme)
  {
  int i,cnum;
  char *result;
  result=(char*)malloc((strlen(str)+1)*sizeof(str));

  for(i=0;str[i]!='\0';i++)
    {
    if(str[i]>='a' && str[i]<='z')
      cnum=(str[i]-'a')+1;
    else if(str[i]>='A' && str[i]<='Z')
      cnum=(str[i]-'A')+26+1;
    else if(str[i]>='0' && str[i]<='9')
      cnum=(str[i]-'0')+52+1;
    else
      cnum=RESET;

    if(cnum==RESET)
      result[i]=str[i];
    else
      {
      if(scheme==CRYPT_SCHEME1)
        cnum=(cnum+12)%62;
      else if(scheme==CRYPT_SCHEME1)
        cnum=(cnum+18)%62;

      if(cnum>=1 && cnum<=26)
        result[i]=(char)(cnum-1+'a');
      else if(cnum>=27 && cnum<=52)
        result[i]=(char)(cnum-26-1+'A');
      else if(cnum>=53 && cnum<=62)
        result[i]=(char)(cnum-52-1+'0');
      }
    }
  result[i]='\0';
  return result;
  }


char* decryptstr(char str[],int scheme)
  {
  int i,cnum;
  char *result;
  result=(char*)malloc((strlen(str)+1)*sizeof(str));

  for(i=0;str[i]!='\0';i++)
    {
    if(str[i]>='a' && str[i]<='z')
      cnum=(str[i]-'a')+1;
    else if(str[i]>='A' && str[i]<='Z')
      cnum=(str[i]-'A')+26+1;
    else if(str[i]>='0' && str[i]<='9')
      cnum=(str[i]-'0')+52+1;
    else
      cnum=RESET;

    if(cnum==RESET)
      result[i]=str[i];
    else
      {
      if(scheme==CRYPT_SCHEME1)
        cnum=(cnum+50)%62;
      else if(scheme==CRYPT_SCHEME2)
        cnum=(cnum+44)%62;

      if(cnum>=1 && cnum<=26)
        result[i]=(char)(cnum-1+'a');
      else if(cnum>=27 && cnum<=52)
        result[i]=(char)(cnum-26-1+'A');
      else if(cnum>=53 && cnum<=62)
        result[i]=(char)(cnum-52-1+'0');
      }
    }
  result[i]='\0';
  return result;
  }


PS: The Program worked fine with Borland C++ 5.02 Compiler, but gave me NULL Pointer returns to File Pointer Variables and other File Related errors possibly due to some long file names I have used (When I shortened the File Names, these errors vanished - some explanation of this anomaly would also be appreciated). :)

shabbir 22Feb2007 08:56

Re: Quiz Making & Participating Program in C
 
Probably the rar file did not attach successfully and you need to use the zip file.

rai_gandalf 22Feb2007 11:34

Re: Quiz Making & Participating Program in C
 
Quote:

Originally Posted by shabbir
Probably the rar file did not attach successfully and you need to use the zip file.

Hey thx for bringing it to my notice .... have done the needful.

The ZIP File contains:
1. The C File
2. The EXE File
3. All Concerned Question, Admin Files that the program needs to read from.

As you run the EXE (or compile & run the program) from the participant side, more Text files will be created (2 file per participating Team), which will contain Information regarding the participant and their answers in Encrypted Form.

Ciao,
Rajiv

shabbir 22Feb2007 12:01

Re: Quiz Making & Participating Program in C
 
I will definitely run with the details you provided to me and let you have the feedback when I reach home.

Bhullarz 1Apr2007 03:25

Re: Quiz Making & Participating Program in C
 
Use TSR technique with time class for time-keeping.you can have a try.


All times are GMT +5.5. The time now is 00:06.