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

From: "Pieter van Ginkel" <pginkel AT westbrabant DOT net>
To: "shifter" <shifter AT sainet DOT net>, <djgpp AT delorie DOT com>
Subject: Re: why don't this work
Date: Sat, 28 Feb 1998 13:15:08 +0100
Message-ID: <01bd4442$82cf5cc0$LocalHost@PIETER>
MIME-Version: 1.0

What your are trying to do when you do "gets(i) != password" is comparing to
pointers. A char * is a pointer to a string, so in the variable password is
a pointer stored (nothing like pascal or basic at all!). What you need to do
is use the string library (string.h) to compare, copy, search and do all
kinds of kewl stuff with strings. In this case you need to use the following
thing: "strcmp( gets(i), pasword )==0" With the ==0 you want the two strings
to be the same. Use >0 and gets(i) must be bigger, use <0 for the opposite
and != 0 if you don't want them to be the same. Another thing you do wrong
and with which you'll have loads of problems later is that you don't reserve
memory for the strings. Pasword points to a pointer but that pointer doesn't
point anywhere. The problem you should get is SIGSEGV, but you'll find that
out later. You reserve memory with the following statement:

#define MEMORY_YOU_WANT 20

pasword = (char *)malloc( MEMORY_YOU_WANT + 1 );

You have to reserve 1 byte more memory that the total memory that you want,
because in C all the strings and with a 0 so anyone can see where it ends.
You also must free the memory at the end of the program with free( pasword).
You need to put (char *) between the malloc and the = because malloc gives a
void * and c doesn't understand how you want to make a char * out of a void
*. If you want to put a string into toe allocated memory you have to use
strcpy( pasword, "mypasword" ). Be sure you don't use #define ...
everywhere, this is just a example.

The last thing I want to tell you is this. GET A TUTORIAL AND START LEARNING
C!!!!!!!!!!!!!!!!!!!!1

You 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