The assignment: Write a program to do the following. When the program starts, you start type characters from the keyboard. The program should print out a warning if you type any characters that are not digits, i.e., from '0' to '9'. In addition, when you stop the typing (by typing a non-character), your program will print out the number of digits and number of non-digit characters you have typed.
Code:
#include <stdio.h>
#include <cstdlib>
int main() {
int digit = 0;
int nondigit = 0;
int c;
while((c=getchar()) != EOF)
{
if(c>='0'&&c<='9')
digit++;
else
{
printf("ERROR:NOT A DIGIT\n");
nondigit++;
}
}
printf("%d\n", digit);
printf("%d\n", nondigit);
system("pause");
return 0;
}
My code recieves input from the keyboard and prints out the correct output when a non-character is typed, but I can't seem to get it to print the error message after a non-digit is input without hitting enter.