Chapter 7.
1) What does a while-loop cause your program to do?
A) A while-loop causes your program to repeat a sequence of statements as long as the starting condition remains true.
2) What does the "continue" statement do?
A) The continue statement jumps back to the top of the loop.
3) What does the "break" statement do?
A) The break statement immediately exits the loop, and program execution resumes after the closing brace.
4) What is it called when you have a loop in which the exit condition can never be met?
A) They are called "eternal" loops.
5) What loop device do you use if you want to ensure the loop executes at least once, regardless of teh success/failure of the test condition?
A) You use a do ... while loop.
6) What are the 3 parts to a for-loop header?
A) They are: - initialization
- test
- action
7) Can you initialize, test, or perform more than one action within a loop header? If so, what does the syntax look like?
A) Yes you can do this, an example would be: for (int i = 0, j = 10; i < 10; i++, j--)
8) Which of the three components of a for-loop header can be left out?
A) All can be left out.
9) According to the new ANSI standard, what is the scope of variables declared in the for-loop header?
A) The scope of the declared variables in the for-loop is limited to the block of the for-loop itself.
10) What types of expressions can be used in a switch statement?
A) Any legal C++ expression that evaluate(or can be unambiguously converted to) an integer value.
11) What happens if there is no 'break' statement at the end of a switch case?
A) Then the execution falls through to the next statement.
12) Why is it a good idea to always have a default case in a switch statement?
A) Because this way, you can have a default way of letting the user know his input was wrong, inaccurate, out of range, ... This can be a tremendous aid in debugging for the programmer himself.
/*1. Guessing Game: */#include <ctime>#include <cstdlib>#include <iostream>void Menu();void guessNumber();void Quit();int main(){ // Call this ONCE, in your main function to seed the random number generator srand( (unsigned)time( NULL) ); bool play = true; int wish; while (play == true) { Menu(); std::cout << "\t\tEnter your Wish: "; std::cin >> wish; switch(wish) { case 1: guessNumber(); break; case 2: break; case 3: Quit(); play = false; break; default: std::cout << "Wrong choice, pick again!\n\n"; break; } } return 0;}void Menu(){ std::cout << "\t****************Menu***************\n"; std::cout << "\t(1) Wish to play a game? Pick N°1:\n"; std::cout << "\t(2) Wish to see the menu? Pick N°2:\n"; std::cout << "\t(3) Wish to Quit? Pick N°3:\n";}void guessNumber(){ bool exit = true; int number, guesses = 1; int secretNumber = (rand() % 100) + 1; std::cout << std::endl << std::endl; std::cout << "\t\tWelcome to the guessing game!\n\n\n"; std::cout << "\tPlease enter a number between 1 and 100:\n"; while (exit) { std::cout << "\t\tYour guess is number: "; std::cin >> number; if (number < secretNumber) { std::cout << "The number is smaller then the secret number!" << std::endl; guesses++; } else if (number > secretNumber) { std::cout << "The number is bigger then the secret number!" << std::endl; guesses++; } else { std::cout << "You've guessed the secret number, it is: " << secretNumber << std::endl; std::cout << "You've guessed it in " << guesses << " tries!\n\n\n"; exit = false; } }}void Quit(){ std::cout << "Thank you for playing this game. Goodbye!" << std::endl;}
/*2. Color Menu: */#include <iostream>#include <string>#include <windows.h>int Menu();int PickColor();bool PrintColor(const int color);int main(){ bool exit = true; int color; while(exit == true) { color = Menu(); exit = PrintColor(color); } return 0;}int Menu(){ std::cout << "1. Dark Blue.\n"; std::cout << "2. Dark Green.\n"; std::cout << "3. Teal.\n"; std::cout << "4. Burgundy.\n"; std::cout << "5. Violet.\n"; std::cout << "6. Gold.\n"; std::cout << "7. Silver.\n"; std::cout << "8. Gray.\n"; std::cout << "9. Blue.\n"; std::cout << "10. Green.\n"; std::cout << "11. Cyan.\n"; std::cout << "12. Red.\n"; std::cout << "13. Purple.\n"; std::cout << "14. Yellow.\n"; std::cout << "15. White.\n"; std::cout << "Q. Quit.\n"; std::cout << "Please select a color to display your menu:\n"; std::cout << "-------------------------------------------\n\n"; return PickColor();}int PickColor(){ std::string choice; std::cout << "Color number: "; std::cin >> choice; if (choice == "1") return 1; else if (choice == "2") return 2; else if (choice == "3") return 3; else if (choice == "4") return 4; else if (choice == "5") return 5; else if (choice == "6") return 6; else if (choice == "7") return 7; else if (choice == "8") return 8; else if (choice == "9") return 9; else if (choice == "10") return 10; else if (choice == "11") return 11; else if (choice == "12") return 12; else if (choice == "13") return 13; else if (choice == "14") return 14; else if (choice == "15") return 15; else if (choice == "Q" || choice == "q") return 16; else { std::cout << "Wrong number! Pick again!\n"; return PickColor(); }}bool PrintColor(const int color){ // In function, call the following line ONCE, to get the handle to the console window HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE ); enum COLOR { DARK_BLUE = 1, DARK_GREEN, TEAL, BURGUNDY, VIOLET, GOLD, SILVER, GREY, BLUE, GREEN, CYAN, RED, PURPLE, YELLOW, WHITE }; switch(color) { case 1: SetConsoleTextAttribute( hConsole, DARK_BLUE); // where color is the color code to set it to break; case 2: SetConsoleTextAttribute( hConsole, DARK_GREEN); break; case 3: SetConsoleTextAttribute( hConsole, TEAL); break; case 4: SetConsoleTextAttribute( hConsole, BURGUNDY); break; case 5: SetConsoleTextAttribute( hConsole, VIOLET); break; case 6: SetConsoleTextAttribute( hConsole, GOLD); break; case 7: SetConsoleTextAttribute( hConsole, SILVER); break; case 8: SetConsoleTextAttribute( hConsole, GREY); break; case 9: SetConsoleTextAttribute( hConsole, BLUE); break; case 10: SetConsoleTextAttribute( hConsole, GREEN); break; case 11: SetConsoleTextAttribute( hConsole, CYAN); break; case 12: SetConsoleTextAttribute( hConsole, RED); break; case 13: SetConsoleTextAttribute( hConsole, PURPLE); break; case 14: SetConsoleTextAttribute( hConsole, YELLOW); break; case 15: SetConsoleTextAttribute( hConsole, WHITE); break; case 16: return false; default: std::cout << std::endl; } return true;}
Edit: altered function names, added the enum to the 2nd exercise and improved input checking, hopefully for the better.
[Edited by - J0Be on May 31, 2007 8:48:42 AM]