🎉 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
Again I would like to ask one of the tutors from this workshop to post their interpretation of the "correct" way to do the extra credit exercises. I notice alot of people who posted code just put all the code into basically one file. Is that the "correct" way to do it? I was trying to use a class to handle just about everything. Is the main not supposed to be pretty much bland and calling functions defined in classes? Maybe I am trying to read too much into it, but, since we have learned classes and functions and such, should we not use that info, or for these exercises does it not matter?

Thanks,

Shawn
Advertisement
J0be, a shortcut for you (in loops).

We know that loops check for the condition to give the boolean values of true OR false.

for instance,
while (true){    cout << "I ain't neva gonna leave!!!";}// orwhile (false){   cout << "Don't worry you won't see because the false in the loop won't let you.";}


now we know that if bool play = true, then the variable play will always evaluate to true (as long as it isn't changed).

so...
while (play){    cout << "See I'm still playing!";}// no need for thiswhile (play == true){    //this is basically saying    //while (true == true)    //silly ain't it :)}

Beginner in Game Development?  Read here. And read here.

 

I understand what your trying to say Alpha_ProgDes, but, my idea was that when I left it out, it would be more difficult for others to know what the variable actually was, by writing (play == true), you know that play is a bool variable.

I know with these small programs, it won't matter that much, but, if you have bigger programs, doesn't this then make it more obvious?


@jwalsh,
Will alter Exercise 2 to use classes and multiple files.

[Edited by - J0Be on May 31, 2007 2:43:26 PM]
Quote: Original post by J0Be
I understand what your trying to say Alpha_ProgDes, but, my ideas was that when I left it out, it would be more difficult for others to know what the variable actually was, by writing (play == true), you know that play is a bool variable.

I know with these small programs, it won't matter that much, but, if you have bigger programs, doesn't this then make it more obvious?


If people who are starting out, then you are absolutely right. Just to point out, I never said what you're doing was wrong. That's why I called it a shortcut [smile]. Keep up the good work!

Beginner in Game Development?  Read here. And read here.

 

Nono, don't get me wrong Alpha_ProgDes, I love the fact that people like you take there time and help us, not only with debugging our code and giving hints on how we should do it, but, give feedback to how we can improve our programs to become better programmers.

Quote: Keep up the good work!


Thanks, for that, really appreciated !!!

Have to go now, will work on Exercise 2 coming week-end. Have most part of it done by now, only have to add the whole char to integer thing which is bugging me big time. Got this at the moment:

int Menu::CharToInt(const char chColor[3]){	int iColor = 0;	if (isalpha(chColor))	{		iColor = int(chColor - 48);	}	else	{		iColor = atoi (chColor);	}	return iColor;}

But, it's not doing what I want, .... just yet

[Edited by - J0Be on May 31, 2007 3:18:20 PM]
Quote: Original post by J0Be
Nono, don't get me wrong Alpha_ProgDes, I love the fact that people like you take there time and help us, not only with debugging our code and giving hints on how we should do it, but, give feedback to how we can improve our programs to become better programmers.

Oh!
**shines BatLight for Zahlman and Toorhvyk** [grin]
point taken.

Quote: Keep up the good work!


Thanks, for that, really appreciated !!!

Beginner in Game Development?  Read here. And read here.

 

Quote: Original post by J0Be
I understand what your trying to say Alpha_ProgDes, but, my idea was that when I left it out, it would be more difficult for others to know what the variable actually was, by writing (play == true), you know that play is a bool variable.

I know with these small programs, it won't matter that much, but, if you have bigger programs, doesn't this then make it more obvious?


@jwalsh,
Will alter Exercise 2 to use classes and multiple files.


There is a term that, when applied to programming, means that there are certain agreed upon conventions you can use when seeing a bit of code. These are known as idioms and when you write code using these idioms it is called 'idiomatic' code.

You can't tell by looking what the type of the variable is, but you don't really need to know the type as long as you understand what the code means. This is where good variable names come in. I think for this case that 'playing' may be a better name than 'play'. Which reads more like a sentence? "while play" or "while playing"?

Lastly, even if you Did need to know the type you can select the variable, right click, and select 'go to definition' (in visual c++ 2005). Tada, you know the type.

Another thing, even if the variable Isn't a bool, you can still write while (x == true)--at least, I'm pretty sure it's legal c++. For example, this
#include <iostream>int main(){	int alpha = 10;	bool beta = true;	if(alpha == true)	{		std::cout << "alpha is true!" << std::endl;	}	if(beta == true)	{		std::cout << "beta is true!" << std::endl;	}}
compiles with only a warning (in Visual C++ 2005 with warnings turned on all the way). It only prints the second statement, however. This just means you Still can't assume that just because you put "variable == true" that the variable is of type bool just by reading it. You need to at least compile to get the information (which may or may not be a problem from the compiler's point of view).

