keep window open with cin.get

Hello,

I am using a third part software "gnuplot" to draw graphics. In my code, I use a header file to send commands to this software to see the graphics. In order to keep each graphic window open on screen instead of closing immediately, I add cin.get(); right before return 0;

However, the consequence is weird: each time I have to enter a character in console to get the program run, and the graphic window closes ASAP. It seems the program wait for cin.get() to catch something before start drawing, which is not what I wanted.

How can I fix this, please?

Thanks in advance.
I'm not quite sure about this but possibly you could use system("pause");. The only two ways that I know how to stop the screen is like you have said and system pause. Some people do not like system pause. It is all I can think of though.
Yes, but a lot of programmers say that system is not a good choice. So it's preferable to use cin.get
Right, I'm not aware of all the consequences. That's all I got for suggestions.
But just because a lot of people say it is not good does not mean that it is not OK to use in some situations where you are aware of the consequences. Unless information in this post pertains to you, I would try it and use it if it worked.

http://www.cplusplus.com/forum/articles/11153/
I don't understand your description, post your code.
If you're using external libraries make sure to provide a link to them.
my code for drawing is simple, just to run a test.
1
2
3
4
5
6
7
8
9
#include "gnuplot-iostream.h"

int main(){
	Gnuplot gp(gnuplot.exe); //create the class
	gp<<"plot sin(x)\n";   // send the command to draw a graph
	std::cin.get();      // please stop to let me see
	
	return 0;
}


gnuplot-iostream.h is a header that helps to send commands to gnuplot.exe.
http://gitorious.org/gnuplot-iostream/gnuplot-iostream/blobs/5fa41c7b536e72f03bcca0b59c7be035d13e950b/gnuplot-iostream.h

gnuplot.exe is a plotting software.

Normally, with the "plot" command a window of graph will appear, with the "cin.get" the program will wait for catching a character on console window, so the graph doesn't disappear.

But here, I have to enter a character to make the program run, then the graph window appears and disappear. The "cin.get" doesn't have the expected effect.
Last edited on
Read examples.cc
1
2
	// -persist option makes the window not disappear when your program exits
	Gnuplot gp("gnuplot -persist");


Also, gp.flush(); so it draws immediately.
Last edited on
closed account (18hRX9L8)
getchar();
Thanks ne555 and usandfriends for your propositions.

I have checked with each of your recommendations. getchar(); doesn't work in this case, it has the same effect as cin.get();, the program needs me to enter a character to start drawing. But the graph disappear immediately.

-persist keep the console window displaying all commands but there is no effect on the graph.

However, gp.flush(); does work. The graph stays on screen till I enter a character for cin.get();.

Can anyone explain why do we need flush to make it draw immediately, which means why cannot the program draw by itself without this command, please?
Last edited on
Any idea?
This is the same as with any output stream: your output commands are accumulated in its buffer, and they are only send out when it is flushed.

cin.get() calls cout.flush(), so when you're using regular console I/O, it works the way you're used to. But, cin.get() has no knowledge of gp, you have to flush it yourself.
You could use gp<<"plot sin(x)\n" << flush; or even gp<<"plot sin(x)" << endl; to do this.
OK, it's clear now. Thanks for the explanation.
Topic archived. No new replies allowed.