Monday, December 20, 2010

Program which will display list of Questions (4), and prompt user to enter the question number, program display the question and prompt for the answer. same will repeat till user enter answer for all the questions. once doe program display the question and the user input answer.

#include<stdio.h>

char que[4][100]={
  {"[1]. What is your name?"},
  {"[2]. What is your age?"},
  {"[3]. What is your Profession?"},
  {"[4]. Are you interested in C Programs?"}
};

char ans[4][100]={
  {""},
  {""},
  {""},
  {""}
};

int choice[4]={0,0,0,0};

int capture_input();
void display_que(int choice[]);
void display_que_ans();

int main()
{
  int i=0,j=0,input=0;
 
  do
  {  
    input=capture_input();  
  
    switch(input)
    {
      case 1:
choice[j++]=input;
printf("%s\n",que[0]);
scanf("%s",ans[0]);
break;

      case 2:
choice[j++]=input;
printf("%s\n",que[1]);
scanf("%s",ans[1]);
break;

      case 3:
choice[j++]=input;
printf("%s\n",que[2]);
scanf("%s",ans[2]);
        break;

      case 4:
choice[j++]=input;
printf("%s\n",que[3]);
scanf("%s",ans[3]);
        break;

      case 9: // Option to exit.
display_que_ans();
return 0;

      default:
printf("Incorrect input, please try again.\n\n");
    }
  }while(j<4);
 
  display_que_ans();
  return 0;
}

int capture_input()
{
  int input=0,i=0,repeat;
 
  do
  {
    display_que(choice);
    repeat=-1;
    printf("Enter your choice (9-For Exit) [X]: ");
    scanf("%d",&input);
  
    // Check for already answered.
    for(i=0;i<4;i++){
      if(choice[i]==input && input !=0)
      {
printf("Already answered, please try again.\n\n");
repeat=0;
break;
      }    
    }
  }while(repeat==0);
  
  return input;
}

void display_que(int choice[])
{
  int i,j,f;
 
  for(i=0;i<4;i++)
  {
    f=0;
    for(j=0;j<4;j++)
    {
      if(choice[j]==(i+1))
      {
f=1;
break;
      }
    }
    if(f==0)
    {
      printf("%s\n",que[i]);
    }
  }
  printf("\n");
}

void display_que_ans()
{
  int i;
 
  printf("\n\n-------------------------------------------------------------\n");
  for(i=0; i<4; i++)
  {
    printf("Que: %s\nAns: %s\n",que[i],ans[i]);
  }
  printf("-------------------------------------------------------------\n");
}

No comments:

Post a Comment