Thread: Basic C programming: using kbhit to stop Loop

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    3

    Basic C programming: using kbhit to stop Loop

    Hello,

    I am still a beginner at C and I am writing a program where the velocity and angle are required from the user.

    The way I wanted to do that is to have a loop (going from 0 to 300 for example for velocity) that would quickly print out the numbers (show the numbers going quickly from 1 to 2 to 3 etc.) and as soon as the user presses enter the loop stops and the value of the loop at that time should be stored.

    I am not sure how to do it though, I would really appreciate an example on how to do that

    THANKS!

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Hi, Random49. I think it would be better if the user could type in the values of the velocity and the angle - you still could limit the values to be accepted. However, if you prefer to do the loop as you said, it would be better if the loop restarted, in case the user hadn't entered anything yet. Please tell me which way you'd prefer to do it, and I will give you some help with the code.

    Regards!

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    3
    Hello,

    I already had it set up such that the user can type in the values like that:

    Code:
    /* get the projectile angle */
    double get_projectile_angle(void)
    {
    	double angle;
    
    
    	printf("\nEnter angle of projectile (0 to 360):  ");
        scanf("%lf",&angle);
    
    
    	return angle;
    }
    It didn't limit the values and I guess I can try to do that but i would prefer it to be in a loop that restarts. THANKS!!!

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Hi! I apologize, I'm not sure how to make the program print the values continuously while waiting to the input. However, extending you code above, you can limit the output like this:

    Code:
    /* get the projectile angle */
    double get_projectile_angle(void)
    {
        double angle;
     
     
        printf("\nEnter angle of projectile (0 to 360):  ");
        scanf("%lf",&angle);
        
        while ( ( angle < 0 )  && ( angle > 360 ) )
        {
              printf("\nEnter angle of projectile (0 to 360):  ");
              scanf("%lf",&angle);
        }
     
        return angle;
    }
    If you do it this way, I recommend you to test the program entering values really close to the limits. If the program do not behave correctly, check this:

    I can't figure this out!

    Regards!

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    3
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Infinite loop will not stop.
    By Retribution in forum C++ Programming
    Replies: 7
    Last Post: 08-10-2011, 04:56 AM
  2. Can't stop endless loop?...
    By tvred in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2010, 10:27 PM
  3. how to stop the loop
    By amits in forum C++ Programming
    Replies: 5
    Last Post: 05-19-2004, 10:46 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. loop stop
    By pode in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2001, 08:36 PM