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; }
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]
>if(mess=="salir") break;
Is wrong. You are comparing the addresses and not the contents at those addresses. Try this instead:
Never use gets() [unless you are confident about no buffer overflow]. To learn why, search the forums.Code:if(strcmp(mess, "salir") == 0) break;
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]
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]
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!
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]
>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.
Please do not confuse people with such posts .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.