Connecting Tech Pros Worldwide Forums | Help | Site Map

A little help with this code!

Newbie
 
Join Date: Jun 2009
Posts: 14
#1: Jul 1 '09
I have to modify the code below so that it counts the number of times the user presses Ctrl-C. The new program will print th emessage OUCH!, then OUCH!!, where the number of exclamation points equals the number of times the handler has been called.

In addition, to printing an increasing number of exclamation points, the program should accept an integer as a command line argument. After the user presses Ctrl-C that many times, the program should exit.

In addition the line sleep(1); has the following error message: implicit declaration of function 'int sleep(...)'

Please advise.

Thanks.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <signal.h>
  3.  
  4. main()
  5. {
  6.     void f(int);
  7.     int  i;
  8.     signal( SIGINT, f );
  9.     for(i=0; i<5; i++ ){
  10.         printf("hello\n");
  11.         sleep(1);
  12.     }
  13. }
  14. void f(int signum)
  15. {
  16.     printf("OUCH!\n");
  17. }
  18.  

Newbie
 
Join Date: Jun 2009
Posts: 14
#2: Jul 1 '09

re: A little help with this code!


This might have something to do with Unix if it affects the code at all.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#3: Jul 1 '09

re: A little help with this code!


Quote:

Originally Posted by stayit View Post

In addition the line sleep(1); has the following error message: implicit declaration of function 'int sleep(...)'

The error message occurs because there is no function prototype to explicitly declare the function. Lacking an explicit declaration, the compiler implicitly assumes a default declaration: that the function returns an int and can take an arbitrary parameter list (which may not be what the function really does). The error message alerts you that the implicit declaration took place. You need to include whichever header declares that function.

Is that your only problem?
Newbie
 
Join Date: Jun 2009
Posts: 14
#4: Jul 1 '09

re: A little help with this code!


I also have the problem of trying to get the number of exclamation points in the code as noted above.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#5: Jul 2 '09

re: A little help with this code!


To print a string of exclamation points whose length is equal to the number of Ctrl-C inputs received involves three steps:
  1. Detect Ctrl-C event
  2. Keep a count of those events
  3. Report the event count via string of exclamation points.
Which of those steps are you having trouble with?
Reply