if and switch

B

Bill Cunningham

If won't work in evaluating integers will it? That's my interpretation
of what I've been reading. You would have to use switch in those interger
cases. I had some code.

if (ans=='y') printf("ok"); exit(0); /* ans is a char */

But what I'm asking for here is the ascii value for y. An integer. So my
compiler complained. Now if I did this,

if (ans=="y") printf("ok"); exit(0);

Then it would work right? Otherwise,

switch (ans=='y')
case1: printf ("ok"); exit (0);
break;
case 2: if (ans!='y') printf("error"); exit(1);
break;

Have I proofreaded myself pretty good on this one? I don't really know
switch well though. I should go with the if (ans=="y") since a string is
what I want.

Bill
 
D

Default User

Bill said:
If won't work in evaluating integers will it?

You haven't finised the previous problem. One of things keeping you
from learning is that you are hopping from question to question with
ever completing anything.

Go back and answer those questions I gave you previously in that
thread. Keep working on your chunk() routine until it is complete and
correct.

I'm not going to give you any further input until you do that.





Brian
 
E

Eric Sosman

Bill said:
If won't work in evaluating integers will it? That's my interpretation
of what I've been reading. You would have to use switch in those interger
cases. I had some code.

if (ans=='y') printf("ok"); exit(0); /* ans is a char */

But what I'm asking for here is the ascii value for y. An integer. So my
compiler complained. Now if I did this,

if (ans=="y") printf("ok"); exit(0);

Then it would work right? Otherwise,

switch (ans=='y')
case1: printf ("ok"); exit (0);
break;
case 2: if (ans!='y') printf("error"); exit(1);
break;

Have I proofreaded myself pretty good on this one? I don't really know
switch well though. I should go with the if (ans=="y") since a string is
what I want.

You have so many fundamental misunderstandings that it's
hard to know where to begin. Very briefly, though:

- The if statement tests whether its expression is zero
(false) or anything else (true). The expression can have an
integer type, and in that sense if is perfectly capable of
"evaluating integers."

- In the first line of code you show, the if controls
whether printf() is called, but exit() is called no matter
how the test turns out.

- The numeric code for the letter y *is* 'y', that is,
when you write 'y' the compiler puts y's code number into
your program.

- If you need the ASCII code number for y, write 121. If
you need y's number in the encoding your machine uses (which
is very probably ASCII, but might be something else), write 'y'.

- Your second line of code is flat-out wrong, R-O-N-G.
That's not how you compare strings in C -- and besides, you
said that ans is a char, not a string.

- The == operator compares its operands and yields a
value of type int: 1 if the operands are equal, 0 if they
are unequal. Your switch would be legal if you added the
{ and matching } that the syntax requires, but case 2
could never, ever be executed.

You need to go back to your C textbook or C teacher and
spend some time learning the language; Usenet is good for
many things, but mass inculcation of basic knowledge is not
one of them. Another excellent resource is the comp.lang.c
Frequently Asked Questions (FAQ) http://www.c-faq.com/, but
it won't make much sense to you until you have learned some C.
 
S

santosh

Bill said:
If won't work in evaluating integers will it?

It can evaluate any legal expression that produces a non-void value.
That's my interpretation of what I've been reading. You would have to
use switch in those interger cases.
No.

I had some code.

if (ans=='y') printf("ok"); exit(0); /* ans is a char */

This is probably not what you meant. What you perhaps meant to write
was:

if (ans == 'y') {
printf("ok\n");
exit(0);
}

Notice the compound statement? And the newline after "ok"? You probably
also want the test condition to be:

if (ans == 'y' || ans == 'Y') { ... }
But what I'm asking for here is the ascii value for y.

C supports other character encoding than ASCII. Using hardcoded ASCII
values in your program ensures unnecessary loss of portability. Just
use character constants.
An integer.

A character constant is an integer.
So my compiler complained. Now if I did this,

if (ans=="y") printf("ok"); exit(0);

Then it would work right?

This broken. You cannot directly compare strings in C. You need to use
something like strcmp() or strncmp().
Otherwise,

switch (ans=='y')
case1: printf ("ok"); exit (0);
break;
case 2: if (ans!='y') printf("error"); exit(1);
break;

Have I proofreaded myself pretty good on this one?
No.

I don't really know
switch well though. I should go with the if (ans=="y") since a string
is what I want.

switch is overkill for this purpose.
 
B

Bill Cunningham

You need to go back to your C textbook or C teacher and
spend some time learning the language; Usenet is good for
many things, but mass inculcation of basic knowledge is not
one of them. Another excellent resource is the comp.lang.c
Frequently Asked Questions (FAQ) http://www.c-faq.com/, but
it won't make much sense to you until you have learned some C.

I actuallly thought I had something. I guess not.

Bill
 
A

Army1987

santosh said:
It can evaluate any legal expression that produces a non-void value.
struct foo { int bar; };
struct foo baz(void) { struct foo ret = { 42 }; return ret; }
/* ... */
if (baz()) {
/* What about that? */
}
 

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,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top