Hope that helps.

edit: A 'true' value can be converted to an integer. In this case, the compiler converted true to 1, and then compared 10 to 1 and found they weren't equal. Lesson? Don't compare integers to booleans :)

[Edited by - nobodynews on May 31, 2007 4:50:35 PM]

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Guessing Game:

//Number Guessing Game#include <iostream>#include <stdlib.h>#include <time.h>using namespace std;int main(){	unsigned short Guess, RandomNumber, Attempts=0;	bool Playing = true;	srand((unsigned)time(NULL));		RandomNumber =(rand() % 100) + 1;	while (Playing)	{		Attempts +=1;		cout<<"Please enter your guess (1-100): ";		cin>>Guess;		if (Guess==RandomNumber)		{			cout<<"You guessed correctly!!"<<endl;			cout<<"It took you "<<Attempts<<" tries."<<endl;			Playing = false;		}		if (Guess<RandomNumber)		{			cout<<"Your guess is low"<<endl;		}		if (Guess>RandomNumber)		{			cout<<"Your guess is high"<<endl;		}	}}
#2 Color Menu

Interpretaion I implemented. I have a couple problems with it, firstly the if statement in the main and also, wasnt sure how to implement the enum of the colors even though it is in there.

Any suggestions would be appreciated.


*Edit* - All 3 programs listed in the one source listing below

//Menu Class Declaration//MenuClass.h#include <iostream>using namespace std;class Menu{	public:	void DisplayMenu();							//Function to display the menu	unsigned short GetInput();					//Function to get users input	void ChangeColor(unsigned short);			//Function to change the color	void ValidateInput(unsigned short);						//Function to check the users input		enum MenuColor {DarkBlue=1, DarkGreen, Teal, Burgundy, Violet, Gold, Silver, Gray, Blue, Green, Cyan, Red, Purple, Yellow, White, Quit};  //Enumerated Constant with values starting at 1};//This file is the definitions of the MenuClass.h//MenuFunctions.cpp#include "MenuClass.h"#include <iostream>#include <windows.h>using namespace std;//The following defines ChangeColor of class Menu	void Menu::ChangeColor(unsigned short color)	{		HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );		SetConsoleTextAttribute( hConsole, color ); // where color is the color code to set it to			}//This will display the menu	void Menu::DisplayMenu()	{		cout<<"Please enter a choice for your color"<<endl;		cout<<endl<<"1. Dark Blue"<<endl;		cout<<"2. Dark Green"<<endl;		cout<<"3. Teal"<<endl;		cout<<"4. Burgundy"<<endl;		cout<<"5. Violet"<<endl;		cout<<"6. Gold"<<endl;		cout<<"7. Silver"<<endl;		cout<<"8. Gray"<<endl;		cout<<"9. Blue"<<endl;		cout<<"10. Green"<<endl;		cout<<"11. Cyan"<<endl;		cout<<"12. Red"<<endl;		cout<<"13. Purple"<<endl;		cout<<"14. Yellow"<<endl;		cout<<"15. White"<<endl;		cout<<"Q. Quit"<<endl;	}//Function to get the users input	unsigned short Menu::GetInput()	{		unsigned int choice;		cout<<endl;		cin>>choice;		return choice;	}//Function to validate the users input	void Menu::ValidateInput(unsigned short choice)	{		if ((choice<1) || (choice>16))		{			cout<<"Error, choice must be between 1 and 16"<<endl;		}	}		//Main for the Menu system#include <iostream>#include "MenuClass.h"using namespace std;int main()	{		unsigned short Choice;		bool Exit=false;						while (!Exit)		{		Menu UserMenu;		UserMenu.DisplayMenu();		Choice=UserMenu.GetInput();		UserMenu.ValidateInput(Choice);		if (Choice==16)		{			Exit=true;		}		else		{			UserMenu.ChangeColor(Choice);		}		}	}


