A data entry program which reads and writes the data using different functions.
#include
#include
#include
FILE *fp; // public variable fp. It can now be accessed in all the functions of the program.
void main()
{
clrscr();
void write();
void read();
int c;
do
{
printf("\n C O M P U T E R -- Q U I Z ");
printf("\n ===========================");
printf("\n1. New Test");
printf("\n2. List of appeared candidates");
printf("\n3. Exit");
printf("\nSelect an option ");
scanf("%d",&c);
if(c==1)
write();
else if(c==2)
read();
}while(c!=3); // Run till the user enters 3 to exit the program.
}
void write()
{
int marks=0,c;
char name[10],doe[10],result[12],choice;
fp=fopen("exam.txt","a");
do
{
clrscr();
printf("\nCandidate Details");
printf("\n-----------------");
printf("\n\nCandidate Name : ");
scanf("%s",&name);
printf("\nDate of exam : ");
scanf("%s",&doe);
printf("\nPress any key to start the quiz..");
getch(); // to pause the screen
clrscr();
printf("\nQ1. What is the full form of CPU ? ");
printf("\n1. Central Processing Unit 2. Central Power Unit");
printf("\nEnter your choice ");
scanf("%d",&c);
if(c==1)
marks+=10;
printf("\nPress any key for the next question ...");
getch();
clrscr();
printf("\nQ2. Which of the following is a temporary memory ? ");
printf("\n1. ROM 2. RAM");
printf("\nEnter your choice ");
scanf("%d",&c);
if(c==2)
marks+=10;
printf("\nPress any key for the next question ...");
getch();
clrscr();
printf("\nQ3. Which company makes Windows ? ");
printf("\n1. Oracle Corp. 2. Microsoft Corp.");
printf("\nEnter your choice ");
scanf("%d",&c);
if(c==2)
marks+=10;
printf("\nPress any key for the next question ...");
getch();
clrscr();
printf("\nQ4. Which of the following is not an Operating System ? ");
printf("\n1. Java 2. Linux");
printf("\nEnter your choice ");
scanf("%d",&c);
if(c==1)
marks+=10;
printf("\nPress any key for the next question ...");
getch();
clrscr();
printf("\nQ5. Which of the following is an Object Oriented Language ? ");
printf("\n1. C++ 2. C");
printf("\nEnter your choice ");
scanf("%d",&c);
if(c==1)
marks+=10;
printf("\nPress any key to finish the quiz ...");
getch();
clrscr();
printf("\n Finished");
// Result is determined as per the marks scored.
if(marks>=0 && marks<=20)
strcpy(result,"Fail");
else if(marks>=30 && marks<=40)
strcpy(result,"Good");
else
strcpy(result,"Excellent");
// Writing the data to the file exam.txt
fputs("\nName ",fp); // Writing the heading first
fputs(name,fp); // Writing the value of name
fputs("\nExam Date ",fp);
fputs(doe,fp);
fputs("\nResult ",fp);
fputs(result,fp);
fclose(fp);
fflush(stdin);
printf("\nDo you want to conduct another test ? ");
scanf("%c",&choice);
}while(choice=='y'|| choice=='Y');
}
void read()
{
char line[15];
fp=fopen("exam.txt","r");
clrscr();
printf("\nList of appeared candidates");
printf("\n===============================");
while(fgets(line,15,fp)!=NULL)
{
printf("%s",line);
}
fclose(fp);
printf("\nPress any key to return to main menu ...");
getch();
}
Description
This is a basic computer quiz program. The program contains two functions namely write and read. The write function is used to write the candidate's details like name, date of exam etc. and saves them in the file. In main, we have asked the user to enter his choice - 1 for writing (taking the quiz) and 2 for reading (printing details of the appeared candidates).
The "Write" function The write function opens the file in "a" mode which means "append" mode. The "append" mode denotes that we want to write the details of all the candidates in the file one after the other without overwriting the previous data. If we use "w" mode in place of "a", it will overwrite the details of the previous candidates. Inside the function we are asking the user some basic questions. Each question has 2 options out of which the user has to choose the correct one. For each correct answer we are assigning 10 marks to the candidate by writing "marks+=10" after each question. Once all the questions have been answered, the total marks are calculated. "Result" is determined as per the result, "Excellent" for those who have scored full (50) marks and "Fail" for those who haven't given a single correct answer After the completion of the test, all the details which were entered by the user are saved in the file along with the result. Note that while writing the details we have used two "fputs()" per value.
This has been done to save the data along with its heading. For example to write the value of "name", the first "fputs()" first writes "Name" as a heading after which the second "fputs()" writes the value of the variable "name". Similarly "doe" has been saved under the heading "Exam Date" and so on. The benefit of using double "fputs()" will be shown while reading the file, where each value will come after the heading. Like "Name Xyz","Exam Date 2-10-2010" etc. You can easily skip the heading part if you want.The loop will ask the user to confirm whether he wants to continue or not. If the user chooses "y" the loop will run again, asking the user details again and quiz starts again. The details of each of the candidates will be saved in the file.
The "Read" function The read function will print the details of all the candidates who have taken the test. It opens the file in "read" mode and starts printing the details using "fgets()" reading 15 bytes at a time. This will print all the contents of the file. The file is closed and the user is asked to press a key to continue.
0 comments:
Post a Comment