Thread: If operator with &&

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    23

    If operator with &&

    Hi

    Can somebody show me how to use IF operator with &&(And)

    I have a code

    Code:
    void request(FILE *in)
     {char c, mas[10]; int i,k;
       clrscr();
       printf("Enter MAS.. : "); scanf("%s",mas);
       printf("\nEnter K: "); scanf("%d",&k);
       in=fopen("text.dat","rb");
       i=1;
       fread(&t,sizeof(t),1,in);
       while (!feof(in))
       {if (strcmp(t.name,mas)==0)&&(t.price,k)==0) //so here my problem. I need to use AND here
       printf("\n %-3d\t %-10s\t %-2d\t     %-2d", i, t.name, t.price, t.ball);
       fread(&t,sizeof(t),1,in);
       i++;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Work on your indenting it's horrid.
    * There is a missing bracket (in the if statement)
    * That's poor use of the comma operator
    * Don't use feof() to control a loop.
    * You can't read/write structures as-is to disk, who's to say they're ordered the same in memory next time?

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    The code is working. But only with simple If.

    if (strcmp(t.name,mas)==0) - itsworking

    I need to use AND operator to make 2 criterias search

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No it doesn't work as far as I call it. While it works for you, it won't nessisarly work on other machines.

    You can't read/write structures as-is to disk.

    The function is poorly designed, it returns nothing, success or failure. You assume it can open "text.dat", and you assume the user won't enter more than 9 characters for the 'MAS'

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    But I can read a structure from file then by the help of if I can choose print it or not.
    I say again the code is working. May be the part of the code I've posted not working. But my problem is the proper use of if with AND

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Your probelm is larger than that, but you seem oblivious to that fact. How do you know that the condition is the problem? Not the loop? Or other other foul code?

    Code:
    if(strcmp(t.name,mas) == 0 && t.price == 0 && k == 0)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does this
    Code:
    &&(t.price,k)==0
    mean that you want to do what zacs7 is implying in his post, that is.
    Code:
    t.price == 0 && k == 0
    or
    Code:
    t.price == k
    ?

    Your code is missing many whitespaces that would make it clearer to read, including no indentation to indicate what is in which loop/conditional statement. You are also missing at least two end braces, so I'd be very surprised if this code at all compiles.

    If the file is a binary data file, why is it called "text.dat"? That is a pretty misleading name.

    What zacs7 is trying to say is that binary representation of data structures vary from one version of compiler to another version, one OS to another OS and definitely between different processor architectures.

    In my opinion, it is OK to store data in a binary format as long as you are aware that it may change if you change any of the above parameters. But you have now formally been told that this is not a great thing to do.

    Another point is: storing data in a text format is more helpful in another way: the data is then readable in a text-editor, and you can quite easily check if the data on disk is what you expect it to be.

    There are times when storing binary structures on disk is fine, but there are generally other ways to do it that are better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > You can't read/write structures as-is to disk, who's to say they're ordered the same in memory next time?
    Sure you can.
    On any given machine, with any given compiler, writing a struct to disk then reading it back again works just fine.

    Problems emerge when things change, like
    - you change compiler, then padding and alignment could be messed up.
    - you change compiler options, ditto
    - you change machine architecture, say x86 16-bit to x86 32-bit
    - you change machine, say x86 to mips, then endian problems could appear.

    Also, it's impossible to write a struct containing a pointer directly to disk, because even if everything else is identical, actual assigned addresses for variables may be different. This is especially true if you're malloc'ing the data in your structs.

    > &&(t.price,k)==0
    Maybe
    && ( t.price == k )

    Also, avoid feof() in control loops, and test the reading function for success
    Code:
       while ( fread(&t,sizeof(t),1,in) == 1) {
       }
    Has the advantage that you only write the fread() line once.
    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.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's what I meant Salem , matsp understands what I tried to say

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    That's what I meant Salem , matsp understands what I tried to say
    Yes, I understand exactly what you're saying, although I think both mine and Salem's point is that "It's OK to do this, as long as you understand what you're doing". Whilst I'm not entirely sure the original poster here decided how to implement the code above with this understanding, it is almost certainly not part of the original posters problem that the data structure is stored in a binary file (at least, I don't think so).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No worries - we're all good here
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. str_replace && str_find
    By q6z4k in forum C Programming
    Replies: 12
    Last Post: 06-01-2008, 04:03 PM
  2. problem w/ color functions && win98 :P
    By DarkMortar in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 04:45 PM
  3. AnimateWindow && Dev-C++ problems
    By willc0de4food in forum Windows Programming
    Replies: 4
    Last Post: 03-13-2006, 04:34 PM
  4. [newb] How is "!(1 && !(0 || 1))" true?
    By eddwills in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2006, 08:19 AM
  5. && or ||
    By chrismax2 in forum C++ Programming
    Replies: 4
    Last Post: 08-17-2005, 04:42 PM