Quizzes and Extra Credit for Week 6.
Post your answers to the Quizzes from Week 6 here rather than in the chapter thread, so as not to spoil things for the others.Chapter 7 Quiz 1. What does a while-loop cause your program to do? 2. What does the “continue” statement do? 3. What does the “break” statement do? 4. What is it called when you have a loop in which the exit condition can never be met? 5. What loop device do you use if you want to ensure the loop executes at least once, regardless of the success/failure of the test condition? 6. What are the 3 parts to a for-loop header? 7. Can you initialize, test, or perform more than one action within a loop header? If so, what does the syntax look like? 8. Which of the three components of a for-loop header can be left out? 9. According to the new ANSI standard, what is the scope of variables declared in the for-loop header? 10. What types of expressions can be used in a switch statement? 11. What happens if there is no ‘break’ statement at the end of a switch case? 12. Why is it a good idea to always have a default case in a switch statement? Chapter 7 Exercises 1. Guessing Game: Returning to the “guess the number” exercise from week 3, lets now make it a complete game. In your main function generate a random ‘secret’ number using the method shown in week 5 between 1 and 100. Next, repeatedly ask the user to guess the number UNTIL s/he gets the answer correct. Each time they guess let them know whether the number was higher or lower than their guess. Once the user has guessed correctly, let them know and tell them how many guesses it took. 2. Color Menu: In this exercise you are going to write a program that shows the user a menu asking them what color they would like to display their menu in. The menu itself, will be a list of matching numbers and colors. Use the menu below to determine menu options. Present the menu to the user over and over again, allowing them to change the color of the menu until they choose the q option. Once they select ‘q’, terminate the program. There is helper code below to allow you to change the color of the console window. Once the user has selected a color option, set the color for the console window, and then re-print the menu onto the screen. Although not strictly necessary for this exercise, I encourage you to make an enum out of the following menu options and color names, and then use a switch statement to check for color values. The program will work just fine without, however….perhaps try it both ways and see how it’s different in this case. Show the users the following menu: -------------------------------------------- 1. Dark Blue 2. Dark Green 3. Teal 4. Burgundy 5. Violet 6. Gold 7. Silver 8. Gray 9. Blue 10. Green 11. Cyan 12. Red 13. Purple 14. Yellow 15. White Q. Quit Please select a color to display your menu: --------------------------------------------
// To gain access to the functionality required to change the console window colors, add the following include file
#include <windows.h>
// In function main, call the following line ONCE, to get the handle to the console window
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
// To actually SET the color of the console window use the following line of code.
SetConsoleTextAttribute( hConsole, COLOR_VALUE ); // where COLOR_VALUE is the color code to set it to
Cheers and Good luck!