Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Need help with C

Options
  • 23-04-2001 10:03pm
    #1
    Closed Accounts Posts: 58 ✭✭


    Sort of stuck here with this

    if((int)strcmp(compare_strings,inputed_array_of_strings+counter)=0)
    {
    reply=1;
    my compilier keeps giving the error

    invalid lvalue in assignment.

    anyone know what wrong with the code cause i have to hand in the assignment tomorrow


Comments

  • Registered Users Posts: 310 ✭✭Cerberus


    Your're gonna kick yourself reeeeal good....
    You have only one equals sign in the if statement.
    You are assigning the whole strcmp function the value 0 which you can't do. You need another equals in there.
    By the way, why are you casting strcmp() to an int? There is no need for it.
    Just put the strcmp() function in the
    if-statement condition list on its own. If the two strings are the same then the strcmp() function will return true and the if-statement will also evaulate to true.


  • Closed Accounts Posts: 58 ✭✭decdoc


    thanks Cerberus
    i was just trying casting it cause i wasn't sure what the error meant and was trying anything
    it's always the simple thing's that get me


  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Hehe, reminds me of a project my head was wrecked over for days; turned out I had misspelt some function or other.


  • Registered Users Posts: 2,660 ✭✭✭Baz_


    If the two strings passed to the strcmp() function are the same then the function returns 0 which in an if statement is false, if they aren't the same it returns non-zero which in an if statement is true.

    to get an equivalent if statement for if the strings are the same the following should work:
    if(!strcmp(str1,str2))
    {
      /*statements to be executed when strings are identical*/
    }
    else
    {
      /*statements to be executed when strings are not identical*/
    }
    
    

    Hope that helps.

    Baz_


  • Registered Users Posts: 1,004 ✭✭✭Lord Khan


    baz strcmp returns 3 possible values

    0 if it is a match
    postive if first comes after second
    negative if seconds comes after the first


  • Advertisement
  • Registered Users Posts: 2,660 ✭✭✭Baz_


    Thats what I said, wasn't it, I mean non-zero is either greather than or less than but not equal to zero.

    He never said whether or not he wanted to deal with different types of cases of not equal to so I didn't bother giving him the details.


  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    Although this doesn't apply directly to your problem, you can run in to a nasty runtime errors if you use an if (x = constant) test a lot in your code...

    A handy way to ensure you avoid this one again is to always put the constant on the right in the test.

    if ( 0 == somevar ) {}

    Irrelevant but useful smile.gif

    Al.


  • Registered Users Posts: 2,199 ✭✭✭Keeks


    <font face="Verdana, Arial" size="2">Originally posted by Trojan:
    you can run in to a nasty runtime errors if you use an if (x = constant) test a lot in your code...
    </font>

    How does this happen?



  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    <font face="Verdana, Arial" size="2">Originally posted by Keeks:
    How does this happen?
    </font>

    Are you just taking the ****? Hmm, anyways ...

    If you want to test if a equals 0 you may do

    if (a = 0) {}

    instead of

    if (a == 0) {}

    You will not get a compiler error as it's valid code, whereas if (0 = a) {} is invalid and will give a compiler error...


    Al.




    [This message has been edited by Trojan (edited 25-04-2001).]


  • Registered Users Posts: 310 ✭✭Cerberus


    Oh yeah, you're right baz_. It returns 0 if they are the same.
    I got me return values mixed up. frown.gif
    bad buzz.



  • Advertisement
  • Registered Users Posts: 897 ✭✭✭Greenbean


    If you really wanna p1ss someone off who is doing a c++ program, just look for one of their header files - and stick a "z" or any character in front of all their code, just before the #if-def style statements. This can cause the effect of seemingly impossible nonsense errors, absolutely out of nowhere, which don't point to the file with the problem.

    Did this by accident to myself and set things back 2 days as I tried to research all these weird compiler errors.


  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    <font face="Verdana, Arial" size="2">Originally posted by Greenbean:

    This can cause the effect of seemingly impossible nonsense errors, absolutely out of nowhere, which don't point to the file with the problem.
    </font>

    That's really nasty GB, no wonder you get in so many fights!

    But you should use an invisible character (like this one: ' ').

    Al.


Advertisement