Jump to content

TOTAL newb

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
science

science

    Newbie

  • Members
  • Pip
  • 8 posts
Alrighty, I borrowed a C programming book from my cousin to help me get into programming, and I downloaded a bloodshed compiler.

Ok, I typed out this simple function:

#include <stdio.h>

main ()
{
printf("hello, world/n");
}

compile it, save it as a C source file on desktop. I click on the desktop icon I just created to get my output, but the window with the output only pops up for like a millisecond! I need to it stay up, so I can move on the more advanced things where I might actually want to LOOK at my output.

Make sense? I know, I'm completely new to this. I hope you can help me.

#2
suicidal pencil

suicidal pencil

    Newbie

  • Members
  • PipPip
  • 27 posts
try this:

-open a command prompt.
-drag the desktop icon into it.
-hit 'enter'.
-The program will work

It's doing exactly what you want. It's printing "hello, world", then exiting.

Here's a bit of a picture guide of what I described. I used a perl file to do this, btw.

Posted Image
Posted Image
Posted Image

Edited by suicidal pencil, 15 May 2008 - 01:24 PM.

Programming is an art form. Everyone can program, but few can do it right.

#3
science

science

    Newbie

  • Members
  • Pip
  • 8 posts
THANK YOU!

If anyone else would like to share another way, please feel free to do so.

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
A common way is also to "pause" the program right in the end. It can be done in various ways. You can choose to you use the system-command, pause, in Windows, which will do the job for you, or you can wait for user-input in the end.

system("pause");
or
getchar();

These functions shall be thrown in right before the end of the main-function. Note that the first solution using the pause-command only will work on platforms having the pause-command.

#5
suicidal pencil

suicidal pencil

    Newbie

  • Members
  • PipPip
  • 27 posts

science said:

THANK YOU!

your welcome :)
Programming is an art form. Everyone can program, but few can do it right.

#6
soku11

soku11

    Newbie

  • Members
  • PipPip
  • 12 posts
Or you can read something to a variable:
char a;
printf("%c",&a);

It will also wait for input without closing the window :) But it all depends on your platform (OS).

#7
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
You could try a loop, which is very common in command-line applications.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#8
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Personally, I'd never use any of these solutions, except the one provided by suicidal pencil. I prefer to run, and also compile the program, directly from the terminal.

#9
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
[I]#include <stdio.h>
#include <iostream>

using namespace std;

main ()
{
     printf("hello, world/n");
cin.get();
}[/I]

This is what I do, to get it to wait for user input, use cin.get();

#10
soku11

soku11

    Newbie

  • Members
  • PipPip
  • 12 posts

chili5 said:


[I]#include <stdio.h>

#include <iostream>


using namespace std;


main ()

{

     printf("hello, world/n");

cin.get();

}[/I]


This is what I do, to get it to wait for user input, use cin.get();

Personally, I dont like mixing languages, especially I/O functions. And further working on this code can cause strange behavior when mixing cin with scanf or cout with printf. But its my subjective opinion :P

#11
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
That's how I learned to do it, theirs probably better ways but I haven't had any problems using that method as of yet.

#12
Zael

Zael

    Newbie

  • Members
  • PipPip
  • 13 posts

#include <stdio.h>

#include <stdlib.h>


main ()

{

     printf("hello, world[B]/n[/B]");

     [I]system("pause");[/I]

}

The italic part is what you actually need. Even as your program works well, the conclusion is that it's very short and it's execution can't be seen, or it simply blinks for a milisecond, as you said.

The system("pause"); part simply means a pause in the console. It actually waits for you to tap something on keyboard in order to continue..

P.s. Please note the bolded /n part as you entered it on wrong way.. Those are to different chars, which means /n is not the same as \n.
If I understood correctly, you wanted a new line, and it's correct to use only \n.