Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,267 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,493 people online right now. Registration is fast and FREE... Join Now!




Program closes after execution

 
Reply to this topicStart new topic

Program closes after execution, New to C, and having some trouble.

CrematedPumpkin
14 Dec, 2006 - 05:31 PM
Post #1

New D.I.C Head
*

Joined: 14 Dec, 2006
Posts: 9


My Contributions
I'm new to C, and reading some tutorials and looking over very simple codes and whatnot I copied some of them into a compiler and compiled and so forth, but I had one problem (sorry if I sound like a complete moron asking this, but I have no one to ask really); On programs asking you to submit information, they close after pressing Enter, which I automatically assumed would be use to insert that information and whatnot. Here's the code for the simplest thing if that helps:
CODE
#include <stdio.h>

int main()
{
  int this_is_a_number;

  printf( "Please enter a number: " );
  scanf( "%d", &this_is_a_number );
  printf( "You entered %d", this_is_a_number );
  getchar();
}

Thanks for any help.
User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Program Closes After Execution
14 Dec, 2006 - 06:10 PM
Post #2

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
used
CODE
system("pause");

User is offlineProfile CardPM
+Quote Post

CrematedPumpkin
RE: Program Closes After Execution
14 Dec, 2006 - 06:11 PM
Post #3

New D.I.C Head
*

Joined: 14 Dec, 2006
Posts: 9


My Contributions
QUOTE(eXceed69 @ 14 Dec, 2006 - 07:10 PM) *

used
CODE
system("pause");


Thanks.

Another question:
I'm assuming %d is used for numbers, but what would you use for text? Thanks again.

This post has been edited by CrematedPumpkin: 14 Dec, 2006 - 06:57 PM
User is offlineProfile CardPM
+Quote Post

gordo
RE: Program Closes After Execution
14 Dec, 2006 - 08:29 PM
Post #4

New D.I.C Head
Group Icon

Joined: 7 Dec, 2006
Posts: 34


Dream Kudos: 25
My Contributions
%s
http://en.wikipedia.org/wiki/Scanf
User is offlineProfile CardPM
+Quote Post

Xing
RE: Program Closes After Execution
14 Dec, 2006 - 09:31 PM
Post #5

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(eXceed69 @ 15 Dec, 2006 - 07:40 AM) *

used
CODE
system("pause");


That won't work on all implementation. Better do this

CODE

printf("Hit RETURN to exit"\n");
fflush(stdout);
(void)getchar();


User is offlineProfile CardPM
+Quote Post

CrematedPumpkin
RE: Program Closes After Execution
15 Dec, 2006 - 12:44 PM
Post #6

New D.I.C Head
*

Joined: 14 Dec, 2006
Posts: 9


My Contributions
Ok, thank you both for you help. Now I shouldn't have too many more questions tongue.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:36PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month