function

M

Morris Dovey

Bill said:
Ok. My goof. But if I wrote this.

if (strcmp(pass,pass2)==0) {printf("success");}
else printf("error");

I can't figure out if and if else. I might need a little help with
conditional statements involving 2 possbilities.

I'm assuming that you intended for the password to be "ded"...

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

int main(void)
{ char pass[10]="ded", pass2[10];

printf("Authorization ");
fflush(stdout);
fgets(pass2,10,stdin);
if(strcmp(pass,pass2))
{ printf("error");
exit(EXIT_FAILURE);
}
printf("success");
return 0;
}

If you terminate the program immediately for an incorrect
password, you won't need an 'else' for the success path.
 
B

Bill Cunningham

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

Ian Collins

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);}
As that you best you can do? I don't think anyone is going to bite this
time.
 
B

Bill Cunningham

Ian Collins said:
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);}
As that you best you can do? I don't think anyone is going to bite this
time.

Guess so; maybe I can figure it out myself. I hope this doesn't bait
trolls but I'm sure it's a if else question. Only talk C please.

Bill
 
W

Walter Roberson

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

As you have initialized pass, my guess is that you would want
to fgets into pass2 .
strcpy(pass2,pass);
if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}

Because of the strcpy, the two variables are going to contain
the same thing, so the comparison will always work.
 
B

Bill Cunningham

Ok. My goof. But if I wrote this.

if (strcmp(pass,pass2)==0) {printf("success");}
else printf("error");

I can't figure out if and if else. I might need a little help with
conditional statements involving 2 possbilities.

Bill
 
W

Walter Roberson

Ok. My goof. But if I wrote this.
if (strcmp(pass,pass2)==0) {printf("success");}
else printf("error");
I can't figure out if and if else.

I'm not sure what the rest of your program looks like now.
I might need a little help with
conditional statements involving 2 possbilities.

The portion inside the () after the 'if' is evaluated
as an expression. If the expression evaluates to non-zero,
the 'if' succeeds and the first statement afterwards
(which might be a compound statement) is executed; if the
expression evalutes to 0, the 'if' fails and the first statement
after the 'else' (which might be a compound statement) is executed
[if there is an 'else'].

If you have only been able to trigger one of the cases, then
it could be that the code above the 'if' is wrong, or it could
be that the expression to be evaluated is wrong. I can't say
which at the moment as we don't know your current code.
 
B

Bill Cunningham

[snip]
If you have only been able to trigger one of the cases, then
it could be that the code above the 'if' is wrong, or it could
be that the expression to be evaluated is wrong. I can't say
which at the moment as we don't know your current code.

Ok thanks I will work on it more and come back if I still can't get it
right.

Bill
 
B

Bill Cunningham

Walter does an if statement between () have to be an arithmetic
statement? I am thinking I am trying to test if the value of the char type
pass is "ded" or not. A string. So I am thinking strcmp() but if a string is
"ded I want to branch to success. If the value of pass isn't "ded" I want to
branch to something else. A failure. I am using fgets() to direct stdin.

Bill
 
B

Bill Cunningham

Might I have to change type with one of the data type changing functions
to change char pass[10] to an arithmetic type to do a comparison? The string
"ded" either exists or doesn't and if needs to evaluate that. "Pass by
value." someone once told me. So many hints that a mathematician designed
this language.

Bill
 
W

Walter Roberson

Walter does an if statement between () have to be an arithmetic
statement?

The part in the () in an 'if' statement must be an expression.
Every expression has a value (by definition.) The value that
results from the expression will be compared to 0, and the
first statement will be chosen if it is -not- 0 and the 'else'
statement (if any) will be chosen if the expression -is- 0.

If you code the expression with one of the logical operators,
such as if (x > 3) then it is fairly obvious what the
logical result of the expression is -- it will be either true or false.
The logical operators all return 1 to mean true and 0 to mean false.

