# Thread: interpretation of a question

1. ## interpretation of a question

guys, i realise i've made 3 threads in the last 24 hours, which is probably excessive and i'm sorry. but i consider this to be a different subject.

i'm supposed to design a program that based on a 6 bit code, add two numbers together, subtract one number from another, multiply two numbers together or divide one number by another. the codes are as follows

if the code is 32 hex you must add InputA to InputB
if the code is 34 hex you must subtract InputB from InputA
if the code is 2 hex you must multiply InputA and InputB
if the code is 3 hex you must divide InputA by InputB

design your program to accept a code and two inputs then display the corresponding result and the code on the screen.

i've made the program already. and it all works fine. BUT i think i might have mis interperated the "6 bit code" bit, i just did
Code:
`scanf("%d", &code)`
and then checked it to be 32||34||2||3

do you guys think i've done that incorrectly? i'm assuming i have. what do you think?

2. Originally Posted by jibbles
i've made the program already. and it all works fine. BUT i think i might have mis interperated the "6 bit code" bit, i just did
Code:
`scanf("%d", &code)`
and then checked it to be 32||34||2||3

do you guys think i've done that incorrectly? i'm assuming i have. what do you think?
Assuming 'code' is an int, it sounds good to me.

Note that scanf() is probably not the best choice. A smart combination of fgets() and strto[u]l() or sscanf() is probably safer. If you insist, don't forget to test the returned values...

3. Well a 6-bit code limits the values to be 0..63 inclusive.
So perhaps you need an error message if code is outside this range?

4. sorry i mightn't have explained myself properly?

the program i wrote works fine, if u enter "32" then it adds the numbers. but the "6 bit code" is what has thrown me. is 32 6 bit code? or am i supposed to be reading a different type of number and converting it to 32? do u know what i mean?

5. salem, your answer makes more sense, thanks.
but that still makes no sense to me, because if u entered 10 for example, which fits inside the limit, it wouldn't do anything.

while on that subject, i'm having trouble with setting my error message..

i've been trying

Code:
```
while (code != 32 || code != 34 || code != 2 || code != 3)
{
printf(options);
scanf("%d",&code);
}```
which doesnt seem to work, i've tried quite a few things, but not getting it.

6. Originally Posted by jibbles
Code:
```
while (code != 32 || code != 34 || code != 2 || code != 3)
{
printf(options);
scanf("%d",&code);
}```
which doesnt seem to work, i've tried quite a few things, but not getting it.
Your logic is flawed.

Rewrite it in pig-English (forget 'neither-nor'):

"While code is not 32 and code is not 34 and etc."

Got it?

And please check the return of scanf(). What if you type 'a32' by accident? I won't warn you any more about it.

7. sorry, what do u mean check the return of scanf() i'm unfamiliar with what u are talking about..

also, i still don't see how my logic is floored?
i'm saying while code is not 32 OR is not 34 etc ask for the code again
aren't i?
so when it is 32 or 34, it should exit the while loop..
right?

8. Originally Posted by jibbles
sorry, what do u mean check the return of scanf() i'm unfamiliar with what u are talking about..
I often find myself wondering just what the hell books or instructors are teaching these days...

You don't know how to check the return value of a function?
Code:
```int foo( void )
{
return 1;
}

int main( void )
{
int bar;

if( foo( ) == something )
printf("foo equals something!\n");
else
printf("foo does not equal something!\n");

bar = foo( );
if( bar == something )
printf("foo equals something!\n");
else
printf("foo does not equal something!\n");

return 0;
}```
You check the return value the same way you check any value. By comparison. There is more than one way to do it. I'll leave it up to you to figure the rest out.

Naturally you'll want to look up the scanf function to see what it actually returns on success and failure.

Quzah.

9. Originally Posted by jibbles
sorry, what do u mean check the return of scanf() i'm unfamiliar with what u are talking about..
most C-functions return a value. scanf() returns the number of successful conversions. In your case, you are expecting 1. If it returns something else, it means that there was some error. You should warn the user and invite him to try again.
also, i still don't see how my logic is floored?
'floored' ? If you meant flawed, I thougth I have explained it already... Let's try a different approach.

What should be the YES logic :

if (a == 2 || a == 4)

But you want to repeat the process as long as this logic is false. Hence:

while (!(a == 2 || a == 4))

this is correct, and it expands to:

while (a != 2 && a != 4)

according to the boolean logic rules.

http://www.texas.net/~square1/start2a.html
i'm saying while code is not 32 OR is not 34 etc ask for the code again
aren't i?
so when it is 32 or 34, it should exit the while loop..
right?
Wrong! Make the test.

10. ok i learnt alittle about scanf returning a 1 last semester but i didn't learn to check it (in most cases)

1 = true
0= false
u reccomend checking that each time u use it?

also, you were right about the while loop.
thank you.
i guess i'm not thinking that straight tonight.

but i do understand why i was wrong before (thanks to your help. THANKS!)

11. It doesn't return one. It returns the number of stored scans, as shown by this
excerpt from the man page, or else on error:

RETURN VALUES

These functions return the number of input items assigned,
which can be fewer than provided for, or even zero, in the
event of a matching failure. Zero indicates that, while
there was input available, no conversions were assigned;
typically this is due to an invalid input character, such
as an alphabetic character for a `%d' conversion. The
value EOF is returned if an input failure occurs before
any conversion such as an end-of-file occurs. If an error
or end-of-file occurs after conversion has begun, the num-
ber of conversions which were successfully completed is
returned.
Formatting edit. I still wish it would preserve whitespace...[/edit]

Quzah.

12. Originally Posted by jibbles
ok i learnt alittle about scanf returning a 1 last semester but i didn't learn to check it (in most cases)
1 = true
0= false
To avoid any misunderstanding, keep in mind that scanf() returns the number of successful conversions.
u reccomend checking that each time u use it?
Yes. If you expect two conversions, the test should be
Code:
```   int ret = scanf("%d%d", &a, &b);

if (ret == 2)
{
/* ok */
}
else
{
/* error */
}```
etc.

And I also recommend that you write in plain English. Bcz English is nt my fst lng.

Popular pages Recent additions