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

  1. #1
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70

    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.
    Last edited by georgio777; 11-12-2009 at 01:04 AM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    here is the fix.

    Code:
    int ch;
    
    ...
    
    		printf("No. Try Again. What is %d / %d = ? ", first, second);
                                    while ((ch = getchar()) != '\n' && ch != EOF);
    		scanf("%dR%d", &answerin, &answerinr);
    		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. #3
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by Brain_Child View Post
    here is the fix.

    Code:
    int ch;
    
    ...
    
    		printf("No. Try Again. What is %d / %d = ? ", first, second);
                                    while ((ch = getchar()) != '\n' && ch != EOF);
    		scanf("%dR%d", &answerin, &answerinr);
    		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?
    Last edited by georgio777; 11-12-2009 at 10:10 AM.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    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. #5
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    OK, never mind you flushed the input buffer, which it makes sense.

    Thank you.

  6. #6
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by slingerland3g View Post
    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.

Popular pages Recent additions subscribe to a feed