Mail Archives: djgpp/1998/07/02/01:45:15
From: | sparhawk AT eunet DOT at (Gerhard Gruber)
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: Simple password program
|
Date: | Wed, 01 Jul 1998 21:07:38 GMT
|
Organization: | Customer of EUnet Austria
|
Lines: | 68
|
Message-ID: | <35a15d4a.3335473@news.Austria.EU.net>
|
References: | <3599C945 DOT 3AC1F40B AT geocities DOT com>
|
NNTP-Posting-Host: | e205.dynamic.vienna.at.eu.net
|
Mime-Version: | 1.0
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Destination: Fozzy the Bear <fozzy99 AT geocities DOT com>
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: Tue, 30 Jun 1998 22:29:42 -0700:
>Hello I am new to C but have learned alot so far, I tried to make a
>simple password program, but it didn't turn out to be so simple. I
>can't get it to work. I get this error:
> password.c: In function `main':
> password.c:22: invalid lvalue in assignment
>please help me figure out what is wrong.
>
>---------------------------start
>here------------------------------------
>
>#include <stdio.h>
>
>main()
>{
>char name[30],password[50]; /* enough ?? */
>
> printf("login: ");
> scanf("%s",name);
I would recommend using fgets() and sscanf() instead of scanf(). scanf will
crash when the user types in a longer string than 30 characters because it
doesn't know the length of the memory where the string is assigned to.
> printf("password: ");
> scanf("%s",password);
>
> if(&name="fozzy") /* ,&password==bear */
You are comparing the memory adress from name with that from the constant
string "fozzy". Since these are two different memory locations this if will
never trigger. Also in C you have to use == to make a comparison. The
statement you wrote here takes the compiler as
assign the adress of the string "fozzy" to the adress of name and check if the
result (from the assignment) is 0 or not 0. This is why the if will never
trigger, since the adresses of the two strings are never zero the else branch
will never be activated.
You have to use a strcmp(name, "fozzy") in order to compare the strings.
> {
> printf("Welcome %s\n");
> }
> else
> {
> print("You Do Not Have authorisation To Use This Computer!");
> print("This Attempt Will Be Logged When I Learn How");
:)
> }
>
> /* return 0; */
>}
You should return(0) here when exiting from main.
--
Bye,
Gerhard
email: sparhawk AT eunet DOT at
g DOT gruber AT sis DOT co DOT at
Spelling corrections are appreciated.
- Raw text -