Want to learn something new...

Discussion in 'Mac Programming' started by cybrscot, Jan 24, 2011.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    The current programs that I am writing allow me to only use it once, then I must enter ./a.out again, then I can use the program once more, then I must enter ./a.out again, and so on. Is there a piece of code I can add that will allow me to enter a value, then return to the scanf line and allow me to enter another value, and so on?

    In the below code, or any that I write that test different values entered by a user, instead of having to enter each value one at a time, and enter ./a.out again to run the program again, I'd like to have the terminal prompt blink ready at the scanf line, ready to accept another value for the printf "Enter Difference Between posted and actual speed". This way I can play with the program for a while and test different user inputs more conveniently than having to enter ./a.out every time. I'm not even sure what you call that type of functionality, so I'm not sure how to look it up.

    Code:
    #include <stdio.h>
    
    main ()
    
    {
    
    	float speed ;
    	
    	printf ("Enter Difference Between Posted and Actual Speed: ") ;
    	scanf ("%f", &speed) ;
    	
    	if (speed > 20 )
    		printf ("Fine is $25.00\n" ) ;
    	else if (speed > 30 )
    		printf ("Fine is $35.00\n" ) ;
    	else if (speed > 40 )
    		printf ("Fine is $100.00\n") ;
    	else if (speed > 50 )
    		printf ("Apprehend suspect and begin booking proceedings\n") ;
    		
    	return 0 ;
    	
    }	
     
  2. mheidt macrumors member

    Joined:
    Jun 7, 2005
    #2
    Look up the keyword "while"

    Put everything into the while loop and define a key, that is interrupting the loop

    Maybe something like "Try again?" expecting y/n.

    or you are scanning for any key (not a number only) and you interrupt, if that key is not a number.
     
  3. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #3
    Ok, will do.
    Thanks
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England

Share This Page