Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- enter_number_in_max_5_attempts.c
- Program that will stop the user from inputting a number between 1 and 3
- after 5 invalid inputs.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- https://www.facebook.com/dmilicev
- */
- #include <stdio.h>
- int main(void)
- {
- int number, good=0, counter=0;
- do { // do-while loop must be executed at least one time
- counter++; // count attempts
- printf("\n\n %d. attempt. Enter a number between 1 and 3, number = ", counter );
- scanf("%d",&number);
- if ( number>=1 && number<=3 ) // condition for good input
- good=1;
- } while( !good && counter<5 );
- if ( counter == 5 )
- printf("\n\n \t You have used all 5 allowed attempts to enter a number. \n\n" );
- else
- printf("\n\n \t In %d. attempt entered number is %d \n\n", counter, number );
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment