Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Successful exceptional handling
Description of Change
Issue: #729
Fixes / Changes
#line no 16
[+] #include <string.h>
First, I changed type of 'n' from int to char, so that if player enters any character other than a number it still gets consumed and does not remain in input buffer. As this input buffer effects the scanning of next number(i.e 'l' here) and doesn't scan player's choice to play again.
#line no 41
[-] int n = 0;
[+] char n = '0';
Second, to avoid infinite loop on getting any character on line no.247 , I have added a new function 'TakeInput'. Thus a player can enter any character/s or number(not in range of 1-9) without entering infinite loop. This time a message will prompt them to give valid input.
Also note, my code is always using TakeInput function to input players choice to avoid the same problem of unused input buffer.
#line no 21
[+] static int TakeInput(); // used to take input from player and handle exceptions accordingly
#line no 85
[+] int TakeInput(){
}
Other changer are made accordingly. Like
#line no 54
[-] scanf("%d", &n);
[+] scanf(" %c", &n);
#line no 59
[-] case 1:
[+] case '1':
#line no 62
[-] case 2:
[+] case '2':
For better visibility
#line no 66
[-] printf("THANK YOU and EXIT!");
[+] printf("THANK YOU and EXIT!\n");
lines 105 and 176
[-] scanf("%d",&m)
[+] m=TakeInput()
#line no 182
[-] scanf("%d", &e1);
[+] e1=TakeInput();
#line no 242 and 252
[-] scanf("%d", &n1);
[+] n1=TakeInput();
For better visibility
line no 272
[-] printf("\n Computer placed at %d position\n", e + 1);
[+] printf("\nComputer placed at %d position\n", e + 1);
#line no 301 and 311
[-] scanf("%d", &n1);
[+] n1=TakeInput();