Console Closing Down

Pages: 1... 34567
No, you're not.



...at least not until you have read the thread.
Hi. I had the same problem my friend helped me out. He has me writing in C# but it should look like this in C++

int main () {
cout << "Blah blah blah"
while (true) {}
return 0;
}
............................................________
....................................,.-‘”...................``~.,
.............................,.-”...................................“-.,
.........................,/...............................................”:,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:”........./
..............?.....__.........................................:`.........../
............./__.(.....“~-,_..............................,:`........../
.........../(_....”~,_........“~,_....................,:`........_/
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}
...........((.....*~_.......”=-._......“;,,./`..../”............../
...,,,___.\`~,......“~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-”
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\
I haven't read all all the posts here, but I can help you.

you can include the "windows.h" header, and use the Sleep( ); function.

you can use it like this
Sleep(1000);
it stops the program for some miliseconds. the number you add there. if you enter 3000 the program stops for 3 seconds.

Sorry for my english. I'm not good at explaining things not even in my language... :) in case you didn't understand, send me a msg, and I'll try to help

I'll even post some code to make it easier for you to understand.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <windows.h>
using namespace std;

void main()
{
cout<<"some text"<<endl;
Sleep(10000); // this stops the program for 10 seconds. try changing the number, and see what happenes
}


I think that's all.

aaa an other way might be with the getch(); function. this stops the program and waits untill you press a key on your keyboard, You need to include the conio.h library

example

1
2
3
4
5
6
7
8
#include "iostream"
#include "conio.h"

void main()
{
cout<<"the same text as before"<<endl;
getch(); // some compilers will tell you to use _getch(); instead of getch(); I don't know why and I don't care :);
}

that's all. By the way... I use Sleep( );
Last edited on
Where do you people come from?

I haven't read all all the posts here, ...
Then what makes you think you are qualified to say anything? You must be pretty bright to know better than everyone else without even reading what they have posted.

but I can help you.
Just like the "help" that has already been thoroughly dismissed and/or discussed in this thread?

[examples]
You've got to be kidding?

Sleep() is a decidedly suboptimal solution. But at least you started to get a clue when you realized that perhaps some user input might be better? Or that uncounted millions of posts about this very topic are how to do the "Press any key" trick?

// some compilers will tell you ... I don't know why and I don't care
If you don't know and don't care, then don't suggest it until you understand what you are talking about.

... send me a msg, ...
What, so you can decide who gets to understand and who doesn't?


Perhaps those of use too slow to understand might need a few more postings by someone with bigger cajones. Or perhaps you ought to at least read the last four or five posts before spouting-off.

Oye, si meramente quieres ayudar, bienvenido al foro. No me dan decir "véte", y no lo digo, pero no ofenderas a otros al prestar el mínimo de leer lo que ya se ha escrito, no hablarnos como tontos, y observar las normas del foro. Oh, también, la mayoría del uso aquí tienen la sensatez evitar palabras crudas.
I quote helios:

............................................________
....................................,.-‘”...................``~.,
.............................,.-”...................................“-.,
.........................,/...............................................”:,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:”........./
..............?.....__.........................................:`.........../
............./__.(.....“~-,_..............................,:`........../
.........../(_....”~,_........“~,_....................,:`........_/
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}
...........((.....*~_.......”=-._......“;,,./`..../”............../
...,,,___.\`~,......“~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-”
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\
I'm new to C++ programming, but I could answer the question. the 1st question here was this

Zolaboony (1) May 19, 2008 at 12:30am
Hi, i am new to C++ and have just written my "Hello World" program. It worked all right but the console/cmd closes down instantly. If you tell me why it is closing down it would be very helpful.



and you said this:

Duoas (1352) May 19, 2008 at 12:40am
It is because your IDE is too stupid to know that a console application run from the IDE should have an automatic pause at the end.

Add the following to the end of your main() function to fix it:

...

std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return 0;
}


You'll need to #include <limits>

Enjoy.


do you think that it's easier to do what you typed there or to use getch(); or Sleep(); ?

and about the thing with getch() and _getch()... on visual studio 2008 they say something like this

warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
c:\program files\microsoft visual studio 9.0\vc\include\conio.h(145) : see declaration of 'getch'

I didn't want to find the reason yet, but I'll try to find it when I'll have some time.

by the way, I read the rest of the posts after I posted my 1st msg
Last edited on
Look, I'm not so stupid that I have forgotten the contents of this thread, let alone my own answers. The answer I gave is correct, platform-independent, standard C++.

What do you think is easier? A solution that only works for some people or a solution that works for everyone, everywhere?

I also actually answered the OP's question, in addition to giving a solution.

Oh, and by "ISO C++ conformant" Microsoft means "according to Microsoft" --no form of getch() is, and has ever been, standard C or C++.

Thanks FD
Last edited on
ok, what ever, but if you learn C++, you don't learn it to make console applications, so this things are not so important. more important are the pointers, classes, functions and other things that you will use to create something useful.
Wtf? I learned C++ to make console apps...I dunno what you are talking about.
Don't get mad at me for this Duoas but I haven't read the full thread either (it was just too long and i gave up halfway).

I just want to know if there's anything wrong with using this because that's what I have been doing for a while.

1
2
3
4
5
6
7
8
9
10
11
12
13
...

std::cout << "Press Enter to Exit ... " << std::endl;

//in case a badbit or failbit has been set
std::cin.clear();

//i prefer this to ignore() because ignore sometimes means hitting enter twice
std::cin.sync(); 

std::cin.get();

return 0;
Last edited on
@thecoolguy98
I've never heard of sync before, but damn, it works perfectly. Then again, I though getch() worked as well before I found out why I was not supposed to use it :P

I cant really see anything wrong with this though, it works and its portable (it is, isnt it?) although I'm surprised no one thought of this before, so I'll let the experts have their say on this before I decide anything.
Last edited on
We know how to stop console closing down and i have answer to another question:
how clear the screen. Maybe my code looks stupid but it works and i use it.
.....
Cout <<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\\n\n\n\n\n\n\n\n\n\n\n\n\n";
thecoolguy98
I don't really care so long as people don't start posting the same thing that has already been re-hashed at length in the thread. Just as in real life, it is rude to jump into a conversation to start bringing up questions that have already been thoroughly answered.

The cin.sync() thing is a neat trick, but it is only required to work on buffered streams --and only on the object's buffer --not necessarily the underlying device's buffer. Hence, while you can generally expect it to work with console I/O, it is not always safe to assume it will work elsewhere --for example if the standard input is piped in from a file or a network connection, etc.

To know why you ever have to hit ENTER twice, you'll have to read the whole thread. The benefit that ignore() has over get() is that it is more forgiving of bad input. So, I would amend it to:
1
2
3
4
std::cout << "Press Enter to exit...";
std::cin.clear();  // brilliant!
std::cin.sync();   // brilliant!
std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );  // lets the user type anything up-to and including the Enter key 

However, for just terminating the program, you don't actually have to read any keys; reading just one and ignoring everything else is fine too.

Default
You can also do it using a std::string constructor:
cout << string( 100, '\n' );
Unless your program is really small and you don't use the string class at all, you won't get any more code by doing it this way.

I know I've posted this stuff elsewhere, but I just recently posted this snippet over at DaniWeb:
http://www.daniweb.com/forums/post703879.html#19

Clears the screen on Win32 and POSIX platforms. If neither, you get the generic 100 newlines.

Enjoy!
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
#include <limits>
#include <iostream>
using namespace std;

int Add (int x, int y)
{
	cout << "In Add(), received " << x << " and " << y << "\n";
	return (x+y);
}

int main()
{
	cout << "I'm in main()!\n";
	int a, b, c;
	cout << "Enter two numbers: ";
	cin >> a;
	cin >> b;
	cout << "\nCalling Add()\n";
	c=Add(a,b);
	cout << "\nBack in main().\n";
	cout << "c was set to " << c;
	cout << "\nExiting...\n\n";
	std::cout << "Press ENTER to continue...";
	std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
	return 0;
}


Right, so I added what Duoas said in the second post of this thread to my basic C++ program, but it doesn't stay open for much more than a millisecond.

I'm using Turbo C++, and going to Run > Run, to run the program.

Anyone have any ideas?
It is because you are mixing formatted input with unformatted input.

Stick a cin.sync(); between lines 22 and 23.

:-)
............................................________
....................................,.-‘”...................``~.,
.............................,.-”...................................“-.,
.........................,/...............................................”:,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:”........./
..............?.....__.........................................:`.........../
............./__.(.....“~-,_..............................,:`........../
.........../(_....”~,_........“~,_....................,:`........_/
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}
...........((.....*~_.......”=-._......“;,,./`..../”............../
...,,,___.\`~,......“~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-”
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\
Thats alot of input for a single, seemingly, supposedly or supposed to be simple task. Why don't they just add a new function to the standard C/C++ library to save such endeavour in topics such as these.

I just wanted to know out of the so bountiful methods why cin.ignore(), the one I chose to use wont work for me. I'm using VC++ 6.0, can't understand why it recognises the command but won't use it.
hi??im new to c++..how to display a string??
>hi??im new to c++..how to display a string??

[code=cpp]cout << myString;[/code]
Pages: 1... 34567