Skip to content

Conversation

@driti924
Copy link

@driti924 driti924 commented Dec 16, 2020

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(){

// Input player's choice of placing 'x' or 'o' in variable x 
char x[100];
fgets(x, 100, stdin);

// to check the length of input!
int i;
for (i = 0; x[i] != '\0'; i++);   

// if length of input=1 (i.e i=2) and input value is a digit then its a valid input.
if(i==2 && x[0]-'0'>=1 && x[0]-'0'<=9  ){   
    int m=x[0]-'0';
    return m;
}
else{
    printf("(Enter a valid number between [1 - 9]) ");
    return 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();

Successful exceptional handling
@driti924
Copy link
Author

Hey this is Driti. This is my first opensource contribution.
One check is failing, I dont understand how to get rid of it as of now. I will try to resolve it.
Any feedback will be appreciated.

@Panquesito7 Panquesito7 added the enhancement New feature or request label Dec 16, 2020
@Panquesito7 Panquesito7 requested a review from kvedala December 16, 2020 23:38
Copy link
Collaborator

@kvedala kvedala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good bug fix. But the updated code is not formatted. Please enable GitHub actions in your fork and it will clean up the code for you.

The check that is failing will be resolved once we merge #796

Updated the code after doing the required corrections.
@driti924 driti924 requested a review from kvedala February 13, 2021 11:31
@github-actions
Copy link
Contributor

github-actions bot commented Oct 7, 2021

This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label Oct 7, 2021
@github-actions
Copy link
Contributor

Please ping one of the maintainers once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to ask for help in our Gitter channel or our Discord server. Thank you for your contributions!

@github-actions github-actions bot closed this Oct 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants