Thread: Need help; stuck with this; read file + compare with user input

  1. #16
    Registered User
    Join Date
    Jan 2007
    Posts
    13
    @citizen: thanks for the tip! i've changed this to
    Code:
    #define MAXVRAAG           30
    #define VRAGEN_COUNT  MAXVRAAG
    like you said.

    But, i've encountered another problem; i can't seem to compare the user input with a member of the array. When i run the program, it's always "Incorrect!".

    included my code;
    Code:
    {
       struct vragen quiz[100]; 
       int i;   
       static const char filename[] = "vragen.txt"; /* the name of a file to open */
       char antwoord[2];
       FILE *file = fopen(filename, "r+"); /* try to open the file */
       if ( file )
       {
          char line[BUFSIZ]; /* space to read a line into */
          int i = 0;
          while ( fgets(line, sizeof line, file) ) /* read each line */
          {
             sscanf(line, "%[^;];%[^;];%[^;];%[^;]",
             quiz[i].vraag,
             quiz[i].antw,
             quiz[i].antw1,
             quiz[i].keuze);
             i++;
             if(i == VRAGEN_COUNT) break;
          }
          fclose(file);
       }
       for(i ; i < VRAGEN_COUNT; i++)
       {
       printf("%s \n\nA: %s \nB: %s\n", quiz[i].vraag, quiz[i].antw, quiz[i].antw1);
       
       scanf("%s", antwoord);
       if(antwoord == quiz[i].keuze)
       {
          printf("Correct!\n");
          }
          else printf("Incorrect!\n");
    } 
    }

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why aren't you resetting i in your second loop? (Which incidently will be used initialized if your file opening fails.) Also, why isn't your second loop inside the same block as the check for the file? Furthermore, you don't bother checking the return value of sscanf, which would tell you if your data was valid. The way you read into the answer (antwoord) will only handle single words. Finally, you can't compare strings that way. You have to write a function to do it for you, or use the standard library functions such as strcmp.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. c script help to read a file & update
    By indy in forum C Programming
    Replies: 8
    Last Post: 12-01-2003, 11:32 AM