October 29th, 2012, 06:03 PM
C program- using switch causing infinite loop
I wrote the program to keep looping as long as a user entered numerical input. When the appropriate "End program" is used, the program terminates. If a user entered 9 for example, the program would say error and ask them to re-enter. However, it goes into an infinite loop when a user enters a character, such as p. How do I correct this? I want it to read the same error line.
Here it is:
printf ("1. You chose a square\n");
printf ("2. You chose a triangle\n");
printf ("3. You chose a circle\n");
printf ("4. You chose a rectangle\n");
printf ("5. End the program\n");
printf( "Enter your selection: \n\n" );
scanf( "%d", &input );
switch ( input )
case 1: /* Prints the selection chosen by the user*/
printf("You chose a square\n\n");
printf("You chose a triangle\n\n");
printf("You chose a circle\n\n");
printf("You chose a rectangle\n\n");
printf( "End the program\n" );
printf("Error: Please re-enter your selection\n");
}while(input != 5);
October 29th, 2012, 06:10 PM
Don't use scanf() for user input. Use fgets() and parse the string instead.
To parse the string you can use strtol(), or sscanf(), or a myriad other methods.
October 29th, 2012, 06:42 PM
Like the previous poster stated, you could parse your input.
The above is just a quick example of what you might do.
unsigned int ch;
char *nptr = NULL;
int ans = 0;
nptr = num;
while ((ch = fgetc(stdin)) != '\n')
if ( isdigit(ch) )/*filter out non-digits*/
*nptr++ = ch;
*nptr = '\0';
ans = atoi(num);
fprintf(stdout, "num->%s, ans->%d\n", num, ans);
October 29th, 2012, 07:43 PM
How do I incorporate this into the program? Have it before the switch loop?
October 29th, 2012, 09:34 PM
scanf reads from stdin.
so does fgets(buffer,length,stdin)
Oh, you'd better find your own reference for fgets.
oops, you'd better investigate sscanf. It's already known that I'm less familiar with the return values of scanf/printf functions than one would hope.
expected_number_of_conversions == sscanf(buffer,"format",addresses,...);
A reference that, by title, looks promising.
[/code] are essential for python code and Makefiles!
November 1st, 2012, 04:12 AM
web development company
I am Just start learn C program this post all programming language like very much.
November 1st, 2012, 06:33 AM
Do you have a clear idea of what you're trying to do? Usually, using switch isn't the best way of doing it.
November 3rd, 2012, 06:48 PM
Interesting, because switch was suggested by my instructor as the best way to do it. I want to avoid stream errors and only have the 5 numbers be considered valid input.
Originally Posted by Shaun_B
November 4th, 2012, 09:07 PM
In my experience, academics like switch case because it makes their assignments more readable and easier to mark. But from the original code, I'd reckon that you'd get an error if you enter a character other than a number.
Originally Posted by RedSumac_
You could do an if( input<1 || input>5 ) first and report the error there. And if you're going to use while like that, you need do ... while I think.
Edit: Sorry, you were using do ... while - you hadn't lined up your code properly, so I missed it.
Last edited by Shaun_B; November 4th, 2012 at 09:55 PM.