🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Week 6 (Ch. 7) - Quizzes and Exercises

Started by
22 comments, last by Dbproguy 16 years, 2 months ago
Hey JOBe or anyone for that matter, lol

How did you get your code snippets to wrap for long lines? If you look at mine, it made the width of the post extremely wide instead.

Thanks,

Shawn
Advertisement
Quote: Original post by shawnre
Hey JOBe or anyone for that matter, lol

How did you get your code snippets to wrap for long lines? If you look at mine, it made the width of the post extremely wide instead.

Thanks,

Shawn



Hello shawnre,

I think it's because your comment in your program is to far away from your code, if you then copy it intoo here, you get those long lines.
It probably didn't wrap his. He probably inserted a new-line in his code to make the length of the line shorter.

When posting lines of code to the GDNet forums definitely keep it under 80 characters wide.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Chapter 7 Quiz

1. What does a while-loop cause your program to do?
The while block will execute, while the statement stated in the while statement is true.

2. What does the “continue” statement do?
It will skip the rest of the loop and to back to the beginning of it.

3. What does the “break” statement do?
It skips the rest of the loop and does not loop through that loop again.

4. What is it called when you have a loop in which the exit condition can never be met?
A forever loop

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?
do...while

6. What are the 3 parts to a for-loop header?
initialization, test and action

7. Can you initialize, test, or perform more than one action within a loop header? If so, what does the syntax look like?
Yea, for (int i = 0, j = 0; i<3; i++, j++;)

8. Which of the three components of a for-loop header can be left out?
Any

9. According to the new ANSI standard, what is the scope of variables declared in the for-loop header?
Just to the for block

10. What types of expressions can be used in a switch statement?
Any valid C++ expression.

11. What happens if there is no ‘break’ statement at the end of a switch case?
It will fall-through to the next cases.

12. Why is it a good idea to always have a default case in a switch statement?
In case something goes wrong.

Chapter 7 Excercises

1. Guessing Game
[source lang=cpp]#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;int Menu();void Guessing();void ViewResults();int guess;int counter = 1;int randomNum;int main(){	srand( (unsigned)time( NULL ) );	int choice, quit = 0;	while (quit == 0)	{		choice=Menu();		switch (choice)		{		case 1:			Guessing();			break;		case 2:			ViewResults();			break;		case 3:			quit = 1;			break;		default:			cout << "Error. Returning to menu." << endl << endl;			quit = 1;			break;		}	}}int Menu(){	int choice;	cout << "Menu:" << endl;	cout << "(1) Start Playing" << endl;	cout << "(2) View Previous Results" << endl;	cout << "(3) Quit" << endl;	cout << "Choice: ";	cin >> choice;	cout << endl;	return (choice);}void Guessing(){	cout << "Welcome to the Guessing Game!" << endl;	cout << "  by Marcus Johnson" << endl << endl;	randomNum = ( rand() % 100 ) + 1;	cout << "I have generated a random number between 1 and 100." << endl;	cout << "Now I would like you to guess it!" << endl << endl;	for (;guess!=randomNum;counter++)	{		cout << "Guess " << counter << ": ";		cin >> guess;		if (guess > randomNum)			cout << "Oops! Too high!" << endl;		else if (guess < randomNum)			cout << "Oops! Too low!" << endl;		else if (guess == randomNum)			cout << "Good job! It took you " << counter << " guesses." << endl << endl << endl;	}}void ViewResults(){	cout << "Here are your previous results:" << endl;	cout << "It took you " << counter-1 << " tries to guess the number " << randomNum << "." << endl << endl << endl;}

2. Color Menu
[source lang=cpp]#include <windows.h>#include <iostream>using std::cout;using std::cin;using std::endl;enum CHOICE { DARK_BLUE = 1, DARK_GREEN, TEAL, BURGUNDY, VIOLET, GOLD, SILVER, GRAY, BLUE, GREEN, CYAN, RED, PURPLE, YELLOW, WHITE };int main(){	int choice, quit = 0;	HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );	while (quit == 0)	{		cout << "Menu:" << endl;		cout << "1.  Dark Blue" << endl << "2.  Dark Green" << endl;		cout << "3.  Teal"      << endl << "4.  Burgundy" << endl;		cout << "5.  Violet"    << endl << "6.  Gold" << endl;		cout << "7.  Silver"    << endl << "8.  Gray" << endl;		cout << "9.  Blue"      << endl << "10. Green" << endl;		cout << "11. Cyan"      << endl << "12. Red" << endl;		cout << "13. Purple"    << endl << "14. Yellow" << endl;		cout << "15. White"     << endl << "16.  Quit" << endl;		cout << endl << "Please select the number next to the color to display your menu: ";		cin >> choice;		switch (choice)		{		case 1:  SetConsoleTextAttribute( hConsole, DARK_BLUE ); 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, GRAY); 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: quit = 1; break;		default: cout << endl << "Error. Try again" << endl << endl;		}	}}


For that I have a better idea besides using the switch case. Really if you just tested it to be a number somewhere 1 through 15 then just enter that number in the SetConsoleTextAttribute in place of the color name, then test if it's 16 and if it then stop the program.

It seems like it would be so much less code.
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life

This topic is closed to new replies.

Advertisement