Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Executing problems


  • Please log in to reply

#1
troppo

troppo

    Member

  • Member
  • PipPipPip
  • 582 posts
Hi All,
First of all im very new to programming and am only trying to program my first little thing, so forgive me if i have no idea what your talking about lol

ok so im writting a small program with this source code from a text book

#include <stdio.h>

main()
{
printf("hello world\n");
return 0:
}

and i save it and then compile it using Dev-C++ and when i double-click on the application file the program comes up quickly and then dissapears really quickly is there something im doing wrong? or have i just missed a step somewhere?

Thank you for your time,
Troppo
  • 0

Advertisements


#2
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
First off, return 0 should have a semicolon [;] after it, not a colon [:]. Let's assume that was just a typo here. return 0 exits the program... so the solution would be:

1. Run the program from command prompt. It then exits to command prompt, but you can still see the results.
2. Remove the return 0. Then, to exit the program, just hit the little X in the top right hand corner, like most other programs.
3. Call something that waits for user input, such as getchar.
Ex.
#include <stdio.h>
main()
{
printf("hello world\n");
printf("press enter to continue\n");
getchar();
return 0;
}

Edited by stettybet0, 12 July 2007 - 09:58 AM.

  • 0

#3
jpshortstuff

jpshortstuff

    Member

  • Member
  • PipPipPip
  • 119 posts

#include <stdio.h>

main()
{
printf("hello world\n");
return 0:
}

no, your not really doing anything wrong.

whats happening is, your program is writing "Hello World" to the screen, then its reaching the 'return 0' line which returns it from the method, ending the program. if you want to leave the message up, try adding this line to your program:

system("PAUSE");

just after your printf("hello world\n");

this will put a predefined: "Press any key to continue..." which will wait for the user to press a key, before getting to the 'return 0' statement.

#include <stdio.h>

main()
{
	  printf("hello world\n");
	  system("PAUSE");
	  return 0:
}

hope this helps,

jpshortstuff

EDIT: beaten by stettybet0, with more knowledgeable solutions :whistling:

Edited by jpshortstuff, 12 July 2007 - 10:09 AM.

  • 0

#4
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
I'm afraid I have to recommend against using what jpshortstuff said... Using his code will make your program unable to work under any operating system but Windows. And anything that can be done through system calls can be done natively in C++. And lastly, you have to include another header file to use system calls.

See here for more info: http://www.gidnetwork.com/b-61.html
  • 0

#5
jpshortstuff

jpshortstuff

    Member

  • Member
  • PipPipPip
  • 119 posts
well there you have it, i was wrong

i was also going to suggest using a form of input to keep the command box up (cin, scanf, getchar()) but i thought id just leave it at the system command for now. i dont really know enough about compatibility and c++ in general to tell what is good or bad

well, i learned something, thanks :whistling:

jpshortstuff

Edited by jpshortstuff, 12 July 2007 - 11:24 AM.

  • 0

#6
troppo

troppo

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 582 posts
Hey All,
Thanks very much its working now :whistling:

thats my first program little thing i no its nothing special but im just starting Thanks so much now i can move on to something a little more difficult thank you all for your help

Troppo
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP