Thread: Array in if condition.

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    Array in if condition.

    Hi,

    Sorry if this seems insignificant but I am not very good. I can't get this code to work:

    Code:
    for(i=0; i<=10; ++i){
                            if (hand[i]==card){
                            x=1;
                            }
                            else{
                            x=0;
                            }
     }
    I have got the program to print the values of the variables, and everything is as I would expect. The program runs through the loop and the if statement, but refuses to recognize when hand[i]==higher_card, even though it can print them out, and they are clearly the same. All of the variables are integers. Can anyone help?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There is nothing inherrently wrong with the code posted.
    You'll need to show more.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    That's the only code in this function. The function is passed hand and card, but since it can print these out correctly I figured there was nothing wrong with how I passed them. The elements of hand are read in from a file (they are definitely integers though). Do you think there might be some sort of space or end of line character or something that's the problem?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How is card defined? How is hand defined? Please show a complete program that illustrates your problem.

    Jim

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    The whole program is to big to show, I'll try and find what's relevant and post it as soon as I can. Thanks for your help with this.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Aha, I've just found where I've gone wrong. The whole thing was immersed in if statements and loops, and I wasn't following it properly. Thank you for helping I feel a bit embarrassed now....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. condition in in array(for loope)
    By i6472 in forum C Programming
    Replies: 2
    Last Post: 05-01-2010, 05:31 PM
  2. array insertion with condition
    By joh23 in forum C Programming
    Replies: 7
    Last Post: 02-12-2005, 07:31 PM