function

B

Bill Cunningham

This is my new code. The same problem. No exits and I always get error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main(){
char pass[10];
printf("Authorization ");
fflush(stdout);
fgets(pass,10,stdin);
if(strcmp(pass,"ded")==0)
printf("success");
else printf("error");}


Bill
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bill Cunningham wrote:
| This is my new code. The same problem. No exits and I always get error.
|
| #include <stdio.h>
| #include <stdlib.h>
| #include <string.h>
|
| main(){
| char pass[10];
| printf("Authorization ");
| fflush(stdout);
| fgets(pass,10,stdin);

Quote:
~ The fgets function reads at most one less than the number of characters
~ specified by /n/ from the stream pointed to by /stream/ into the array
~ pointed to by /s/. No additional characters are read after a new-line
~ character (which is retained) or after end-of-file. A null character is
~ written immediately after the last character read into the array.

| if(strcmp(pass,"ded")==0)

Are you certain that the array pass[] contains /exactly/ 'd', 'e', 'd', 0?
Is it conceivable that the array contains something else? Specifically, that
it might contain an additional character before the terminating null
character? Reread the description of the fgets() function; there is a clue in
there. Run the program again, and make note of /exactly/ what you type when
you get the unexpected 'error' answer. Are you certain that you /only/ typed
the 'd', 'e', and 'd', keys? Or did you perhaps type something else in after
that final 'd'? What was it? Does it fit with the description of fgets()? What
can you do in the strcmp() call to accomodate it?


| printf("success");
| else printf("error");}
|
|
| Bill
|
|


- --
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
- ---------- Slackware - Because I know what I'm doing. ------


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Armoured with GnuPG

iD8DBQFHyeBZagVFX4UWr64RAutYAKCHs72pw2oRugct0tXj7jyFkrbn4ACg6dsq
xFi8WcN3/Ze7vLhoukqi6nw=
=qAjm
-----END PGP SIGNATURE-----
 
P

pete

Bill said:
This is my new code. The same problem. No exits and I always get error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main(){
char pass[10];
printf("Authorization ");
fflush(stdout);
fgets(pass,10,stdin);
if(strcmp(pass,"ded")==0)
printf("success");
else printf("error");}

fgets leaves the newline character in place
and does not give you a string.

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
char pass[10];
char *p;

printf("Authorization ");
fflush(stdout);
fgets(pass, 10, stdin);
p = memchr(pass, '\n', 10);
if (p != NULL) {
*p = '\0';
if (strcmp(pass, "ded") == 0) {
puts("success");
} else {
puts("error");
}
}
return 0;
}

/* END new.c */
 
B

Bill Cunningham

news:5b777$47c9ddac$cef8bf78> Are you certain that the array pass[] contains
/exactly/ 'd', 'e', 'd', 0?
Is it conceivable that the array contains something else? Specifically,
that
it might contain an additional character before the terminating null
character? Reread the description of the fgets() function; there is a clue
in
there. Run the program again, and make note of /exactly/ what you type
when
you get the unexpected 'error' answer. Are you certain that you /only/
typed
the 'd', 'e', and 'd', keys? Or did you perhaps type something else in
after
that final 'd'? What was it? Does it fit with the description of fgets()?
What
can you do in the strcmp() call to accomodate it?

I am certain that ded is all I type. Is it possible that '\0' is needed.
I'll check fgets.

Bill
 
M

Mark McIntyre

Bill said:
I have been having fits with this function.
Can anyone help me I think I need to use if and else if but I have rewritten
it and got errors. I want a conditional in this program a choice between 2
options and only one works. I don't think I need the strcpy either.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main(){
char pass[10]="ded", pass2[10];
printf("Authorization ");
fflush(stdout);
fgets(pass,10,stdin);
strcpy(pass2,pass);
if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}
else
printf("error"); exit(EXIT_FAILURE);}

When you take on board all the tips on style, formatting and making your
code legible that dozens of people have previously given you, you will
be more likely to get some assisstance.
 
I

Ian Collins

Mark said:
When you take on board all the tips on style, formatting and making your
code legible that dozens of people have previously given you, you will
be more likely to get some assisstance.

I've never known a troll to do that, have you?
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bill Cunningham wrote:
| news:5b777$47c9ddac$cef8bf78> Are you certain that the array pass[] contains
| /exactly/ 'd', 'e', 'd', 0?
|> Is it conceivable that the array contains something else? Specifically,
|> that
|> it might contain an additional character before the terminating null
|> character? Reread the description of the fgets() function; there is a clue
|> in
|> there. Run the program again, and make note of /exactly/ what you type
|> when
|> you get the unexpected 'error' answer. Are you certain that you /only/
|> typed
|> the 'd', 'e', and 'd', keys? Or did you perhaps type something else in
|> after
|> that final 'd'? What was it? Does it fit with the description of fgets()?
|> What
|> can you do in the strcmp() call to accomodate it?
|
| I am certain that ded is all I type. Is it possible that '\0' is needed.
| I'll check fgets.

OK, I'll make it plainer: You typed
~ d
~ e
~ d
~ [Enter]

