1

I'm learning programmation in C and tried to create a program that asks the user his age. When the user writes his age (for example 18) he gets the message "So you're 18 years old". When I execute the .exe file it automatically closes after you see the message, so fast that you don't see it. Then I added getchar so that the user reads the message and then presses Enter to quite. Here's the program I wrote:

#include <stdio.h>
#include <stdlib.h>

int main()

{
int age=0;
printf("How old are you?\n");
scanf("%d",&age);
printf("So you're %d years old", age);
getchar();
return 0;
}

Unfortunately, when I execute the .exe file, it still closes automatically like if the getchar() doesn't exist and I don't know why.

Scientifica
  • 169
  • 1
  • 10
  • 2
    Use `getch()` instead. – barak manos Nov 09 '14 at 13:12
  • You're welcome, don't forget to click the Green V :) – barak manos Nov 09 '14 at 13:39
  • ^^ there's no green V here. You post a comment not an answer. But I voted up your comment By the way no one voted my question. Didn't got the student badge T_T – Scientifica Nov 09 '14 at 13:44
  • Yes, you got it from me, because the question is constructive (although I'm not sure it hasn't already been asked here). – barak manos Nov 09 '14 at 15:05
  • @barakmanos Worth pointing out that getch() is from conio.h, which doesn't exist in gcc or other ANSI implementations; it's a Turbo-C extension that was picked up by Microsoft C, but has never been part of the standard; console operations are always system-specific (hardware and OS). – Zeiss Ikon Nov 10 '14 at 13:40

2 Answers2

2
scanf("%d",&age);

When the execution of the program reaches the above line,you type an integer and press enter. The integer is taken up by scanf and the \n( newline character or Enter )which you have pressed remains in the stdin which is taken up by the getchar().To get rid of it,replace your scanf with

scanf("%d%*c",&age);

The %*c tells scanf to scan a character and then discard it.In your case,%*c reads the newline character and discards it.

Another way would be to flush the stdin by using the following after the scanf in your code:

while ( (c = getchar()) != '\n' && c != EOF );

Note that c is an int in the above line

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1

You're only having trouble seeing the result because you're starting the program from a windowing environment, and the window closes as soon as its internal tasks are completed. If you run the compiled program from a command line in a pre-existing shell window (Linux, Mac, or Windows), the results will stay on the screen after you're returned to the prompt (unless you've ended by executing a clear-screen of some sort). Even better, in that case, you don't need the extraneous getchar() call.

For Windows, after opening the command prompt window, you'd issue a "cd" command to change to the directory that contains the compiled program, and then type its name. For Linux (and, I presume, Mac, since Mac is UNIX under the hood), you'd need to type ./ ahead of the program name after changing to the appropriate directory with "cd".

Zeiss Ikon
  • 481
  • 4
  • 13
  • You're right! But the user wouldn't open the program using a compiler. – Scientifica Nov 09 '14 at 13:26
  • Given this is an obvious early learning exercise, the only "user" is likely to be the programmer. Also, at least in Linux, command-line only applications are still very common. Been a while since I've used Windows, but I recall it being possible to set up a shortcut to open a window and then keep it open after the program finishes -- packaging such a shortcut (or instructions for creating it) with the program would probably suffice if one were to actually release a text-only, command-line-interface application for a non-Linux OS. – Zeiss Ikon Nov 09 '14 at 13:31
  • Sorry I didn't got your point I'm a beginner and don't any other OS than Windows. Could you clarify that, please? – Scientifica Nov 09 '14 at 13:35
  • Assuming you're clicking on an icon to launch the compiled program, you should be able to alter the settings for the icon to "keep window open" or uncheck "close when program completes" or something similar. – Zeiss Ikon Nov 09 '14 at 22:43
  • Yes it's true. In fact in the compiler the window keeps opened. But then if I want to give the .exe file to any person, this person will simply execute the file without any compiler. It's true that this is just an exercise. But I'm learning programation and who knows one time I'll need to make a similar program. Then I'll find this problem and wouldn't be able to fix it. It's through exercises that we learn. – Scientifica Nov 10 '14 at 11:35
  • Hence my suggestion that you package the program with instructions how to make the icon stay open if you distribute it. Simplest is to have them run it from a command prompt window, which is how non-GUI applications are built to work. – Zeiss Ikon Nov 10 '14 at 13:44
  • Nice idea. However it makes first running the application a bit harder (must use cmd instead of a simple double click), and secondly I tried it: I runned the application using cmd but I got the same result: the window closes again automatically. – Scientifica Nov 10 '14 at 16:20
  • In Windows Vista and 7, at least (and surely in earlier versions, back to 3.0) you can open a command prompt window. In XP, as I recall, one way was to "run program" and type "command" -- in 7 (on my girlfriend's computer) there's a menu entry for "command prompt". This is different from using "run program" directly, in that the window, open before the program starts, will stay open after it finishes. – Zeiss Ikon Nov 10 '14 at 20:29
  • Ok I see. In fact I use XP which has ended in fact. I don't need to upgrade to Windows 7 or 8 still I use my computer for math, watching animes and playing a MOBA game. So you're right: no need for a `getch()`. Thank you for your explanations and your patience. – Scientifica Nov 10 '14 at 21:47