interpretation of a question

This is a discussion on interpretation of a question within the C Programming forums, part of the General Programming Boards category; guys, i realise i've made 3 threads in the last 24 hours, which is probably excessive and i'm sorry. but ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    28

    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. #2
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote 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...
    Emmanuel Delahaye

    "C is a sharp tool"

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    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. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    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. #6
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote 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.
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    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. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  9. #9
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote 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.
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    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. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    [edit]Formatting edit. I still wish it would preserve whitespace...[/edit]

    Quzah.
    Last edited by quzah; 08-08-2004 at 03:59 AM.
    Hope is the first step on the road to disappointment.

  12. #12
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote 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.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21