Connecting Tech Pros Worldwide Help | Site Map

A little help with this code!

  #1  
Old July 1st, 2009, 07:49 PM
Newbie
 
Join Date: Jun 2009
Posts: 14
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.  

Last edited by Banfa; July 2nd, 2009 at 09:15 AM. Reason: A
  #2  
Old July 1st, 2009, 10:13 PM
Newbie
 
Join Date: Jun 2009
Posts: 14

re: A little help with this code!


This might have something to do with Unix if it affects the code at all.
  #3  
Old July 1st, 2009, 10:54 PM
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 804
Provided Answers: 3

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?
  #4  
Old July 1st, 2009, 11:26 PM
Newbie
 
Join Date: Jun 2009
Posts: 14

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.
  #5  
Old July 2nd, 2009, 04:52 AM
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 804
Provided Answers: 3

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Little help with this if statement daduke40 answers 1 March 26th, 2008 05:13 AM
What is wrong with this code? Bill Reid answers 30 July 1st, 2007 02:05 AM
Almost got it, need just a little help with this java grader program to output correc lifeshortlivitup answers 1 November 3rd, 2006 06:08 AM
New to C++ and Need a little help with a public domain program OZ answers 2 July 22nd, 2005 04:21 PM