0

my code is to show a few questions that are contained in a file when the user select the option to. But i'm facing this problem: After de program show 2 questions it always shows only the last line of my file. I don't know what is wrong.

Here is the code (only de case 1 of the switch menu)

            printf("\nThe selected subject was Geology");

                while(continue != 0)
                {
                    r=0;
                    srand(time(NULL));
                    r = rand()%7;
                    printf("\n%d", r);
                    if(r==0)
                        r=1;

                    for(i=0; i<r; i++)
                        fscanf(arqgeo, "%s %s %s %s %s %c", question, alta, altb, altc, altd, &respa);

                    printf("\n\n%s \n%s \n%s \n%s \n%s \n", question, alta, altb, altc, altd);
                    printf("Enter alternative: ");
                    setbuf(stdin,NULL);
                    scanf("%c",&resp);

                    if(arqgeo == NULL)
                    {
                        printf("An error has occurred\n");
                        printf("Contact the developers !!!\n");
                    } else {
                        if(resp == respa)
                        {
                            printf("You're right!!!\n");
                            pont++;
                            printf("\n\n\n");
                    } else {
                        puts("\a");
                        printf("You missed!!!");
                        printf(" The correct answer is: %c", respa);
                        erro++;
                        printf("\n\n\n");
                    }
                    }
                    printf("Do you wish to continue? Enter a number other than 0  ");
                    scanf("%d", &continue);
                    }
                    break;

where: "respa" is the right answer, "alta-d" is the alternatives, "resp" is the answer of the user, "arqgeo" is the file that contain the questions

--sorry for the bad english--

  • 2
    See [`srand()` — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once/) – Jonathan Leffler Nov 15 '19 at 17:47
  • 2
    Also, `setbuf(stdin,NULL);` is only effective if it executed before any I/O operation on the stream. You're using it far too late to have any effect. It also won't give you character by character input — that takes more effort to achieve. – Jonathan Leffler Nov 15 '19 at 17:48
  • 1
    Using "continue" as a variable name is a very bad idea as "continue" is a keyword in C. – Gilbert Nov 15 '19 at 20:27
  • "Contact the developers !!!" are you sure that's wise? – Neil Nov 15 '19 at 22:38

1 Answers1

0

Adding to Jonathan Leffler's and my earlier comments.

Each iteration reads some lines from the file. The first iteration works fine but the second picks up where the first left of so you blow through a lot of lines quickly.

What is needed is to rewind the file to the beginning at the start of each iteration. The fseek() function will do this for you. The trouble is you will also repeat questions if rand() % 7 returns the same number again. A static array initialized to all zeros to check if an question has already been used would be needed. Or use a bit map. rewind() is a special case of fseek() that returns to the start of the file (think old-style magnetic tapes).

Also, if(arqgeo == NULL) is wrong as the value of apqgeo does not change on troubles. Look at the ferror() and feof() functions to test eof/error conditions. scanf() also returns the count of the fields converted with EOF indicating some type of trouble where these "f" functions would be useful. At any rate returns of <= 0 indicate time to get out (EOF is usually (-1)).

While it's annoying to have to test each and every input statement for troubles it is good practice. If you don't you need to be hyper alert for behaviors caused by silent errors.

Challenge: arrange question file and and it's handling to get rid of the r == 0 test. If you have 8 questions you really don't need it.

Gilbert
  • 3,740
  • 17
  • 19