• Home
  • Textbooks
  • You Can Do It!: A Beginners Introduction to Computer Programming
  • Keyboards and Mice

You Can Do It!: A Beginners Introduction to Computer Programming

Francis Glassborow

Chapter 11

Keyboards and Mice - all with Video Answers

Educators


Chapter Questions

Problem 1

Now that you can read the keyboard directly you can change your menu programs so that they use the value of a raw keyboard input to control them. That way your users will not be irritated by having to press the return key after the letter that selects the menu entry; they will just press the key and the selected action will occur.

As a reminder, here is the final version of get_choice () from Chapter 8 (with comments about changes stripped out):
char get_choice(){
cout << "Action: (Press letter followed by Return): ";
while(true){
string input;
getdata(cin, input);
char const letter(toupper(input[0]));
int const choice(choices.find(letter));
// Check that choice is valid
if(choice < choices.size()){
return letter;
}
// Note you can only get here if the choice was not valid.
cout << ‘"’ << letter << ‘"’<< "is not an option.\n";
cout << "Please try again: ";
}
}
Modify that function so that users do not need to press Return to signify that they have made a choice. They should be able to simply press the appropriate key. You may need to look back to remind yourselt about such details as what choices is.

When you have made this modification to get_choice $O$ your menu program should work exactly as it did before with the exception that the user now needs to press only the key that indicates their choice.

Check back soon!

Problem 2

Write a program that ignores (does not even display) keys other than the letters, the space bar and the return key. Keys that it does not ignore should be displayed in the console window, with the correct case. Input should terminate when the user presses the Return key twice in succession.

The purpose of this exercise is to help you with the idea of providing controlled input. In other words arranging that a program will accept only certain keys. I would hope that you have noticed that get_choice $O$ with raw keyboard read naturally limits choice to the valid keys only.

Check back soon!

Problem 3

Write a program to test playpen_xO. Make sure you test it for various scales. You may find fow: :Wait $\mathrm{O}$ useful in writing this program in order to slow it down to a reasonable speed (note the uppercase first letter to avoid conflict with system functions called wait). The value of the argument passed to Wait is a time delay in thousandths of a second. For example, Wait $(500)$ causes a hall second pause.

Check back soon!
00:08

Problem 4

Write a function playpen $y$ that given a playpen object and a mouse: : locat ion object will return the $y$-coordinate relative to the origin and scale of the playpen object. Be careful because Windows' $y$-coordinates are measured down whereas playpen ones are measured upwards. Modify the test program you wrote for Exercise 3 to test your solution to this exercise.

Tiffany Tran
Tiffany Tran
Numerade Educator

Problem 5

Write a program to test the draw1 ine with mouse () function. Consider what enhancements would make it easier to use. Try some of them out.

Check back soon!

Problem 6

Write a function to select a start point for a line and then repeatedly call temp_ 1 ine $\mathrm{O}$ to draw a line from the start to the current mouse cursor position. When you click the mouse button the second time it draws a permanent line and returns to the caller.

Check back soon!

Problem 7

Write a program that displays a palette of 16 colors across the top of your Playpen window and then allows you to choose a color by clicking on it. Then draw a 10 by $\mathbf{1 0}$, colored square at a point selected by pointing and clicking the mouse button.

Check back soon!

Problem 8

Write a program that provides a menu of shapes across the top of the Playpen window. Make one of the shapes an " $\mathrm{X}$ " - when the user selects that shape, the program exits. Your program should repeatedly prompt the user for a color and a scale and then plot the correctly scaled shape selected with the mouse at a point on the screen selected with the mouse.
(I have not provided a sample solution for this exercise because you should be able to build on the ideas from doing Exercise 7.)

Check back soon!