If you code the expression with an arithmetic value but no logical
operator, then remember the implicit comparison to 0. For example
if you code if (x) then it is the same as if (x != 0).
This is the case whether x is some kind of integer or some kind
of floating point value -- it is either 0 or it isn't.

The above two paragraphs might sound like two different rules, but
they are really the same rule: does the result of the expression
equal 0 or does it not? The fact that a logical operator
happens to produce 1 for true does not matter to C: what is important
to C's 'if' statements is that the result is not 0.

What is tested in the 'if' statement can also be a pointer of
some kind. This involves a special case: when x is a pointer,
if (x) is considered true if the pointer is not NULL, and
considered false if the pointer -is- NULL. In many systems,
NULL -is- 0 internally, but that is not true in all implementations;
whatever internal value or values that the implementation uses
to represent NULL, the implementation is responsible for testing
against those internal values for the if (x) case where x is a pointer,
which is the same situation as if if (x != NULL) had been coded.

I am thinking I am trying to test if the value of the char type
pass is "ded" or not. A string. So I am thinking strcmp() but if a string is
"ded I want to branch to success. If the value of pass isn't "ded" I want to
branch to something else. A failure.

strcmp(pass, "ded") will return an arithmetic value that will be
non-zero if the strings are not the same, and will be 0 if the
strings -are- the same. Because 0 is returned if they -are- the
same, the code if (strcmp(pass,"ded") == 0) will end up
comparing 0 to 0 if the strings -are- the same, and since 0 == 0
the test would be true and the first statement after the 'if' would
be executed. Therefore your originally posted code

if (strcmp(pass,pass2) == 0)

was exactly the right kind of test to make to check to see if
the two strings were the same. The original code was marred by
the strcpy() that you had above the test, which had the effect
of forcing the two strings to be the same, so because of the
strcpy(), the two would always compare equal in strcmp(), so
0 would be returned by strcmp, that 0 would be tested with == 0
because you coded that, and that would be true, so only the first
statement after the 'if' could be reached. If you did not have
the strcpy() in there, that problem would not have been present --
but you would have still had the problem that you were fgets'ing
into your initialized variable rather than into your other variable.
 
U

Ulrich Eckhardt

Bill said:
Guess so; maybe I can figure it out myself. I hope this doesn't bait
trolls but I'm sure it's a if else question. Only talk C please.

Okay, then C the FAQ, please. ;)

<SCNR>

Uli
 
R

Robbie Hatley

Bill Cunningham said:
I have been having fits with this function. Can anyone help me?

What is your program supposed to do? You don't specify.
I think I need to use if and else if but I have rewritten
it and got errors.

What errors do you get? You don't specify.
I want a conditional in this program a choice between 2
options and only one works.

Sort of like a password, eh?
I don't think I need the strcpy either.

Then why not take it out and see what happens?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main(){
char pass[10]="ded", pass2[10];

Make your arrays at least 1 larger than the strings going into
them, to allow for the NUL terminators:

char pass[11]="ded", pass2[11];
printf("Authorization ");
fflush(stdout);
fgets(pass,10,stdin);

You're over-writing your "reference" password. You don't
want to do that. That last line should actually be:

fgets(pass2,10,stdin);
strcpy(pass2,pass);

Why on earth would you want to do that? You just destroyed
your "conditional" by making *ALL* passwords pass! That's
like having a computer with password regex ".*" (any character
string you please). So just get rid of that strcpy().
if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}
else
printf("error"); exit(EXIT_FAILURE);}

It's better to format your code for readibility:

if (strcmp(pass,pass2)==0)
{
printf("success"); exit(0);
}
else
{
printf("error"); exit(EXIT_FAILURE);
}

There may be other errors there that I'm not seeing.
I don't have time to do your debugging for you.
I'll leave that as an exercise for the student.
But I think you'll find my tips above helpful.
 
B

Ben Bacarisse

Bill Cunningham said:
[snip]
If you have only been able to trigger one of the cases, then
it could be that the code above the 'if' is wrong, or it could
be that the expression to be evaluated is wrong. I can't say
which at the moment as we don't know your current code.

