C++ Workshop - C++ Keywords, Variables, & Constants (Ch. 3)

Started by
65 comments, last by Dbproguy 15 years, 11 months ago
Indeed. The 2nd edition of the book is based on a previous version C++ and is non-standard. Many of the code examples will not compile, and many of the exercises and questions have typos.

If you're serious about learning C++, you should pick up the 5th edition of the book.

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
Advertisement
I would like to know if this is correct. You can use the same variable name for multiple function without them being the same.

 #include <iostream>using namespace std;int foo(){	cout<<"a in foo" <<endl;	int a=5;	cout<<a <<endl;        return 0;}int main(){		cout<<"a in main" << endl;	int a=8;	cout<<a <<endl;	test();        cout<<a <<endl; // Proves that "a" in main is different from "a" in foo.	return 0;}


Just like you can have a command with the same name in multiples namespace doing different things, you can have multiple different variables with the same name, right ?
Quote:Original post by Myotis
I would like to know if this is correct. You can use the same variable name for multiple function without them being the same.

Yes.

Quote:Original post by Myotis
Just like you can have a command with the same name in multiples namespace doing different things, you can have multiple different variables with the same name, right ?

If you mean "the same name but in different namespaces" then yes.

<ADVANCED>
In fact, this is more complex than this, but you'll learn more about abou this subject in a later chapter. As for today, your approximation is good enough.
</ADVANCED>

Regards,

About the enum command :

The section about it in the book is pretty short. The exemple it shows uses the enum in the code to test condition...What about using enum with cout<< and cin>> ?

If you output a color such as :
 #include <iostream>using namespace std;int main(){	enum COLOR{blue, red, yellow, green, black, pink};	COLOR mine=red;	cout<<mine;	cout<<endl;	return 0;}


the output will be 1. Is there a way to make it be "red" ?

Also, I dont see how you can input an enum.
 #include <iostream>using namespace std;int main(){	enum COLOR{blue, red, yellow, green, black, pink};	COLOR mine;	cin>>mine;	cout<<mine;	cout<<endl;	return 0;}


This code wont compile. I thought the user would have to enter either a number from 0 to 6 or simply the color names. How do you input a color ?

Thanks a lot.
Quote:Original post by Myotis
About the enum command :

The section about it in the book is pretty short. The exemple it shows uses the enum in the code to test condition...What about using enum with cout<< and cin>> ?

If you output a color such as : *** Source Snippet Removed ***

the output will be 1. Is there a way to make it be "red" ?

Also, I dont see how you can input an enum.
*** Source Snippet Removed ***

This code wont compile. I thought the user would have to enter either a number from 0 to 6 or simply the color names. How do you input a color ?

Thanks a lot.


enums just give you an easy way to "enumerate" settings. they are an easier way of writing:

const int blue = 0;const int red = 1;etc...


The names cannot be output, as it will only output the value, the same way you can't cout a variable name. The easiest way to output an enum value would be:

enum Color {BLUE, RED, YELLOW};Color c = RED;if (c == BLUE)    cout << "blue";else if (c == RED)    cout << "red";else    cout << "yellow";
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Hi guys,

Ive just finished the Quiz Questions from JWalsh and was wondering where I could find the answers to see whether I answered them correctly?

Thank you.

Edit: Sorry guys, found the thread.
I got through it fairly easy. What I've noticed, Most of this stuff in the first few chapters I seem to really already know for the most part (Just not how to do it in C++.) having been a beginner programmer of DarkBASIC previously.

Anyways, Thanks again JWalsh

Have fun everyone!
Marcus
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life

This topic is closed to new replies.

Advertisement