Thread: confused :(

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    47

    confused :(

    This program will allow the user to input string then scans the file if it contains the same string given by the user. But i always get "MATCHED" even if i enter random string. Please help. I tried and tried to place the if statement in different positions but i dont get my expected output.

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<time.h>
    #include<string.h>
    int main()
    {
     int found;
     char str[512];
     char string[512];
     int count=0;
     char a,b;
     do
     {
    
    
     printf("ENTER STRING: ");
     scanf("%s", &string);
     FILE *fp;
     fp=fopen("JL-answers[easy].txt", "r+");
     if (fp==NULL)
     {
      printf("ERORR.\n");
      exit(1);
     }
     while(!feof(fp))
     {
      fscanf(fp, "%s", str);
      if (strcmpi(string, str)==0)
       {
        found = 1;
       }
     }
     if (found<=1)
     {
      printf("MATCHED\n");
     }
     else
     {
      printf("STRING NOT FOUND\n");
     }
     fclose(fp);
     count++;
     }
     while(count<=15);
    
    
        
     return 0; 
    
    
    }

  2. #2
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Why "found" uninitialized? Why it is compared in such strange way: <= 1?
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    I did this.

    int found=0;

    (found=1)

    But still, i dont get my expected result

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Pick an initial value where this isn't true, regardless of what your code does.
    > if (found<=1)

    Better yet, try == 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Code:
    while( fscanf(fp, "%s", str) == 1 ) {
        if( strcmpi(string, str) == 0 ) {
            found = 1;
            break;
        }
    }
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Fact - Beethoven wrote his first symphony in C

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Note that strcmpi is non-standard.

    Since you declared str to be an array of 512 characters, it can store a string with a length of at most 511. Therefore, you should specify 511 as the field width when reading with fscanf and %s, e.g.,
    Code:
    while( fscanf(fp, "%511s", str) == 1 ) {
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confused
    By luke luvevou in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2009, 09:14 AM
  2. this is fun but Im new and confused
    By c-hopper in forum C Programming
    Replies: 48
    Last Post: 10-21-2008, 10:05 PM
  3. confused
    By mopar123 in forum C Programming
    Replies: 18
    Last Post: 09-01-2005, 04:06 PM
  4. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 05:41 PM
  5. im so confused???
    By Flim Flam in forum C Programming
    Replies: 7
    Last Post: 07-01-2003, 07:35 AM