Thread: If and get; really short post, help!

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    If and get; really short post, help!

    This is my program:

    Code:
    #include <stdio.h>
    
    void main()
    {
    	char love[20];
    
    
    	printf("Insert the name of the person that you love. \n");
    	gets(love);
    	
    	if(love == "Lisa") 
    	{	 printf("\n Wonderful, because %s loves you too!", love);
    	}
    	else if(love == "Alex") 
    	{	printf("\n Wonderful, because %s loves you too!", love);       
    	}
    	else 
    	{	printf("\n Error! \n You do not love %s.", love);
    	}
    	
    }
    However, when run on Quincy 2000 I receive errors, and my first two "If" statements do not work. Everytime I type in a name, it automatically goes to the third if statement.
    Also, my error says something about "the return value for main is not int". But I don't have a return value, nor do I have any integers.

    What is the problem?
    Thank you.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you have to call the function strcmp() to compare two strings.
    Code:
    if( strcmp(love,"Lisa") == 0)
    {
       // the strings are equal
    }

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    main needs to return int, not void, that is why you get that error.
    Last edited by cwr; 10-24-2005 at 11:14 AM. Reason: Remove redundant content.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You have to understand what is happening in your program and why that won't work.

    Code:
    if(love == "Lisa")
    The above is NOT how you do string comparisons in C. You need to be using the strcmp function in the <string.h> header. What is happening above is that the program is comparing to see if the address of the love array is equal to the address of the string literal "Lisa". Those two addresses will NEVER be equal. Neither will the address of the love array ever be equal to the string literal "Alex".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Never heard of Quincy 2000 but maybe your code should look like this:
    Code:
    #include <stdio.h>
    #include <string.h> //  Needed for stricmp
    void main()
    {
    	char love[20] = {0};
    	printf("Insert the name of the person that you love. \n");
    	gets(love);
    	
    	if(stricmp(love,"Lisa") == 0) 
    	{
    	printf("\n Wonderful, because %s loves you too!", love);
    	}
    	else 	if(stricmp(love,"Alex") == 0)  
    	{
    	printf("\n Wonderful, because %s loves you too!", love);       
    	}
    	else 
    	{
    	printf("\n Error! \n You do not love %s.", love);
    	}
    }

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    There is no such standard function as stricmp. And again, main needs to return int, not void.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    And never ever use gets(). It's not safe. Use fgets() instead.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Yep, you're right about gets and main return but stricmp is the Win32 version of strcasecmp in the Linux and *nix world.

    Been using it for years in all sorts of Win32 projects, hasn't failed me yet.

    Bob

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	char love[20] = {0};
    	printf("Insert the name of the person that you love: \n-> ");
    	gets(love);
    	
    	if(!strcmp(love,"Lisa")) 
    	{
    	printf("\n Wonderful, because %s loves you too!", love);
    	}
    	else 	if(!strcmp(love,"Alex"))  
    	{
    	printf("\n Wonderful, because %s loves you too!", love);       
    	}
    	else 
    	{
    	printf("\n Error! \n You do not love %s.", love);
    	}
    	gets(love);
    	return 0;
    }
    As its a int you need to put a return 0; at the end. Also the gets before the return will stop thw window closing before you get to read the ouput.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Ugh! Please don't ever encourage the use of gets(). Did you not read my previous post in this same thread?
    If you understand what you're doing, you're not learning anything.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BobS0327
    Yep, you're right about gets and main return but stricmp is the Win32 version of strcasecmp in the Linux and *nix world.

    Been using it for years in all sorts of Win32 projects, hasn't failed me yet.

    Bob
    Hence the phrase:
    Quote Originally Posted by cwr
    There is no such standard function as stricmp.
    The standard refers to the ANSI C standard. That is to say the "rules" for the C language itself. Anything that isn't standard is either undefined, or implementation specific. AKA: Not standard.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Ansi C standards bring back fond memories of writing DOS console apps. Being a contract programmer, I just can't remember the last time a potential employer asked me if I was knowledgeable in ANSI C standards. Nowadays, skillset requirements are Windows GUI, COM, DCOM, Graphics/Animation etc. There is now a serious demand developing for C# programmers. And the only C# standard is the Bill Gates standard.

    All of the high end stuff C stuff I worked on would never meet ANSI C standards.

    The point I'm trying to make is that standards are nice but I wouldn't get in a tither over them.

    BTW I have K&R's The C Programming Lanuage. It's been superceded by Petzold on Windows, Russinovich on Windows Internals, Hart on Systems Programming etc.

    Bob

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    we are in the presence of a contract programmer, I am unworthy.

    -seriously no sarcasm intended.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BobS0327
    The point I'm trying to make is that standards are nice but I wouldn't get in a tither over them.
    The reason we harp on standards so much here, is because we're usually dealing with people who don't understand programming at all, or very well. They don't understand why when they put the line:
    Code:
    if( strcmpi( foo, bar ) )
    on one computer it compiles, but another it doesn't. Therefore we tell them to use the standard variant. You see?

    You know what your compiler has, and you know what you can use. Most people don't. Thus, it is so much easier to teach them how to do things according to the standard, so they know what the language defines, and how it works. Rather than spend hours explaing to many many people why this works here, but not there; why they can use getch some places but not others. And the like.

    You want fun? "Hey n00b, you have some of these. Have fun!"

    getc
    getch
    getche
    getchar

    "They all read a key."



    Quzah.
    Last edited by quzah; 10-24-2005 at 06:13 PM. Reason: Added "getc*" fun.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by quzah
    You know what your compiler has, and you know what you can use. Most people don't. Thus, it is so much easier to teach them how to do things according to the standard, so they know what the language defines, and how it works. Rather than spend hours explaing to many many people why this works here, but not there; why they can use getch some places but not others. And the like.
    And educational institutions are one of the biggest offenders. I can't count the number of times students are required to learn programming with 15-year-old, obsolete compilers such as Tucbo C, then the students can't understand why the functions in conio.h and dos.h don't work with modern 32-bit compilers!

Popular pages Recent additions subscribe to a feed