Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to write a function in C. The function prints a line with - printf(\

ID: 3866805 • Letter: I

Question

I am trying to write a function in C.

The function prints a line with - printf("Player Move: ");

I need to then take a row and column number seperated by a space.

IF the input is not exactly as follows it must prompt again:

Player Move: 20 10

If the row and column number are below the board size, input is not valid or is equal to 0, it must reprompt with no message.

Using fgetc with stdin gives me a problem as the moment a number is more than one digit it cannot be error checked for size as it takes 2 positions in the array.

if I use sscanf, I cannot check if there is a single space or if the input numbers we only 2 digits, as it would only take 2 if asked.

I have tried:

printf("Player O] ");

position = 0;

while ((next = fgetc(stdin)) != ' ') {

result[position] = (char)next;

position++;

}   

Then pass this to a function that returns 1 if error occurs:

  if (position != 3){

return 1;

} else if ((atoi(&result[0]) > row) || (atoi(&result[2]) > col) || (atoi(&result[0]) == 0) ||

(atoi(&result[2]) == 0)) {

return 1;

} else if ((strcmp(&result[1], " ") != 1)) {

return 1;

} else { return 0;}

Thanks for your help

Explanation / Answer

Following is the code that prints "Player Move" and takes input two numbers separated by space. Checks the validity of the data as specidfied in the program. and if it is not valid then it aging prints the prompt "Player Move".


#include<stdio.h>

int main(){

    int a,b;
    int row = 50; //Not specidied in the question so assumed a value
    int col = 30; //Not specidied in the question so assumed a value
    do {
        printf("Player Move:");
        scanf("%d %d",&a, &b);
    } while (a == 0 || b == 0 || a > row || b > col);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote