Standard C++ question

Started by
3 comments, last by daviangel 15 years, 3 months ago
Hey there, new here and relatively new to programming. Specifically C++ that I'm new, dabbled in other things like actionscript and various game scripts and program scripts. One thing I noticed in most other scripts are there are dozen ways to do the same thing, like adding 72+72 then finding out the square root. But the commands had essentially only one way to write them. That's where my question comes in. I recently Picked up Beginning C++ game programming(2004) and it seems like its going to be a very good resource. Before picking up that book I read various other online sites(specifically: www.cprogramming.com) and I also skimmed through C++ Primer Plus(5th edition; 2005). Right now I'm comparing the "Hello World!" program, which I loathe with all my being. I just really hate that statement. On the website I looked at the first program starts out with the following code: ------- #include <iostream> using namespace std; int main() { cout<<"HEY, you, I'm alive! Oh, and Hello World!\n"; cin.get(); } ------- I get the use of everything in there, why its used. In C++ primer Plus the first program is the following code: ------- #include <iostream> int main() { using namespace std; cout << “Come up and C++ me some time.”; cout << endl; cout << “You won’t regret it!” << endl; return 0; } ------- Again I get the use of everything in there. Then Beginning C++ Game Programming, they through what seems to be a rather complex line for someone just learning, with little explanation of what it actually is/does. Just that it stops the cmd prompt from exiting instantly. ------- std::cout << "Press the enter key to exit"; std::cin.ignore(std::cin.rdbuf()->in_avail()+1); ------- The questions that arise are: Is there a difference between the use of cout << "xxxx \n"; and Cout << "xxxx" << endl; besides one is shorter and quicker to type. Or is it a compiler specific problem? Is there a reason that isn't apparently obvious that Beginning C++ Game Programming uses a large string of commands when other resources just use cin.get(); Being 5 years from its initial print date, is Beginning C++ Game Programming out of date now, with a new standard way to writing C++ or is the author implementing code usage in a way that will be easier to transfer over to harder projects later on down the road? And last but not least, am I making much sense in my questions? Thanks in advance. EDIT: Also I forgot to ask if "using namespace std" before int main() will allow me to use the std namespace for all functions. And if I type it after int main(), would I have to type it at the start of each function to be able to use std namespace?
I suck
Advertisement
Quote:Is there a difference between the use of cout << "xxxx \n"; and Cout << "xxxx" << endl; besides one is shorter and quicker to type. Or is it a compiler specific problem?
Both are standard c++. std::endl is not the same as a newline actually. The newline character '\n' is an actual character while std::endl is a manipulator (you'll deal with them eventually with C++ streams). Using endl has the effect of inserting a newline character into the buffer and then flushing the buffer. I'm not quite sure why books teach people to use std::endl actually when what they really want is a newline.

Quote:Is there a reason that isn't apparently obvious that Beginning C++ Game Programming uses a large string when other resources just use cin.get();
Sorry, I can't interpet this question properly. Can you rephrase?

Quote:Being 5 years from its initial print date, is Beginning C++ Game Programming out of date now, with a new standard way to writing C++
The last major revision was in 1999. That means any book around that time onwards can teach you modern C++. It's all up to the author. I can't comment on your book specifically, though.

Quote:And last but not least, am I making much sense in my questions?
Compared to some of the other questions we get here, you sound poetic. Don't worry about your questions. As long as you first do your own research, and take the time to make a coherent post, you'll be fine.

Quote:Also I forgot to ask if "using namespace std" before int main() will allow me to use the std namespace for all functions. And if I type it after int main(), would I have to type it at the start of each function to be able to use std namespace?
using namespace std does not do what you think it does.

You're imagining there is this namespace that cannot be accessed until the using declaration. That's incorrect. Normally you have to qualify everything, like std::cout or std::endl. The idea behind namespace is that you eliminate name conflicts. One example is a sort function. You write a function to sort numbers. But the standard library also provides a sort function. See the potential issue here? Qualifying the namespace removes the ambiguity (std::sort). using namespace std; removes the need for typing std:: . But it does expose you to the problem of name conflicts. Usually I just type out std::.

Or I constrain the scope. So to your second question. It's not really "after" or "before" main. It's more about the scope in which you write that statement. Outside of main, or other functions, means global scope. If you write the using statement within a function, it will only be valid within that function scope. So you'll have to rewrite it within each function.
Thanks, that clarifies it a fair bit.

Quote:Sorry, I can't interpet this question properly. Can you rephrase?


Most resources that I have looked at use cin.get(); to pause the program. The book I am using uses:

std::cout << "Press the enter key to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail()+1);

cout<< is obvious, but what is the difference between std::cin.ignore(std::cin.rdbuf()->in_avail() +1); and cin.get();

The longer version seems like a glorified cin.get();, but the question I had was: Is there a reason that longer bit of code is used over cin.get(); ? My personal thought was that later on down the road when I am working with more than just "hello world!" "The number of x+y+18 other variables = 101" the longer bit of code will provide more use than just obtaining the input of the user.

And when you say naming conflicts, does that refer to the names of functions you want to define, variables, pointers or classes(really confused on this subject, as to be expected)?
I suck
Here is a thread from several months ago discussing this question:

http://www.gamedev.net/community/forums/topic.asp?topic_id=490128

It's kind of funny though, the OP there is using Linux and apparently compiling and running from the command line. Obviously the line in question is quite pointless for him. [lol] I thought people were supposed to be encouraged to run their programs from a command prompt anyhow, and avoid that silly little addition in the first place?
Quote:Original post by lmelior
Here is a thread from several months ago discussing this question:

http://www.gamedev.net/community/forums/topic.asp?topic_id=490128

It's kind of funny though, the OP there is using Linux and apparently compiling and running from the command line. Obviously the line in question is quite pointless for him. [lol] I thought people were supposed to be encouraged to run their programs from a command prompt anyhow, and avoid that silly little addition in the first place?

Yeah this topic seems to come up time and time again and I agree some books are retarded and make this alot more complex than need book.
Also note that this is a Windows pecularity/feature as Stroustroup puts it in his new book and for comparison this is the way he deals with it in said book:
//// This is example code from Chapter 2.6 "Drill" of// "Software - Principles and Practice using C++" by Bjarne Stroustrup//#include "std_lib_facilities.h"//------------------------------------------------------------------------------int main() // C++ programs start by executing the function main{    cout << "Hello, World!\n"; // output "Hello, World!"    keep_window_open();        // wait for a character to be entered    return 0;}//------------------------------------------------------------------------------

The way I handle it personally and convinced my C++ teacher in college to do it since I really didn't want to add that extra code in all my programs is to just hit start without debugging in Visual Studio or Ctrl+F5 ;)
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

This topic is closed to new replies.

Advertisement