Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
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.

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 ;
	
}
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.