4 Replies - 1250 Views - Last Post: 11 November 2007 - 08:23 PM Rate Topic: -----

#1 Nidza   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 18-March 07

Infinite loops, grrr...

Posted 10 November 2007 - 05:58 PM

Okay.My problem is just a small part of a bigger problem which is a program I'm doing atm. Blablabla, lets get on it already.

I made a copy of my problem, so it should be understandable for all. I'm using C and DJGPP compiler.

#include <stdio.h>

int main()

{
int option;
while (1)
{
 printf("Type 1,2 or 3 :");
 scanf("%d",&option);


switch(option)
		{
case 1: 
	printf("Oneeeeeeeeee");
		break;
case 2: 
	printf("Twoooooooooo");
		break;
case 3: 
	printf("Threeeeeeeee");
		break;
default:
printf("wtf?! try again..");
break;
	   }

}
return 0;
}




My question is... Why is my program going into the infinite loop if I input any character instead of a number? I still think he'll recognize the char as a "default", break the case, go back to the beginning and ask for the option input again...Or not ? Thanks in advance

Nidza

This post has been edited by Nidza: 10 November 2007 - 05:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Infinite loops, grrr...

#2 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Infinite loops, grrr...

Posted 10 November 2007 - 11:58 PM

You'll need to flush the input buffer to stop the loop from running infinitely.
Use the fflush(stdin); function like this:
#include <stdio.h>

int main()

{
int option;
while (1)
{
printf("Type 1,2 or 3 :");
scanf("%d",&option);


switch(option)
		{
case 1: 
	printf("Oneeeeeeeeee");
		break;
case 2: 
	printf("Twoooooooooo");
		break;
case 3: 
	printf("Threeeeeeeee");
		break;
default:
printf("wtf?! try again..");
break;
	   }
/* ADD THESE TWO STATEMENTS */
fflush(stdin);
option=0;

}
return 0;
}

Your code should work fine :)
Was This Post Helpful? 0
  • +
  • -

#3 Nidza   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 18-March 07

Re: Infinite loops, grrr...

Posted 11 November 2007 - 06:48 AM

View Postborn2c0de, on 10 Nov, 2007 - 11:58 PM, said:

You'll need to flush the input buffer to stop the loop from running infinitely.
Use the fflush(stdin); function like this:
#include <stdio.h>

int main()

{
int option;
while (1)
{
printf("Type 1,2 or 3 :");
scanf("%d",&option);


switch(option)
		{
case 1: 
	printf("Oneeeeeeeeee");
		break;
case 2: 
	printf("Twoooooooooo");
		break;
case 3: 
	printf("Threeeeeeeee");
		break;
default:
printf("wtf?! try again..");
break;
	   }
/* ADD THESE TWO STATEMENTS */
fflush(stdin);
option=0;

}
return 0;
}

Your code should work fine :)

Perfect, thanks buddy :)
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6841
  • View blog
  • Posts: 31,532
  • Joined: 10-May 07

Re: Infinite loops, grrr...

Posted 11 November 2007 - 10:10 AM

View PostNidza, on 10 Nov, 2007 - 05:58 PM, said:

Okay.My problem is just a small part of a bigger problem which is a program I'm doing atm. Blablabla, lets get on it already.

I made a copy of my problem, so it should be understandable for all. I'm using C and DJGPP compiler.

My question is... Why is my program going into the infinite loop if I input any character instead of a number? I still think he'll recognize the char as a "default", break the case, go back to the beginning and ask for the option input again...Or not ? Thanks in advance

Nidza


If you can't compute the results of a character, & must have a number, issue isdigit(); to verify that the input was in fact a digit, before entering the loop. isdigit(); can be found in ctype.h.
Was This Post Helpful? 0
  • +
  • -

#5 Nidza   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 18-March 07

Re: Infinite loops, grrr...

Posted 11 November 2007 - 08:23 PM

View Postno2pencil, on 11 Nov, 2007 - 10:10 AM, said:

View PostNidza, on 10 Nov, 2007 - 05:58 PM, said:

Okay.My problem is just a small part of a bigger problem which is a program I'm doing atm. Blablabla, lets get on it already.

I made a copy of my problem, so it should be understandable for all. I'm using C and DJGPP compiler.

My question is... Why is my program going into the infinite loop if I input any character instead of a number? I still think he'll recognize the char as a "default", break the case, go back to the beginning and ask for the option input again...Or not ? Thanks in advance

Nidza


If you can't compute the results of a character, & must have a number, issue isdigit(); to verify that the input was in fact a digit, before entering the loop. isdigit(); can be found in ctype.h.

Checking it now, cheers.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1