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

Join 149,911 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,169 people online right now. Registration is fast and FREE... Join Now!




Beginner C question: using variables

 
Reply to this topicStart new topic

Beginner C question: using variables, console closes immediately

cyblord
3 Jan, 2008 - 11:39 PM
Post #1

New D.I.C Head
*

Joined: 3 Jan, 2008
Posts: 2


My Contributions
Hey everyone, I'm new to the forum. I'm a complete beginner to programming C, so please bare with me here tongue.gif

I'm using Code::Blocks v1.0 (on window$) with GCC.

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();
}


When I choose to start a new project and build it as a part of the project it runs fine. But when I build it as a standalone file and run it, console simply pops up and disappears.

How do build and run it from a single c file instead of having it as a part of a project?

Thanks!
User is offlineProfile CardPM
+Quote Post

Delta_Echo
RE: Beginner C Question: Using Variables
4 Jan, 2008 - 04:48 AM
Post #2

D.I.C Regular
***

Joined: 24 Oct, 2007
Posts: 480


My Contributions
Im learning C++, if they are as alike as i have heard then using cin.get();
at the end should hold the program open until you press enter. Thats all i an give ya sad.gif.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Beginner C Question: Using Variables
4 Jan, 2008 - 06:08 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
You are using the getchar() function to hold for input from the buffer. the problem here is that there is already a character in the buffer ( a newline character) from your previous input operation. you can either clear the buffer before using getchar(), ory ou can add another call to getchar(). Or you can run it from the command line, and avoid the problem all together! smile.gif

http://www.dreamincode.net/forums/showtopic30581.htm

User is online!Profile CardPM
+Quote Post

cyblord
RE: Beginner C Question: Using Variables
4 Jan, 2008 - 12:22 PM
Post #4

New D.I.C Head
*

Joined: 3 Jan, 2008
Posts: 2


My Contributions
I'll take the easy way out for now and run it from cmd tongue.gif

Thanks!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 01:59PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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