That translated to
~ 'd', 'e', 'd', '\n', 0
when you executed fgets().

So, your pass[] array contains:
~ pass[0] == 'd'
~ pass[1] == 'e'
~ pass[2] == 'd'
~ pass[3] == '\n'
~ pass[4] == 0

and when you try to compare this to "ded", you fail because
~ 'd', 'e', 'd', 0 does not match to 'd', 'e', 'd', '\n', 0



- --
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
- ---------- Slackware - Because I know what I'm doing. ------


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Armoured with GnuPG

iD8DBQFHygHAagVFX4UWr64RApOxAKDOlNjPFSViKNhUtnkk46TVknr4gwCghKfk
LQi1w7gHkJhmVK1gIIDA1sw=
=+rFQ
-----END PGP SIGNATURE-----
 
C

CBFalconer

Bill said:
This is my new code. The same problem. No exits and I always get
error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main(){ try: int main(void) {
char pass[10];
printf("Authorization ");
fflush(stdout);
fgets(pass,10,stdin);
if(strcmp(pass,"ded")==0)
try: if (0 == strcmp(pass, "ded\n"))
printf("success");
else printf("error");}
add: putchar('\n');
return 0;
}
 
C

CBFalconer

Ian said:
I've never known a troll to do that, have you?

Cunningham isn't a troll. He has a mental impairment, and is
making valiant efforts to learn something through it. He has been
around for years, and comes and goes.
 
D

Default User

CBFalconer said:
Cunningham isn't a troll.

Maybe, maybe not.
He has a mental impairment

He SAYS he has a mental impairment.
, and is
making valiant efforts to learn something through it.

Maybe, maybe it's a "long con" troll.
He has been
around for years, and comes and goes.

This part is true. He has supposedly been studying C since 2002. With
no measurable progress, and no desire to follow sensible procedures for
learning (like working through a text with exercises or looking
functions before trying to use them).

I killfiled him after one last attempt to get him to work
systematically on one problem at a time. He ignored me.

Whether he's trolling or incapable of doing so doesn't matter. He can
do what he's doing for another six years, or sixty years, and won't
ever learn C.

If he legitimately has a problem this severe, he needs to move on to
another endeavour. Programming is not for him.




Brian
 
A

Antoninus Twink

CBFalconer wrote:
Maybe, maybe not.


He SAYS he has a mental impairment.

I killfiled him after one last attempt to get him to work
systematically on one problem at a time. He ignored me.

He sounds perfectly sane to me.
 
P

pete

Richard said:
pete said:



Right (if it fits)...


...but wrong.
I'm not sure what you were trying to say here but, whatever
it was, you didn't manage it.

Yes, I blew it.
It's the remaining newline near the end of the string
which (pass) points to, that causes

strcmp(pass, "ded")

to not be equal to zero,
even when "ded" is entered from standard input.
 
K

Keith Thompson

Bill Cunningham said:
This is my new code. The same problem. No exits and I always get error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main(){
char pass[10];
printf("Authorization ");
fflush(stdout);
fgets(pass,10,stdin);
if(strcmp(pass,"ded")==0)
printf("success");
else printf("error");}

One more time: the way you format your code makes it very difficult to
read. You need to use more whitespace.

One *last* time: here's what your code would look with reasonable
formatting:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
char pass[10];
printf("Authorization ");
fflush(stdout);
fgets(pass, 10, stdin);
if (strcmp(pass, "ded") == 0)
printf("success");
else
printf("error");
}

The layout isn't the only thing you need to change, but it's the
*first* thing you need to change. In particular, *please* stop hiding
the '}' character at the end of the line.

If you'll do this one simple thing, I'll be willing to offer more
help. If you don't, I won't.
 
S

santosh

Bill said:
This is my new code. The same problem. No exits and I always get
error.

<snip>

Don't take this as an insult, but since it seems you suffer from
attention and memory related difficulties, why don't you try a language
that automates more things for you like, say, Python or Java or Ruby
etc.? C is fairly low level and demands rigorous attention to details.

In fact this latest post of yours is a good example. Languages like Java
let you directly compare two strings, while you must manually do this
in C.

Just a suggestion.
 
K

Kenny McCormack

He sounds perfectly sane to me.

But I think it is perfectly clear that Default Loser is mentally impaired.
Initially, I assumed that that was what we were discussing here.
 
C

CBFalconer

.... snip about Cunningham trolling ...
If he legitimately has a problem this severe, he needs to move on
to another endeavour. Programming is not for him.

Maybe so. But I am not willing to kick him in the face. He does
no harm.
 
K

Kenny McCormack

Karthigan Srinivasan said:
This should work.

gets(pass2);

This is a really, really, reall[y] bad idea. Never, *ever* use gets(),

Surely you exagerate. I mean, come on. Mixing primaries during
daylight hours is a bad idea. Slitting one's wrists is a really bad
idea (it stains the carpet). Invading Iraq *was* a really, really,
really bad idea.

By comparison, I think using gets() is pretty innocuous.
 
K

Kenny McCormack

Surely you exagerate. "dozens", to me, means at least 24.

I'd say the number of people who've commented significantly in this
thread is about 5, maybe 10.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top