HW1 FAQs

 

Question:

How do I pad a hexadecimal number with zeros in baseout?

Answer:

There are multiple ways to handle printing 16 digits with leading zeros. If you are printing from storage in an array, you could start by putting '0' in the array 16 times. Then when your algorithm stores other numbers in the array, it overwrites some of zeros such that when you are done, the leading zeros are already there. Alternatively, if you count the number of digits printed for the actual part of your number, you can have an additional loop of printing zeros until your digit count is 16. Alternatively, rather than having your loop of processing the number end with the result of a division is 0, you could have your loop end after it processes 16 numbers. This way, after you're done processing the number via divides and modulus, you'll keep getting 0 as the result of the modulus, and these 0s are your leading zeros.

 

Question:

Why do my debug messages come before my normal output?

Answer:

The debug strings are displayed first because they print to stderr while the regular strings print to stdout. Since stderr is non-buffered, they show immediately. Since stdout is buffered, they only show when the buffer is flushed (newline, call to get input, buffer full, or program terminates normally).

 

Question:

Why am I seeing "garbage" in the terminal's output when I run my program?

Answer:

You are most likely printing out ASCII values (characters) of blocks of memory that you did not allocate. A block of memory will have whatever value was last written to it, and therefore, have "strange" values if you did not purposely write to the memory yourself. This typically happens when you attempt to print out the values of an array but kept printing out values past the length of the array.

 

Question:

I see an '^M' at the end of every line. Why is that?

Answer:

These Control-M characters are often the result of transferring a text file between operating systems. This probably happened with the student transferred the file from home to school.

To remove all of these extra unwanted Control-M characters at once, within command mode in "vi" you can say...

:%s/^V^M//g

The ':' brings you to colon mode.
The '%' means to effect the entire file.
The 's' tells vi to search.

What follows the first forward slash is the text in your code for which you are searching. The Control-V Control-M will only show as one character: '^M'. Control-V (V for vi) is the escape prefix in vi allowing to enter Control-M. Normally, Control-M is the same as a ; therefore, you can't search for it without entering Control-V first.

What follows the second forward slash is the replacement text; in this case, you want to replace the Control-M with nothing. The last forward slash ends your replacement.

The final 'g' is to make the change on all occurrences of the found pattern within the same line.

Also, you can emulate this process by running the command dos2unix from the command line. For more information: man dos2unix

 

Question:

for the writeline method, is the '\n' supposed to be passed as an argument or does the method itself add the '\n' to the end of the string?

Answer:

For the writeline method, '\n' is an ordinary character. If it's present in the string, then it's displayed. If it's not present in the string, then it's not printed. Newline is special for input in that your decin and getline methods need to check for it. NULL is special in output in that writeline must stop printing when it sees NULL.