Sign in to follow this  
empire2105

Newbie question/s

Recommended Posts

Im new to programming (apart from some stuff my dad showed me on oue ATARI in BASIC, but that was a long time ago). Anyhow im interested in taking up programming as a hobby because im planning on doing computing after I leave high school. I plan on learning C++ and making simple 2D games (side scrollers, etc) and then maybe working up to 3D games. Atm i'm reading through one of two books I bought from amazon called "C++: A Beginner's Guide" (the one im reading now) and "Beginning C++ Game Programming". The first thing I would like to ask is how to i stop the console window from closing as soon as i run the program? In the book it says you can use
	cout << "Press enter key to exit...";
	cin.ignore(std::cin.rdbuf()->in_avail() +1);
But this doesnt work when you use "cin >> blah;" for some reason. My other question is, I was looking at the stickie on this forum and noticed it said:
Quote:
Find the 'input' section of the book or tutorial. Some courses will input text using a sequence of lines such as: char name[number]; followed some time after by cin >> name; or std::cin >> name;. This is a bug (known as a buffer overflow: a common weakness in many programs and often exploited by hackers), and the text you are reading is encouraging unsafe behavior which can lead to unexpected (and dangerous) results.
The book I am reading uses the cin >> name; method, is this wrong? Thanks in advnace.

Share this post


Link to post
Share on other sites
cin >> name isn't the problem; the problem is the char name[number] bit. That creates a sequence of characters which is only "number" long; if the user enters more than "number" worth of characters, then the name variable will overrun. Text (aka "strings") should be stored in the std::string class, not char arrays.

Share this post


Link to post
Share on other sites
To answer your question about the window closing too quickly, you should really just go to your start menu -->run-->cmd ... Once you do that you can go to the folder where your executable is by using (cd to change directories and dir to see what is in that directory) and then just type the program name to run it. You must be in the same folder as the program though.

I used to have that problem too but after looking through the CPP workshop they have going on here I have found out that it is not good programming practice to add unecessary pauses in your programs, and running it from the command line prompt makes it show up. Hope this helps.

Share this post


Link to post
Share on other sites
I don't like to use system("pause")...it has some habit of doing weird things to my computer (freezing and such). I don't know it could just be me. ;)

Quote:

...I have found out that it is not good programming practice to add unecessary pauses in your programs...

Really, this isn't an unnecessary pause. It makes sense in this situation that you pause the window when its done, doesn't it? But, if you don't want to use 'bad practices' in your program, then I'd suggest the following, which should work just fine:

int main()
{
//do program stuff
char quit = 'n';

while (quit != 'y')
{
cout << "Quit the program (y or n)?";
cin >> quit;
}

return 0;
}



This technically isn't an artificial pause, as you are just asking if you want to quit now. If you type y when prompted it will quit. (Make sure that you add "using namespace std;" (w/o quotes) to the top of your program, under your #includes, or you will have to use std::cout and std::cin)

Share this post


Link to post
Share on other sites
Quote:
Original post by bschneid
Quote:

...I have found out that it is not good programming practice to add unecessary pauses in your programs...

Really, this isn't an unnecessary pause. It makes sense in this situation that you pause the window when its done, doesn't it?


No, it doesn't, because if you're writing a console program, it's with the expectation that your users are going to run it properly, i.e. from a console window - as you should, too.

If you want the convenience of auto-opening a console window and having it stick around at the end of the program run, you can do it with a batch file. Just make a text file, put in two lines - the first being the name of your program, and the second being pause - and change the extension to .bat. (That's for Windows of course; naturally you can do something equivalent on Linux/etc. with any kind of *sh scripting.) Now you have the ability to either pause or not pause your program at the end, and you don't have to recompile two versions each time you change the rest of the code.

Quote:


int main()
{
//do program stuff
char quit = 'n';

while (quit != 'y')
{
cout << "Quit the program (y or n)?";
cin >> quit;
}

return 0;
}



This technically isn't an artificial pause, as you are just asking if you want to quit now. If you type y when prompted it will quit.[/quote]

Exactly - if it makes sense for your program to loop in a way like this, then the program termination is *expected*, and there's no good reason to pause at the end. Adding "boilerplate" artificial-pause-at-end code would just annoy the user. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
No, it doesn't, because if you're writing a console program, it's with the expectation that your users are going to run it properly, i.e. from a console window - as you should, too.


Ok, fair enough. I should've added that I assumed that the program is for learning purposes only, making him the only user. In that case its much easier to just click the run button in the IDE, which doesn't involve properly opening the console window.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this