0

This code runs perfectly in my IDE; I even tried it in an online compiler just to be sure, but when I try to open the .exe it will only ask for the integer and automatically close. I tried it with another program from the school where I asked for like 15 numbers but right before a goodbye message it just closes. Any idea how to fix cmd?

#include <stdio.h>

int main()
{
    int numberOfWidgets;

    printf("Give me a number: ");
    scanf("%d", &numberOfWidgets);

    printf("You choose the number: %d", numberOfWidgets);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    you can run program from command prompt cmd, find location of file, and type your file name to execute from there. Don't directly open the .exe file. – Asphodel Jan 21 '22 at 18:56
  • Welcome to SO. How do you run your .exe file? Run it in a shell or double click it in Explorer? If you use the latter, then you will just not see any output after you entered the number because the program just terminates and closes the windows immediately. Add some code to waitfor some extra input – Gerhardh Jan 21 '22 at 18:56
  • Easiest way to do it right: use File Explorer to find your exe. Click on the address bar and type “cmd” and press Enter. That’ll get you a Windows Console window opened to the same directory as your exe. Type the name of your exe and watch it run. – Dúthomhas Jan 21 '22 at 19:03
  • 2
    If you are running it by double clicking on it in the file explorer, then the terminal window it creates will automatically close when the program exits without giving you time to read the final output. Try running the program from an existing terminal window by entering its name at the command prompt. (You may need to enter the drive letter: and CD to the correct directory first.) – Ian Abbott Jan 21 '22 at 19:04
  • duplicates: [How to make sure console doesn't close immediately when running code?](https://stackoverflow.com/q/60811419/995714), [Why does my program's output flash and close in Windows?](https://stackoverflow.com/q/1048975/995714), [How to stop console application from exiting immediately?](https://stackoverflow.com/q/2529617/995714) – phuclv Jan 22 '22 at 02:23
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Feb 01 '22 at 09:59

1 Answers1

0

You need to add two lines to the end of your program:

int main() {
    ...
    getchar();
    getchar();

    return 0;
}

The first call to getchar will clear the return key you pressed when you entered the number, the second one will stop and wait for a key to be pressed.

That way, your program will not exit until you press a key, and you will be able to read the output.

Lev M.
  • 6,088
  • 1
  • 10
  • 23
  • 1
    it's a bad idea because when running from cmd it'll require another enter to exit – phuclv Jan 22 '22 at 02:20
  • @phuclv that is only a problem if you are making a batch tool. It looks like OP's program is designed to be interactive, and if needed a message "Hit any key to exit" could easily be added. – Lev M. Jan 22 '22 at 12:07