www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/28/03:31:13

From: Chris Doty <orz AT atticus DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: why don't this work
Date: Sat, 28 Feb 1998 03:08:52 -0800
Organization: Worcester Polytechnic Institute
Lines: 33
Message-ID: <34F7F044.7F52@atticus.com>
References: <34F79C17 DOT 71DC AT sainet DOT net>
NNTP-Posting-Host: orz.res.wpi.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

You're comparing the address of the strings, not the content of the
strings.  Use strcmp.
Replace this line:
   }while (gets(i) != password);
with this:
   }while (!strcmp(gets(i), password));
In C strings are arrays of characters, and arrays are pointers.  Net
result:  strings are pointers.  If you say string1 = string2; and then
say string1[0] = 'a'; you'll find that string2s first character is now
also 'a'.
shifter wrote:
> 
> #include <stdio.h>
> 
> main()
> {
> char *i;
> char *password;
> 
>    clrscr();
>    printf ("Input a password:");
>    gets(password);
>    printf("you typed:%s\n",password);
>    do{
>    printf ("Input a password:%s:",i);
>    }while (gets(i) != password);
>    printf("you got it!!!");
>    exit(0);
> }
> 
> i'm trying to get it to break out of loop with the correct password
> what am i doing wrong?
> ralph <shifter AT sainet DOT net>

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019