/* alarm1.cpp * an alarm example of synchronous version */ #include #include int main() { int seconds; char line[128]; char message[64]; while(1) { printf("Alarm>"); if (fgets(line, sizeof (line), stdin) == NULL) exit(0); if (strlen (line) <=1) continue; /* separate the number of seconds to wait (%d) from the message * string to print (%64[^\n] , the rest of the line, up to 64 * characters excluding newline). */ if (sscanf(line, "%d %64[^\n]", &seconds, message ) <2 ) { fprintf( stderr, "Bad Command\n"); } else { sleep (seconds); printf( "(%d) %s\n", seconds, message); } } }