CS 31 Programming Assignment 2 FAQ File

Questions are in regular text. Answers are in boldface.

  1. How should we handle unprintable characters?

    Say "The character is unprintable" and then display its ASCII value. The unprintable characters are those with ASCII value less than 32.

    Your program need process only those characters with ASCII values in the range 0-127.

  2. I've checked all keyboard strokes from the ASCII table and all work properly with my code except the delete key. The program does not register that I've input the key at all. Any suggestions? or just a comment under BUG section in my write-up?

    That's OK. A remark in your documentation will suffice.

  3. I can't get the single quotations around the character to print out as suggested on the homework description. For example, The ASCII value of '+' is 43. don't come out on several versions of trying different things.

    You probably just need to use the escape sequence \'. If that doesn't work, you can use the ASCII value of ' by converting it to a character.

  4. Also, I ran into a problem with the static_cast operator on the character input to get the ASCII value when I tried to follow the text example.

    Hmmm. The static_cast<int> operator seems to work fine on my machine. But it is possible that your installation of MSVC++ does not support it, as it is a relatively recent addition to the language. In this case, you should be able to get by with the earlier, simpler notation: int(). E.g., cout << int('a'); should print out 97.

  5. I can get the program to translate almost all characters except for certain ones such as 'del' and 'nul' and the ascii characters with values <= 32 ( since im not really sure what those are). I understand that del is not so important, but im wondering why it doesnt acknowledge the space bar as a character. Thank you.

    The problem is that the >> operator ignores leading whitespace (blanks, tabs, and newlines). If you want to treat all characters the same, and not ignore whitespace, you have to use the "get()" member function instead of the >> operator. E.g.,

              char myChar;          
              cin.get( myChar );
    
    I overlooked this issue in the specification, and I don't believe the TAs covered it in discussion, so we won't deduct points for programs that can't handle whitespace and unprintable characters correctly. We will discuss the "get()" function in more detail later in the quarter. Sorry for this oversight! Examples of an unprintable character are the escape key (ESC) and the CTRL sequences (CTRL-A, CTRL-B, etc.). Some of these (e.g., ESC) will also not be readable with >>. If you use the "get()" member function, you should be able to read all of these without any problem.

  6. I don't know how to set the value of a character variable. Suppose I have a variable of type char named ch, and I want to set its value to the letter z. What's wrong with the statement ch = z;?

    A character not enclosed in single apostrophes is interpreted by the compiler as an identifier (a programmer-defined name). To specify a character constant, enclose the character in apostrophes: ch = 'z';

  7. Everything in my program pretty much works. However, I'm having a lot of trouble figuring out how to make the code for outputting the specific letter of the alphabet with st, nd, rd, and th. How am I supposed to be able to do that for every letter.

    You can print out st this way:

    cout << "st";
    That may not seem like much help, but it should be enough. Try it. Alternatively, you can store the suffix in two separate variables, e.g.
    char s1,s2;
    and then set the values of these later, e.g.,
    s1 = 's'; s2 = 't';
    The single apostrophes are necessary on the right-hand sides of the above assignments.



Homework 2 Home Page