[Edited by - shawnre on June 1, 2007 8:47:41 PM]
Reviewed Exercise 2.

// Menu.h file#include <iostream>#include <windows.h>class Menu{public:	Menu();	~Menu();	enum COLOR { DARK_BLUE = 1, DARK_GREEN, TEAL, BURGUNDY, VIOLET, GOLD, SILVER,			  GREY, BLUE, GREEN, CYAN, RED, PURPLE, YELLOW, WHITE };	void ColorMenu();	int CharToInt(char arrColor[3]);	bool PrintColor(const int color);private:	HANDLE m_hConsole;	COLOR m_Color;};


// Menu.cpp file#include "Menu.h"#include <cstdlib>Menu::Menu() {	// Call the following line ONCE, to get the handle to the console window	m_hConsole = GetStdHandle( STD_OUTPUT_HANDLE );	m_Color = WHITE; // set color to your preference to start with.	SetConsoleTextAttribute(m_hConsole, m_Color);}Menu::~Menu() {}void Menu::ColorMenu(){	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";}int Menu::CharToInt(char chColor[3])					{	int iColor = 0;	std::cin.getline(chColor, 3);	std::cout << std::endl;	if (isalpha(chColor[0])) // check wether first letter in string is an	{			 // alphabetical value		if (chColor[0] == 'Q' || chColor[0] == 'q')		{			return iColor = 16;		}		else		{			return iColor = 99;		}	}	else													{		iColor = atoi (chColor);// if not, use ASCII to integer to alter value.	}	return iColor; }bool Menu::PrintColor(const int color){	switch(color)		{		case DARK_BLUE:			SetConsoleTextAttribute( m_hConsole, DARK_BLUE);			break;		case DARK_GREEN:			SetConsoleTextAttribute( m_hConsole, DARK_GREEN);			break;		case TEAL:			SetConsoleTextAttribute( m_hConsole, TEAL);			break;		case BURGUNDY:			SetConsoleTextAttribute( m_hConsole, BURGUNDY);			break;		case VIOLET:			SetConsoleTextAttribute( m_hConsole, VIOLET);			break;		case GOLD:			SetConsoleTextAttribute( m_hConsole, GOLD);			break;		case SILVER:			SetConsoleTextAttribute( m_hConsole, SILVER);			break;		case GREY:			SetConsoleTextAttribute( m_hConsole, GREY);			break;		case BLUE:			SetConsoleTextAttribute( m_hConsole, BLUE);			break;		case GREEN:			SetConsoleTextAttribute( m_hConsole, GREEN);			break;		case CYAN:			SetConsoleTextAttribute( m_hConsole, CYAN);			break;		case RED:			SetConsoleTextAttribute( m_hConsole, RED);			break;		case PURPLE:			SetConsoleTextAttribute( m_hConsole, PURPLE);			break;		case YELLOW:			SetConsoleTextAttribute( m_hConsole, YELLOW);			break;		case WHITE:			SetConsoleTextAttribute( m_hConsole, WHITE);			break;		case 16:			return false;		default:			std::cout << "Wrong value, select again!\n\n\n";		}	return true;}


//Main.cpp#include "Menu.h"int main(){	Menu mColor;	bool exit = true;	char arrColor[3] = "";	while(exit)	{		mColor.ColorMenu();		int numVal = mColor.CharToInt(arrColor);		exit = mColor.PrintColor(numVal);	}	return 0;}

This topic is closed to new replies.

Advertisement