3 Replies - 1096 Views - Last Post: 27 July 2011 - 02:20 PM Rate Topic: -----

#1 defunktlemon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-November 10

error checking - infinite looping

Posted 26 July 2011 - 06:02 AM

Hi
I have constructed this program which creates a random number and asks the user to guess what it is. The users' first guess is replied with either "the number is too high or low. guess again". I am trying to do error checking. If the user inputs a negative number or a number about 1023 then this error check works ok. But when I input a letter or special character, the program enters into an infinite loop. Can you tell me why this is happening and what I need to do to correctly check this, please?
thank you


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
	//seed PRNG
	srand(time(NULL));
	
	//pick pseudorandom number in [0, 1023]
	int randomNum = rand() % 1024;
	int num;
	
	//prompts to user and guesses from user
	printf("I'm thinking of a number between 0 and 1023. What is it?\n");
	scanf("%d", &num);
	while(num != randomNum){
		if(num < 0 || num > 1023){
			printf("Sorry. That is not a number between 0 - 1023. Please try again\n");
			scanf("%d", &num);
		}
		else if(num > randomNum){
			printf("Sorry. The number is lower than that. Please try again\n");
			scanf("%d", &num);
		}
		else if(num < randomNum){
			printf("Sorry. The number is higher than that. Please try again\n");
			scanf("%d", &num);
		}
		else{
			printf("That is not a number. Please input a number between 0 - 1023\n");
			scanf("%d", &num);
		}
	}
	printf("Well done! That was the number I was thinking of\n");
		   
	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: error checking - infinite looping

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: error checking - infinite looping

Posted 26 July 2011 - 07:01 AM

Quote

But when I input a letter or special character, the program enters into an infinite loop.


Since you are using scanf("%d",var); to retrieve the data when you enter an invalid number the file stream will go into an error state and before you can use the stream again you must first clear the error with clearerr(fp), and then remove these bad characters from the input stream. In your case since you are dealing with input from stdin you would replace fp with stdin. To remove the bad characters you will need to remove each character from the stream. This is why it is usually better to use fgets() to retrieve the data into a C-string and then use sscanf() to process that stream. Retrieving a character is less likely to cause a file stream error and if the C-string stream processing fails it will not affect the underlying file stream.

Jim

This post has been edited by jimblumberg: 26 July 2011 - 07:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 defunktlemon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-November 10

Re: error checking - infinite looping

Posted 27 July 2011 - 01:43 PM

I'm thankful for your reply Jim, but this is my first program and I don't understand what a stream is. Would you mind describing that a bit?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: error checking - infinite looping

Posted 27 July 2011 - 02:20 PM

Here is a good explanation of the stream concept: C++ input output streams.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1