DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Replacing system("pause"); with a function (http://www.daniweb.com/forums/thread104737.html)

#include<DAN.h> Jan 14th, 2008 5:25 pm
Replacing system("pause"); with a function
 
I hear alot of people saying not use:
system("pause");
system("cls");
system("title");

I think you get what I mean, so I'm trying to make functions that will replace all of these, and for my first installment I'm going to show you how to replace system("pause");. Here is what I have.

#include <iostream>
#include <conio.h>

void PrsAnyKey2Cont ();
char KeyStroke;

void PrsAnyKey2Cont ()
{
    cout << "Press any key to continue . . .\n";
    KeyStroke = getch ();
    KeyStroke = getch ();
    switch (KeyStroke)
    {
          default :
          break;
    }
}

int main ()
{
    cout << "Hello world!!" << endl;
    PrsAnyKey2Cont ();
    return 0;
}

There are probably a million things wrong with this right now and I plan on making a better function that will allow you to set the time of the pause and wether or not the user even needs to input a key to continue. Thanks for any constructive critisism!! ^_^

Narue Jan 14th, 2008 5:28 pm
Re: Replacing system("pause"); with a function
 
>Thanks for any constructive critisism!!
PrsAnyKey2Cont is a very awkward name.

#include<DAN.h> Jan 14th, 2008 5:32 pm
Re: Replacing system("pause"); with a function
 
Yeah, I know, but I couldn't think of anything short and descriptive. XD Do you have any seggestions for a better name?

Narue Jan 14th, 2008 6:11 pm
Re: Replacing system("pause"); with a function
 
>Do you have any seggestions for a better name?
At the risk of borrowing an existing name, "pause" is short and sweet. Throwing it in a "console" namespace is descriptive, and I can type and read console::pause so much faster than PrsAnyKey2Cont. :)

#include<DAN.h> Jan 14th, 2008 6:17 pm
Re: Replacing system("pause"); with a function
 
Excuse me for sounding like a total noob here for a second, but what is a "console namespace". To tell you the truth I've been writing:
using namespace std;
at the beginning of all of my programs for almost 2 years and I've never learned what it meant. Sorry. =[

Brent.tc Jan 14th, 2008 6:31 pm
Re: Replacing system("pause"); with a function
 
I BELIEVE that a namespace is just something used to give context to where something should be used. Also, it helps to avoid conflicting names.

#include<DAN.h> Jan 14th, 2008 6:34 pm
Re: Replacing system("pause"); with a function
 
So, what does it mean to say std::pause?

Brent.tc Jan 14th, 2008 6:39 pm
Re: Replacing system("pause"); with a function
 
Is there a pause in std? do you mean std::system("pause") ?
Well anyways, since 'pause' is/would be in namespace std, you must first 'access' the std namespace.

Think of it as a box where you put things to keep them either organized, or from getting jumbled with other things. You can't simply remove something from the box without first opening the box... (Said box has a lid...)

#include<DAN.h> Jan 14th, 2008 7:04 pm
Re: Replacing system("pause"); with a function
 
Yeah I just forgot to put using namespace std;, which would open the box, right?

Brent.tc Jan 14th, 2008 7:12 pm
Re: Replacing system("pause"); with a function
 
More like dumping the box all over the floor... Because once the box is opened (using namespace std; ) you no longer have to open it each time you want something from it (std:wantedObject)


All times are GMT -4. The time now is 11:28 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC