Advertisement

C++ Workshop - Getting Started with C++ (Ch. 1 & 2)

Started by June 01, 2006 11:40 AM
182 comments, last by Dbproguy 16 years, 9 months ago
Dmitry,

Although there was a discussion about it earlier in the weekly thread, I'll go ahead and summarize.

std:: is there to identify which namespace you're referring to. In this case, the Standard Namespace.

If you've removed the std:: from the beginning of the classes you're using and it still worked, then you've likely got a line similar to the following somewhere above the lines of code which are indicated to use std::.

using namespace std;

This is a global directive that tells the rest of the compiled module to look within the namespace 'std' when trying to identify symbols. It's easier to use than putting std:: in front of everything in the standard namespace, however it can also slow down compile times and potentially lead to name collisions with the classes you create.

Read through the remaining part of this weekly thread for more info, or do some research on C++ namespaces for more information.

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
Quote:
Original post by Nuc12
Just got a copy of the book and have a question about Exercise 3 in getting started. The code looks perfect to me and does not give me any errors when compiling but question 4 says to fix the error. Is this a mistake or am I missing something?


I know this is an old post, but, just incase the person is still reading it, the code misses the pound symbol # infront of include.



Advertisement
Hey guys/gals, I understand the difference between the \n and endl in terms of the new line vs. new line and flushing the stream.

1. Ok, by stream you are referring to what? Are there more than just the output streams? example maybe...

2. In working on the age program, I noticed something strange with the cin command. It appears that after a cin, either a \n or a endl is performed. Which is it?

For example, with this, the age prompt is on the same line as the first cout. The cin executes, still no \n or endl, but, the next cout is printed on a new line.

#include <iostream>int main(){	using namespace std;	int age;	cout <<"Please enter your age: ";	cin >>age;	cout <<"You are "<<age<<"years old."<<endl;	return(0);}


Thanks, sorry I am keeping this alive still, I am just starting from the beginning.

Shawn

1)A stream is a generic term for information. A stream can be just about anything. There are input streams and output streams. You've used the console streams, cin and cout. There are also file streams. There are string streams! In this case, it refers to the output stream. You said you get the difference, but you may not get the importance. If you don't flush the stream there are no guarantees you'll be able to see the output for awhile. What if there is a problem with the computer that causes it to, say, reset, and you want to debug the cause. So you write data to, say, a file, which is much slower than writing to the screen. If you don't flush the buffer then this data may not be written to the disk before the computer resets. As such, this is only important with output.

With input in standard c++, pretty much everything is buffered. Also, until you hit enter, the program isn't aware the data exists. When you hit enter, then the data goes into a buffer which can then be parsed. This leads to 2.

2)Where does the new line come from? From you, silly! You're the one who hit enter which is just as valid of a character as 'n' or Space. In fact, there are some ways of reading data where this newline remains in the buffer. If you mix std::cin with std::getline this sometimes happens and you have to manually delete any remaining characters in the input buffer.

And that std::getline? You can use it with different 'delimeters' that tell the program when to stop parsing the buffered data. The default is the newline, but you can have it use, say '=' or a comma(','). In fact, here's an example of the former:
#include <iostream>#include <string>#include <vector>int main (){	std::cout << "Enter some stuff!" << std::endl;	std::string firstLine;	std::string secondLine;	std::string thirdLine;	std::getline(std::cin, firstLine, '=');	std::getline(std::cin, secondLine, '=');	std::getline(std::cin, thirdLine);	std::cout << "First Line: " << firstLine << "|";	std::cout << "Second Line: " << secondLine << "|";	std::cout << "Third Line: " << thirdLine;}


Try entering something like "Hello world![enter][enter]Goodbye World!=Two Plus Two=Four[enter]"

without the quotes (or with, I it doesn't really matter) and hit enter in place of [enter]. Prepare to be enlightened!

If you wanted to get an integer with cin after the second line, you'd still have "Four[enter]" remaining in the buffer which will cause an error unless you ignore all the data. try cin.ignore(1000); this will ignore 0 and 1000 bytes of data depending on how much data is in the buffer. Of course, it's theoretically possible to have more than 1000 bytes in the buffer. Which is why you would specify the largest possible value which is numeric_limits<int>::max. But I digress.

edit: many changes

[Edited by - nobodynews on May 21, 2007 9:28:04 PM]

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

Wow this Is cool, I have this book lying around somewhere, but is it too late to start this?
A few of us still check this forum once in while...
So just drop a note if you need some help och want some one to look att you code
But don't expect an answer in shorter time than a week.
Advertisement
Quote:
Original post by Spylogo
Wow this Is cool, I have this book lying around somewhere, but is it too late to start this?


It's never to late !

Hey guys, I need some help with this part of my code. I'm pretty sure this is the part thats giving me trouble. I don't think the i+1 makes sense, as i+1 hasn't even been initialized yet. Right before it I have random numbers set up. If you need the full code that I'm working on let me know. I'm trying to get this program to draw 10 random points, and then lines between them. The code locks up on me and I get the dreaded endless loop that freezes on me.



void DrawMe()
{
for(int i =0; i<10; i++)
{
DrawLine(points.x,points.y,points[i+1].x,points[i+1].y, green);
}
}
};

On a side note, my name is Gerald Mendoza, and I'm currently attending college to become a game developer. I'm about half way through school, and this semester is really kicking my butt. My teacher is worthless, and won't help me at all. I've tried on several occasions to get just this little bit of help, and he won't even lift a few fingers to type me an email or even talk to me about it in class.
If you guys could help me with this code I'd greatly appreciate it. Also, I'll probably be using this website pretty religiously now, and I'm glad I ran across it.

Thanks Guys,
Gerald Mendoza

check this out n00bs: http://www.youtube.com/user/antiRTFM
These chapters were very informative! This book seems fairly great so far, hopefully I can at least make a text game by the end of it, I already know that anything with graphics is out of the question in this book as I've read through the table of contents.

I got through the chapters without any problems. Thanks JWalsh btw, This is a great course, beats going to college

I would like to mention, the book says to use the following code if the program flashes by quickly
char response;std::cin >> response;

However, just std::cin.get(); will work just fine, you don't have to input any data before pressing enter and it is one line as opposed to two lines of extra code.

and @ kingIZZZY, that collection of videos looks interesting though I don't want to get myself started into too many things.
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life

This topic is closed to new replies.

Advertisement