# Thread: scaning two integers with scanf() and the conflict with the while() loop

1. ## 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));
char repeat;
printf("Division:\n");
do{
first = 1 + rand() % 100;
second = 1 + rand() % 100;
printf("\n");
printf("   What is %d / %d = ? ", first, second);
printf("\n");
{
printf("No. Try Again. What is %d / %d = ? ", first, second);
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.

2. here is the fix.

Code:
```int ch;

...

printf("No. Try Again. What is %d / %d = ? ", first, second);
while ((ch = getchar()) != '\n' && ch != EOF);
printf("\n");```
Im not too sure why this infinite loops though. I think that entering a character other than R is causing problems, the T character wont leave the input buffer, so everytime you get back to that scanf you are hitting the T again, loop again, hit T again, loop again, etc.

what that code does is basically clear the T (and anything else) from the input buffer so that the next thing the user inputs is what is read into scanf.

but again, you will have to wait for someone with more experience to come along and explain exactly why entering T breaks this program. (im interested on the details myself)

3. Originally Posted by Brain_Child
here is the fix.

Code:
```int ch;

...

printf("No. Try Again. What is %d / %d = ? ", first, second);
while ((ch = getchar()) != '\n' && ch != EOF);
printf("\n");```
Im not too sure why this infinite loops though. I think that entering a character other than R is causing problems, the T character wont leave the input buffer, so everytime you get back to that scanf you are hitting the T again, loop again, hit T again, loop again, etc.

what that code does is basically clear the T (and anything else) from the input buffer so that the next thing the user inputs is what is read into scanf.

but again, you will have to wait for someone with more experience to come along and explain exactly why entering T breaks this program. (im interested on the details myself)
Wow! It worked! But I don't understand what you did? What is code above doing to avoid the infinite loop if the input is incorrect?

4. Well if you know the user has to place an "R'" as input, and if they also press "T" for the error, you may want to catch this, by scanning for that as well. Then checking your input to see if an "R" or "T" was placed. scanf() will just place erroneous data on the input queue if there is no match and this will have to be cleared out prior to another scanf() call.

Perhaps using fgets() or sscanf() is more appropriate here.

5. OK, never mind you flushed the input buffer, which it makes sense.

Thank you.

6. Originally Posted by slingerland3g
Well if you know the user has to place an "R'" as input, and if they also press "T" for the error, you may want to catch this, by scanning for that as well. Then checking your input to see if an "R" or "T" was placed. scanf() will just place erroneous data on the input queue if there is no match and this will have to be cleared out prior to another scanf() call.

Perhaps using fgets() or sscanf() is more appropriate here.
I know that scanf() is placing erroneous data, that's why I am asking of a better way to write this program.

Anyway, I haven't learned how fgets() or sscanf() works.

Probably an example could help me.