Ok thanks I will work on it more and come back if I still can't get it
right.

If your current version is similar to the original, then the problem
you have is that fgets retains the terminating newline (the character
that marks the end of a line of input). You need either to remove
this newline from pass2 or test for strcmp(pass2, "ded\n") == 0.
 
K

Karthigan Srinivasan

This should work.

Best Regards,
Karthigan.

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

int main(){

char pass[10]="ded", pass2[10];

printf("Authorization ");
fflush(stdout);

gets(pass2);

if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}

else {printf("error"); exit(EXIT_FAILURE);}

return 0;
}

*************************************************************


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

Richard Heathfield

Karthigan Srinivasan said:
This should work.

gets(pass2);

This is a really, really, reall bad idea. Never, *ever* use gets(), because
it has no way to protect your buffer against being overrun by excessive
data.

<snip>
 
B

Bill Cunningham

If your current version is similar to the original, then the problem
you have is that fgets retains the terminating newline (the character
that marks the end of a line of input). You need either to remove
this newline from pass2 or test for strcmp(pass2, "ded\n") == 0.
Wow. Thanks Ben. I didn't know strcmp took a literal string. So much for
my knowledge.

Bill
 
B

Bill Cunningham

I'm assuming that you intended for the password to be "ded"...

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

int main(void)
{ char pass[10]="ded", pass2[10];

printf("Authorization ");
fflush(stdout);
fgets(pass2,10,stdin);
if(strcmp(pass,pass2))
{ printf("error");
exit(EXIT_FAILURE);
}
printf("success");
return 0;
}

If you terminate the program immediately for an incorrect
password, you won't need an 'else' for the success path.
 
K

Keith Thompson

Bill Cunningham 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);}

Do you pay a per-character fee for using whitespace in your source
code?

I think your code layout is making it difficult for you to see what
your program is actually doing. It certainly makes it difficult for
me to read it. In particular, your style of hiding each closing brace
character '}', combined with your insistence on putting multiple
statements on a line, is probably the cause of your current confusion.
Using only a single column for indentation also makes your code
difficult to read.

Here's your program again, with no changes other than the addition of
whitespace (I would make several other changes as well, but I haven't
done so here). It should be enough to show you what the problem is.

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

Code layout matters.
 
K

Keith Thompson

Keith Thompson said:
Bill Cunningham 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);}

Do you pay a per-character fee for using whitespace in your source
code?

I think your code layout is making it difficult for you to see what
your program is actually doing. It certainly makes it difficult for
me to read it. In particular, your style of hiding each closing brace
character '}', combined with your insistence on putting multiple
statements on a line, is probably the cause of your current confusion.
Using only a single column for indentation also makes your code
difficult to read.

Here's your program again, with no changes other than the addition of
whitespace (I would make several other changes as well, but I haven't
done so here). It should be enough to show you what the problem is.

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

Code layout matters.

My apologies, I didn't read the code closely enough.

I has assumed that you wanted both statements:
printf("error");
exit(EXIT_FAILURE);
to be in the else clause, which would require braces surrounding both
of them.

Since the if clause:
printf("success");
exit(0);
always exits, it doesn't matter *in this particular case*. The
behavior of the program is the same whether the exit(EXIT_FAILURE); is
within the else clause, or following and separate from the entire
if/else statement.

But your original code almost completely obscures this point. Your
problem isn't what I thought it was, but your coding style still makes
your code difficult to read. And IMHO the exit(EXIT_FAILURE); clause
*should* be part of the else clause.

Here's another version of your program, again with no changes other than
whitespace:

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

It's not as legible as your original version, but it's not all that
much worse.

Fixing this problem (and, among other things, showing that you can
accept good advice) will make it more likely that my response to your
next question will be anything more than "Please clean up this mess if
you expect me to read it".

Furthermore, all you said about your problem is that you "got errors".
Telling us *what* errors you got, as well as what you expected the
program to do, would make it much easier to help you.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top