Thread: Why this wont work??

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Why this wont work??

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    main(void)
    {
      char mess[80];
      int i;
      
      while(1){
        printf("Mensaje:");
        gets(mess);
      
       if(mess=="salir") break;
      }
      
      return 0;
    }
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >if(mess=="salir") break;
    Is wrong. You are comparing the addresses and not the contents at those addresses. Try this instead:
    Code:
    if(strcmp(mess, "salir") == 0) break;
    Never use gets() [unless you are confident about no buffer overflow]. To learn why, search the forums.

  3. #3
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    isn't main suppose to be int main?
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    ?

    What do you mean i'm compairing the addresses??

    if(mess=="salir") is equal to if(&mess[0]=="salir")?.

    Please explain!!
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    isn't main suppose to be int main?
    --------

    no, because all names and variables is all with the "label" int before them.
    It's standard in the compilers understanding of the language.

    It only if you need something else like char, float, double etc, that you need to call the varibles with char, float etc.
    !G!

  6. #6
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    you cannot compare strings like that you have to use strcmp.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It's standard in the compilers understanding of the language.
    Not anymore. The new C standard removed the implicit int feature, so you'd best get into the habit of putting int in front of your identifiers before the new standard is fully adopted into compilers.

    >What do you mean i'm compairing the addresses??
    The name of an array is a pointer to the address of its first element. Likewise, a string literal is a pointer to the memory location where it begins. So

    if(mess=="salir")

    is equivalent to

    if(0x1FA==0x2AC) /* These addresses are fake by the way */

    Since the addresses are not the same, the test will fail. That's why you need to use strcmp, which compares the actual values of both strings instead of the addresses.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Gugge
    isn't main suppose to be int main?
    --------

    no, because all names and variables is all with the "label" int before them.
    It's standard in the compilers understanding of the language.

    It only if you need something else like char, float, double etc, that you need to call the varibles with char, float etc.
    Please do not confuse people with such posts .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM