I am starting out, trying to learn come C programming over the summer (I've had experience in Java for over a year) and I got a problem when I use my compiler.

I am using Dev-C++ and everytime I run my program it quickly closes. Now I know I can easily put a getchar() but I know there is a better way.

So if anyone has any tips I that would be grateful.

Recommended Answers

All 5 Replies

>Now I know I can easily put a getchar() but I know there is a better way.
If you know there's a better way, why are you asking?

>So if anyone has any tips I that would be grateful.
The problem is that the IDE creates a console process for your program, but when your program ends, so does the console process and the window goes bye-bye.

The bad news is that if you want to run your program from the IDE, getchar is the best way. Surprise! Of course, there are issues with existing characters in the input buffer, which tends to be tricky if you want consistent behavior.

If you want, you can run the program from the command line and the problem goes away. But I don't recommend using any of the other suggestions that some of the bozos around here are likely to come up with, like system("PAUSE") or getch . :icon_rolleyes:

>I don't recommend using any of the other suggestions that some of
>the bozos around here are likely to come up with, like system("PAUSE") or getch.

Even Salem did that once... :P

Completely agree though, getchar() is the best solution. What more do you want?

>Even Salem did that once...
Yep, and I've corrected Salem on more than on occasion, so he's not infallible. Very nearly, but not completely.

Not better, but cute, create your own display window with a Windows message box ...

// use a message box to display console output
// compile as GUI
#include <windows.h>
#include <string.h>
#include <stdlib.h>
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  char output[1000];
  char st[20];
  int number, sum;
  
  for ( number = 2; number <= 1000; number += 2 )
    sum += number;
  // convert int to string
  // Pelles C uses _itoa() instead of itoa()
  _itoa(sum, st, 10);
  // build up output string
  strcpy(output, "The sum of even integers from 2 to 1000 is ");
  strcat(output, st);
  
  // now send the result to a Windows message box
  MessageBox(NULL, output, "Show console output in a MessageBox", MB_OK);
  return 0;
}
commented: Thank you +5
Member Avatar for iamthwee

Not better, but cute, create your own display window with a Windows message box ...

// use a message box to display console output
// compile as GUI
#include <windows.h>
#include <string.h>
#include <stdlib.h>
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  char output[1000];
  char st[20];
  int number, sum;
  
  for ( number = 2; number <= 1000; number += 2 )
    sum += number;
  // convert int to string
  // Pelles C uses _itoa() instead of itoa()
  _itoa(sum, st, 10);
  // build up output string
  strcpy(output, "The sum of even integers from 2 to 1000 is ");
  strcat(output, st);
  
  // now send the result to a Windows message box
  MessageBox(NULL, output, "Show console output in a MessageBox", MB_OK);
  return 0;
}

Great idea!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.