scaning two integers with scanf() and the conflict with the while() loop
Hi everyone, I'm having trouble making work a function program related to simple math calculations.
In this case is in the Division function.
For this project I have to make the user answer some simple divisions problems that must also include an integer result with it's modulus.
For example: The results should follow long division notation (10 / 6 results in 1 R 4). When inputing the answer for division use 1R4 for the input format.
Here is what I have done by now:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "division.h"
/* This function will create random exercises of multiplication */
int division()
{
srand(time(NULL));
int first, second, answerout, answeroutr, answerin, answerinr;
char repeat;
printf("Division:\n");
do{
first = 1 + rand() % 100;
second = 1 + rand() % 100;
answerout = first / second;
answeroutr = first % second;
printf("\n");
printf(" What is %d / %d = ? ", first, second);
scanf("%dR%d", &answerin, &answerinr);
printf("\n");
while ( answerin != answerout || answerinr != answeroutr )
{
printf("No. Try Again. What is %d / %d = ? ", first, second);
scanf("%dR%d", &answerin, &answerinr);
printf("\n");
}
printf("Correct!\n");
printf("\n");
printf("Play Again? ");
getchar();
repeat = getchar();
while ( getchar() != '\n');
} while(repeat == 'Y' || repeat == 'y');
return EXIT_SUCCESS;
}
I have highlighted where the conflict is.
What I'm trying to solve is that if the user input another value that not is INTERGERRMODULUS i.e. Instead of putting as input 1R4 in the 10 / 6 problem, they put 1T4. The while loop should appear saying that the value is wrong. But in this case what happens is that it creates an infinite loop if the input is incorrect.
What suggestions do you gave me to solve this problem?
Thank you.