Want to learn something new...

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

  1. cybrscot macrumors 6502


    Dec 7, 2010
    Somewhere in Southeast Asia
    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.

    #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

    Jun 7, 2005
    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


    Dec 7, 2010
    Somewhere in Southeast Asia
    Ok, will do.
  4. balamw Moderator


    Staff Member

    Aug 16, 2005
    New England

Share This Page