Program help

This is a discussion on Program help within the C Programming forums, part of the General Programming Boards category; I am to write a program that opens a file (input.txt) that contains unsigned values. The program has to ask ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    21

    Program help

    I am to write a program that opens a file (input.txt) that contains unsigned values. The program has to ask the user for a couple of commands. The first command is "f <value>". This will mean that the user inputs "f" and then a inputs a value 0-3. The value inputted pretains to a certain nibble value. if user inputs "f" and a value, the program searches through the file for which numbers have that inputted value and in which nibble. The program then outputs which item it was and which nibble it was. for example, the file contains 271, 17767, 35243, 52719, 257.

    user inputs "f 0"
    program will output the following:
    Match: Item 0, Nibble 3
    Match: Item 4, Nibble 1
    Match: Item 4, Nibble 3


    My question is how do I get the program to check the items in the file to see which nibbles match the inputted value. So far, I have gotten the program to actually read the file for the items and just output them in 4 digit hexadecimal. I know I am to use a for loop for the "f <value>". I just don't know how to actually get the program to check and mask for the nibble value that is inputted and which items actually contain that value in which nibble. If anything is confusing in my explanation, please let me know. Thank you in advance for any help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you need to figure out how to get the value of any nibble. Look up your bitwise operators.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    I know how to do that, using the &. However, I do not know how to check for the same value as the input and find the matches.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What's wrong with == ?

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    I'm not following you. I'm slightly confused. I would use the & operator to mask the bits. But what would I use == for?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You want to check whether two things are equal (the value of the nibble, and the value inputted). That's usually a good indicator for the use of ==.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    oh, ok. Understood. But how do I get the program to check nibble values? Is this where I would have to mask the items in the file so the program can check and compare each nibble value with the user input?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's why I asked if you knew how to get nibble values. You have a number that you read from the file. It has some nibbles in it. You need to get those nibble values, check each of them against the target, and if they match print it out.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    well I kind of know how to get the nibble values. I am still confused on it though. The whole masking thing is still strange to me. Am I correct to use the & operator to mask? If so, how would I go about doing that?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course you use & to mask. Think about what your number is
    Code:
    0101 0010 0001 0000
    -n1- -n2- -n3- -n4-
    You will need to get each of the nibbles n1, n2, n3, and n4 by removing/masking out the other nibbles, and then moving/shifting the bits you have to the appropriate place (i.e., in the example above n1 is 5, not 20480 or 0x5000).

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    alright, thank you so much. I understand the masking and it is working on my program right now. Now i have to do that for all the nibbles in the value and then check to see if it matches the user input and print it if it does. I'm sure ill run into another problem so I'll be back when I do. Again, you have been great help to me, thank you.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    alright, I have been able to get the nibble values. I was thinking of writing a function to test the nibble values against the input value. something like
    Code:
    testnibble(number, nibble, value)
    That design was actually given to me by my professor. How would I go about writing a function that does what i described above?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by fiendslyr View Post
    That design was actually given to me by my professor. How would I go about writing a function that does what i described above?
    I usually use a keyboard for my function-writing needs.

    (No, really: what other answer do you expect to get? Type the commands for getting a nibble, and checking whether the nibble equals the value.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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