• Enter a do-while loop and generate a random, byte-sized, value in the range of
0 to 255. Store the random value in a variable of type byte. Display the value
in hex as shown below.
• The user will input a string containing 1's and 0's. Use a do-while loop to
ensure that the length of the string is 8 characters; if it is not, honk the
speaker and display an error message in yellow, and obtain new input.
An invalid guess is not counted as one of the five hex values. The hex value
for which an incorrect number of bit was entered will be repeated again, as
shown below.
• Convert the string to a byte-sized number. Compare the number with the random
value. If the two values are the same, display a message indicating a correct
result and increment the number of correct responses. If the values are
different, display a message indicating that the user's response was incorrect.
• Continue looping until the user has been presented with five hex values.
• Display the number of correct responses.

so far i have this:

string sInput;
Random rndGen = new Random();
byte byhex = (byte)rndGen.Next(256);
byte byInput = Convert.ToByte(sInput, 2);

Console.Title = "ICA15 - Hex Quiz";
Console.WriteLine("{0, 40}", "ICA15 - Hex Quiz");

Console.WriteLine("\nInput the binary equivalent to the following ten hex values.");

Console.WriteLine("\nThe binary equivalent to {0} is ", byhex);
byInput = byte.Parse(Console.ReadLine());

the last writeline is not showing...

Attachments cnt157.jpg 29.76 KB
6 Years
Discussion Span
Last Post by Momerath

my question is if my random byte sized is correct and
how to put that in a console.writeline..

how to contain just 1 and o in string?


Your byte size is correct, and you can't prevent someone from typing in something other than 1 and 0 so you'll have to verify that is all they typed in. I'd use Byte.TryParse instead of Convert.ToByte as it won't throw an exception if it fails.


like this:
byte byInput = byte.TryParse(sInput, 2);

another question:
how come After line 9, a simple console.write("") is not showing if i run it...


To your first question, no, that's not how you use TryParse:

byte byInput;
if (Byte.TryParse(sInput, out byInput) {
   // parse succeeded, byInput has a value
} else {
   // parse failed

As for your second question, the code you have posted won't compile so I can't answer why a specific line won't run. Post all the code (or enough that shows the problem and will actually compile).


according the ICA
the user will input their binary value as a string 1's and 0's.
The string (sInput) can be converted to a byte by the following code:

byInput = Convert.ToByte(sInput, 2);


That's true, but is a poor way to do conversions. If you haven't learned exceptions yet (or TryParse) then I'd stick with it. Maybe your next class will cover exceptions.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.