Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function

Reply
Thread Tools

function

 
 
Bill Cunningham
Guest
Posts: n/a
 
      03-01-2008
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


 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      03-01-2008
-----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

iD8DBQFHyeBZagVFX4UWr64RAutYAKCHs72pw2oRugct0tXj7j yFkrbn4ACg6dsq
xFi8WcN3/Ze7vLhoukqi6nw=
=qAjm
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      03-01-2008
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);
> 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 */


--
pete
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-02-2008
pete said:

<snip>

> fgets leaves the newline character in place


Right (if it fits)...

> and does not give you a string.


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

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      03-02-2008

"Lew Pitcher" <(E-Mail Removed)> wrote in message
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


 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      03-02-2008
Bill Cunningham wrote:
> 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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-02-2008
Mark McIntyre wrote:
>
> 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?

--
Ian Collins.
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      03-02-2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bill Cunningham wrote:
| "Lew Pitcher" <(E-Mail Removed)> wrote in message
| 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

iD8DBQFHygHAagVFX4UWr64RApOxAKDOlNjPFSViKNhUtnkk46 TVknr4gwCghKfk
LQi1w7gHkJhmVK1gIIDA1sw=
=+rFQ
-----END PGP SIGNATURE-----
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-02-2008
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(){

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;
}

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-02-2008
Ian Collins wrote:
> Mark McIntyre wrote:
>>
>> 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?


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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments