Mail Archives: djgpp/1998/05/22/17:16:30
Message-ID: | <3565E7E9.8C3045E1@imailbox.com>
|
From: | "Robert C. Paulsen, Jr." <paulsen AT ZAP DOT imailbox DOT com>
|
Organization: | .
|
MIME-Version: | 1.0
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: "Problem with a text string"
|
References: | <6k4lhm$6ne$1 AT talia DOT mad DOT ibernet DOT es>
|
Lines: | 26
|
Date: | Fri, 22 May 1998 21:02:43 GMT
|
NNTP-Posting-Host: | 209.99.40.70
|
NNTP-Posting-Date: | Fri, 22 May 1998 16:02:43 CDT
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
JOA wrote:
>
> Hello, the program:
> #include <stdio.h>
> char name[];
> main()
> {
> printf ("Write your name:");
> scanf ("%s",&name);
> if (nombre=="John") printf ("hello John");
> else printf ("Error");
> }
>
> Always write "Error" if i write "John", but never write "hello John". What
> can i do?
C and C++ do not support comparisons of character arrays with the ==
operator, nor with any of the comparison operators. Instead, use the
strcmp() function:
============================================
#include <stdio.h>
#include <string.h>
char name[80]; // <- note the "80"
main()
{
printf ("Write your name:");
scanf ("%s",&name);
if ( strcmp(name,"John") == 0 ) printf ("hello John");
else printf ("Error");
}
To ignore case sensitivity, use stricmp().
--
Robert Paulsen ICQ 11815732 http://paulsen.home.texas.net
If my return address contains "ZAP." please remove it. Sorry for the
inconvenience but the unsolicited email is getting out of control.
- Raw text -