Help with C "Hello World" - How to Stop It Closing?

In summary, to keep the input window open, you can use cin.getline() to obtain the input value, and then use a while loop to keep the input window open.
  • #1
madmike159
Gold Member
371
0
I started learning C not long ago. Like most people I started with "Hello World". I relised that it closes itself unless you tell it not to.

I have this code

#include <stdio.h>

int main(char *argv[])
{
printf("Hello World!\n");
getchar();
return 0;
}

It makes it close when you press enter. The problem is when you do read input commands like scanf() it closes when you press enter. Does anyone know how I can change this to so it closes when you press something else (like esc, or Ctrl+enter)?
 
Technology news on Phys.org
  • #2
Try using cin.getline(someString, 256) for input.
 
  • #3
Contrapositive said:
Try using cin.getline(someString, 256) for input.

The OP wanted C, not C++.
 
  • #4
You're worrying about the black-box window that pops up on a Windows computer with something like Visual Studio; this has nothing specifically do to with your program at all.

Try opening a command-line window and running your programs by typing their names there.

- Warren
 
  • #5
madmike159 said:
I started learning C not long ago. Like most people I started with "Hello World". I relised that it closes itself unless you tell it not to.

I have this code

#include <stdio.h>

int main(char *argv[])
{
printf("Hello World!\n");
getchar();
return 0;
}

It makes it close when you press enter. The problem is when you do read input commands like scanf() it closes when you press enter. Does anyone know how I can change this to so it closes when you press something else (like esc, or Ctrl+enter)?

When you call getchar(), it returns a value. The value will be a code that is different depending on what key was pressed. try saying something like char key = getchar(); and then "key" will have the value of the keypress. If you want to turn this into a "pause until return is pressed", you can do something like:

Code:
while (getchar() != '\n') {} // Loop forever until return

... I *THINK* that works.

However chroot's advice is probably closer to what you want. If you do what chroot says then the window will not close when the program ends, it will just stay open. Then the "press return" nonsense will not be necessary.
 

Related to Help with C "Hello World" - How to Stop It Closing?

1. What is "Hello World" in C and why does it keep closing?

"Hello World" is a common phrase used in programming to indicate a basic program that displays the words "Hello World" on the screen. It is often used as a simple introductory program for beginners. The program may be closing due to an error or lack of proper code to keep it running.

2. How do I prevent "Hello World" from closing?

To prevent "Hello World" from closing, you will need to add a statement that allows the program to wait for user input before closing. This can be done by including the getchar() function at the end of your code. This will pause the program and wait for the user to press a key before closing.

3. Why is my "Hello World" program closing immediately after opening?

This may be due to an error in your code. Make sure you have included all necessary libraries and have properly declared your main function. Check for any syntax errors or missing semicolons that may cause the program to close immediately.

4. Can I change the message in "Hello World" to something else?

Yes, you can change the message in "Hello World" to anything you want. Simply replace the words "Hello World" with your desired message in the printf() function. Keep in mind that the program will still need a statement to wait for user input before closing, such as getchar().

5. How can I use "Hello World" to learn more about programming in C?

"Hello World" is a great starting point for learning about programming in C. You can experiment with different messages, change the code to add more functionality, and explore different concepts such as variables and functions. It is also helpful to read through the code and understand what each line does. Additionally, there are many online resources and tutorials available to help you learn more about C programming using "Hello World" as a starting point.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
4
Views
857
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top