Your primary problem is when you use scanf()
to read an integer from stdin
, you also input '\n'
as a result of pressing [Enter]. scanf()
using the "%d"
conversion specifier does not read the '\n'
and leaves it in the input buffer (stdin
here) waiting to be read by the next input. When getch()
encounters the '\n'
in the input buffer -- it takes that as your input and happily exits -- nothing to wait for.
This is one of many reasons why you should use fgets()
for all user-input. fgets()
will read an entire line of data (into a sufficiently sized buffer/character array) including the trailing '\n'
. scanf()
is so full of pitfalls for the new programmer -- just like this one, you should learn how to handle input in a way that what remains in the input buffer does not depend on the scanf()
conversion specifier used or whether a matching-failure occurs.
fgets()
is recommended for all new (and seasoned) programmers.
How do I get my Number out from what fgets()
reads?
Answer: sscanf()
. Simply parse the string filled by fgets()
using sscanf()
instead or attempting to read directly from the input stream with scanf()
. There are many benefits to this approach, including solving the problem you have run into. Just pass your buffer as the first parameter to sscanf()
. It's just like using scanf()
from the conversion there on. Don't forget to VALIDATE every input and every conversion...
What else is Wrong in Your Code?
void main()
is not defined for a standards conforming program. (it is an anachronism - a holdover from an ancient time) Unless you are programming in a freestanding environment (without the benefit of any OS), in a conforming implementation, the allowable declarations for main
for are int main (void)
and int main (int argc, char *argv[])
(which you will see written with the equivalent char **argv
). See: C11 Standard - §5.1.2.2.1 Program startup(p1). See also: What should main() return in C and C++? In a freestanding environment, the name and type of the function called at program startup are implementation-defined. See: 5.1.2.1 Freestanding environment
Space your code adequately. Errors are harder to find (and your old professor will go blind trying to read) code with all characters crammed together. The compiler doesn't care -- but humans do.
Lastly, always ensure you output a '\n'
as the final output from your program to make your program POSIX compliant. Otherwise, it will mess up the command-line prompt for any user not using windows.
Putting it altogether, you can do something similar to:
#include <stdio.h>
#if defined (_WIN32) || defined (__MINGW32__)
#include <conio.h>
#endif
int main(void)
{
char buf[1024]; /* buffer to hold entire line of input */
int x = 0, y = 0, z = 0;
fputs ("Enter a number: ", stdout);
/* read entire line of input into buf - validate EVERY input */
if (fgets (buf, sizeof buf, stdin) == NULL) {
return 1;
}
/* parse number from line using sscanf() - validate EVERY conversion */
if (sscanf (buf, "%d", &x) != 1) {
fprintf (stderr, "error: invalid integer input '%s'.\n", buf);
return 1;
}
z = x / 2; /* WARNING - integer division! */
for (y = 2; y <= z; y++) {
if (x % y == 0) {
printf ("%d is not prime\n", x);
break;
}
}
if (y == z + 1) {
printf ("%d is prime\n", x);
}
#if defined (_WIN32) || defined (__MINGW32__)
getch();
#endif
}
(note: the preprocessor macros just conditionaly include conio.h
and call getch()
if they are available -- otherwise they just skip that code)
Note the warning about integer division -- while intentional in this case, make sure you understand what it is. Let me know if you have further questions.