Keeping console open, "recommended way"?

Pages: 12
May 28, 2013 at 12:26am
I have read the "Console Closing Down" thread (http://www.cplusplus.com/forum/beginner/1988/) completely, but there doesn't seem to be an answer I'm looking for. I know all of the ways not to keep the console open, but I can't find the "recommended" way of keeping the console open.

It's only for small personal projects, so I know I could use things like system("PAUSE"), etc. but I want to learn to do it the right way.

Thanks!
Steve
May 28, 2013 at 1:35am
Take Duoas' earlier suggestion and combine it with what Ropez (I think) suggested.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <limits>

struct KeepRunning{
   ~KeepRunning(){
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
   }
};

int main(){
   KeepRunning Kr;
   std::cout << "Hello world!" << std::endl;
   return 0;
}


But remember what is probably the second most important thing from that pinned thread: the solution became necessary only because the original poster's IDE failed to keep the console open like most other IDE's.
May 28, 2013 at 2:35am
I've tried that before and it doesn't seem to work with my program, it might be because I have an extra \n left in cin?

I use Code::Blocks, there might be some sort of option to keep the compiler open, but I can't find it.
May 28, 2013 at 2:38am
I do, too.

Projects-->Properties-->Build Target-->"Pause when execution ends"
May 28, 2013 at 2:44am
"Pause when execution ends" is checked for me.

When it closes, I mean when I run the .exe from my desktop(or wherever you have it) not from "inside" the compiler(e.g. not when I hit F9)
May 28, 2013 at 2:47am
Try switching around the delimiter parameter in cin.ignore to see if you indeed have a hanging newline character in the buffer.
May 28, 2013 at 2:56am
I'm afraid that's a little above my knowledge, sorry!
May 28, 2013 at 3:03am
i was playing around with _getche(), i found that when i press only Enter, the return value was 13 decimal.
if the Enter key sends a value of 13, you can use this as the delimiter for cin.ignore().
i don't have time to try it, i'm really sorry.
hope that was useful.
May 28, 2013 at 3:07am
I'm sorry, I just simply don't know what a "delimiter" is, in this case, and what I'm supposed to do with it.
May 28, 2013 at 3:09am
Enter sends the newline character into the buffer, and '\n' has an ASCII code of 10.

Don't depend on a nonstandard function like _getche(). In the past, I used getch() to find the ASCII values returned by the arrow keys, but the values were different depending on where in my program the getch() was called.

Edit:
cin.ignore(number_to_ignore, delimiter);
The delimiter is what character the function should stop at even if it has not read in the "number(of characters)_to_ignore".

Edit 2:
Rechard3, the value you got was probably for the carriage return, which might work fine. I think only Windows and Macs use '\r' (13).
Last edited on May 28, 2013 at 3:14am
May 28, 2013 at 3:13am
thx for the advice, i'll keep that in mind.
May 28, 2013 at 3:15am
You mean so it should look like this?:
std::cin.ignore( '\n', std::numeric_limits<std::streamsize>::max());

With it like that,^^ It doesn't close immediately, but instead I must press enter 10 times to close. Or press enter 5 times, if I'm "submitting" an integer with it.
Last edited on May 28, 2013 at 3:16am
May 28, 2013 at 3:19am
Below is what that might be equivalent too, which is not what I meant:
1
2
cin.ignore(10, N); //N is the character equivalent to the ASCII value
                  //    return by numeric_lists<streamsize>::max() 


I meant switching out '\n' with other characters.
May 28, 2013 at 3:36am
If you start debugging with CTRL + F5, that keeps the console open for me. Is that what you mean?
May 28, 2013 at 3:55am
Daleth:

I'm getting different results, depending on which .exe I run. Should I be running the "main.exe" in my programs folder, or should I run the "program_name_here".exe in "\bin\Debug"?

Ima767god:

"CTRL + F5" does nothing for me, are you using Code::Blocks?
May 28, 2013 at 4:01am
Location doesn't matter, only as long as the programs are exactly the same.

Why don't you post your program here, so we can take a proper look at it.
May 28, 2013 at 4:02am
Sorry StevetheHunter, that is "beyond my pay grade".
May 28, 2013 at 4:17am
Then it's odd that I get different results.

Here's the code, I know it's probably pretty bad, but I'm a beginner!:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <limits>

using namespace std;


struct KeepRunning{
   ~KeepRunning(){
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
   }
};


int Sale(int x, int y)
{

    double saledecimal = y / 100.0;
    int z;
    double newPrice = x * saledecimal;
    cout << "\nYou get " << newPrice << " dollars off!\n";
    cout << "\nSo now your item costs " << x - newPrice << " dollars!" << endl;

}


int main()
{
    KeepRunning Kr;
    cout << "Enter the price of the item: ";
    int itemPrice;
    cin >> itemPrice;

    cout << "Enter the sale percentage: ";
    int salePercentage;
    cin >> salePercentage;

    Sale(itemPrice, salePercentage);

    return 0;
}
Last edited on May 28, 2013 at 4:17am
May 28, 2013 at 4:24am
Why look at that hanging newline from your cin calls. I assumed you had already implemented cin.ignore() to catch those hanging newlines.

At least on line 36:
1
2
cin.ignore(); //Grab '\n' from buffer; now it is empty
//I could be wrong about needing just one call to cin.ignore() 


More complete answer:
http://www.cplusplus.com/forum/beginner/1988/2/
Look for Duoas' color program example.
May 28, 2013 at 6:17am
Another possibility:

1
2
3
4
void pause() {
	std::cin.sync();
	std::cin.get();
}
Pages: 12