0

I am making a quiz program in c for a school project.I was storing question and answers in a text file.The text file contains 1 question and followed by 4 choices and a correct answer(each in a new line) and so on.The code for file handling is

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int tnum=2,mnum;
printf("Enter a file name to load the quiz from or enter dhruv.txt to load the default file\n");
printf("(For type of file and arrangement of data in it, refer to the documentation\n");
printf("WARNING: An improper quiz file may result in malfunctioning of the program.\n");
char quizfile[100];
scanf("%s",quizfile);
FILE *dj;
dj = fopen(quizfile,"r");
int test = 1;
while(dj == NULL)
{
    printf("Requested file does not exist.Please enter a valid name\n");
    scanf("%s",quizfile);
    dj = fopen(quizfile,"r");
    test++;
    if(test == 5)
    {
    exit(0);
    }
}
char line[500];
char ques[20][500],ansa[20][500],ansb[20][500],ansc[20][500],ansd[20][500],anse[20][500];
int start = 1,quesval=1,ans1=1,ans2=1,ans3=1,ans4=1,ans5=1;
while(fgets(line,sizeof line,dj) != NULL)
{
    if((start%6) == 1)
    {
        strcpy(ques[quesval],line);
        quesval++;
    }
    if((start%6) == 2)
    {
        strcpy(ansa[ans1],line);
        ans1++;
    }
    if((start%6) == 3)
    {
        strcpy(ansb[ans2],line);
        ans2++;
    }
    if((start%6) == 4)
    {
        strcpy(ansc[ans3],line);
        ans3++;
    }

    if((start%6) == 0)
    {
        strcpy(anse[ans5],line);
        ans5++;
    }

    if((start%6) == 5)
    {
        strcpy(ansd[ans4],line);
        ans4++;
    }

    start++;

}
fclose(dj);
printf("Quiz file successfully loaded\n");
printf("/t/t WELCOME TO THE QUIZ\n\n");
printf("Every team must select one of the four correct answers to the asked questions to gain points\n");
printf("Wrong answer will not be penalized\n");
for(int k =1;k<quesval;k++)
{
    int myvar;
    myvar = k%tnum;
    if(myvar == 0)
    {
        myvar = tnum;
    }
    printf("Question for TEAM %d\n\n",myvar);
    printf("%s \n A.%s B.%s C.%s D.%s\n",ques[k],ansa[k],ansb[k],ansc[k],ansd[k]);
}
getch();
}

The problem is

        if((start%6) == 0)
    {
        strcpy(anse[ans5],line);
        ans5++;
    }

The program shows File does not exist if i use this but as soon as i comment it out the program works fine.I don't know what the error is.Please do help

EDIT:My text file looks like:

Who is the owner
dhruv
jain
kalio
polika
dhruv
who is his friend
sarika
katrina
jen
aarushi
aarushi
where is he
home
office
college
toilet
office
where will he go
home
office
college
toilet
home

EDIT I am using Turbo c++ in windows 7 using DOSBOX..The script is updated above

6
  • Your question is not so clear. Plz elaborate more. Commented Oct 15, 2012 at 18:00
  • why is it "ans1 ans2 ans3 ans5 ans4"? Commented Oct 15, 2012 at 18:02
  • Have you run a debugger on the code to determine exactly where the error occurs? This is obviously not the complete program; perhaps the actual error is somewhere else? Commented Oct 15, 2012 at 18:08
  • @Ancurio i was testing something else.Don't worry about that Commented Oct 15, 2012 at 18:11
  • @JohnBode No, it's not..as soon as i comment out ((start%6) == 0)) everything gets fixed Commented Oct 15, 2012 at 18:11

2 Answers 2

1

It's difficult to say without seeing your input file, but I suspect that your array declarations are backwards. For example, you have:

 char ques[500][20];

This declares an array of 500 elements, where each element can be up to 20 characters. You probably want:

 char ques[20][500];

This declares an array of 20 elements, where each element can be up to 500 characters.

If your input file contains lines longer than 20 characters, then your current code is likely overwriting your arrays.

Sign up to request clarification or add additional context in comments.

9 Comments

I did what you told and rectified it but the same error was shown.
ohk..But when i even without & result is same..And he problem still lies
Include the filename in your error message and confirm that the filename is correct: printf("%s does not exist. Please enter a valid name\n", quizfile);
Assuming you've made the changes I mentioned, then I don't see anything in the code that would result in the symptoms you have reported. Perhaps you can update your original post to show the code that you are now currently using (with changes), and also include details about your platform and compiler.
Ahhh ... This is a DOS program? If that's the case, you may be trying to use too much stack space. If you add up the space consumed by all of your arrays, it comes out to around 60K. If I recall, the stack size defaults to something like 8K or 16K for Turbo C. Try declaring your arrays as static
|
0

There are several problems here but your immediate problem is this:

strcpy(anse[ans5],line);

(And all the other strcpy calls like it.)

You are copying line to the array beginning at anse[1][0]. If line contains more than 20 characters, it will overwrite memory past the end of anse. For example, if line contains 25 characters, you'll be putting it in anse[1][0] through anse[1][24]. Unfortunately anse[1][24] does not exist because anse is only 20 characters long. If any question exceeds 20 characters, you'll be corrupting memory and possibly causing a crash. Let me guess: Question 5 is longer than 19 characters, right?

In short, you have your rows and columns mixed up in your declarations. I think you wanted to allow 20 questions of 500 characters each, but you're actually allowing 500 questions of 20 characters each.

Next problem: In C, arrays are zero-based, not one-based. The first string in ques, for example, is ques[0], not ques[1].

To simplify this, think of two-dimensional arrays as a table composed of rows and columns. For example, declare a 3x4 array named foo:

char foo[3][4];

Picture it like this:

   0 1 2 3
0  . . . .
1  . . . .
2  . . . .

What I have is an array of three character strings, each 4 characters long. The first string in my array is at foo[0]. The first character of the first string is at foo[0][0]. The second character is at foo[0][1], the second character of the third string is foo[2][1], and so on.

To solve this, your declarations should look like this:

char ques[20][500],ansa[20][500],ansb[20][500],ansc[20][500],ansd[20][500],anse[20][500];
int start = 1,quesval=0,ans1=0,ans2=0,ans3=0,ans4=0,ans5=0;

When you get it working, you should then ask yourself why you're testing the value of start six times each pass through the loop when it only changes once. There's a much better solution available here. Consider a three-dimensional array like this:

char answers[20][500][5];

That gives you 20 questions with 5 answers each.

6 Comments

None of the question or answer is longer than 15 characters. I never have used 3d arrays before so i kind of prevented it.Thanks i will try to use them now as soon as my program reads my file
Whether your questions are longer than 20 or not, you still need to fix that. And I should have added that your code works for me, so are you sure you're showing us all the code?
yes..may be it's the stack size thing which cbranch told..Which compiler are you using?
You're using over 60K of stack, which is pretty huge and may not work at all on a 32-bit machine. Move your arrays to block zero (ie, outside any function). I used Visual Studio 2010, but compiler shouldn't matter.
I used Visual studio 2010 and it works fine..any reason for it to not to work in turbo c++?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.