#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    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:

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

    int main()
    {
    int input;

    do
    {
    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");
    break;
    case 2:
    printf("You chose a triangle\n\n");
    break;
    case 3:
    printf("You chose a circle\n\n");
    break;
    case 4:
    printf("You chose a rectangle\n\n");
    break;
    case 5:
    printf( "End the program\n" );
    default:
    printf("Error: Please re-enter your selection\n");
    }
    }while(input != 5);

    system("PAUSE");
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    3
    Like the previous poster stated, you could parse your input.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main()
    {
    	unsigned int ch;
    	char num[10];
    	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);
    	
    	return 0;
    }
    The above is just a quick example of what you might do.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    How do I incorporate this into the program? Have it before the switch loop?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    scanf reads from stdin.
    so does fgets(buffer,length,stdin)
    Oh, you'd better find your own reference for fgets.

    Code:
    expected_number_of_conversions == sscanf(buffer,"format",addresses,...);
    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.
    A reference that, by title, looks promising.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    web development company


    I am Just start learn C program this post all programming language like very much.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    62
    Rep Power
    4
    Do you have a clear idea of what you're trying to do? Usually, using switch isn't the best way of doing it.

    Regards,

    Shaun.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by Shaun_B
    Do you have a clear idea of what you're trying to do? Usually, using switch isn't the best way of doing it.

    Regards,

    Shaun.
    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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    62
    Rep Power
    4
    Originally Posted by RedSumac_
    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.
    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.

    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.

    Regards,

    Shaun.
    Last edited by Shaun_B; November 4th, 2012 at 10:55 PM.

IMN logo majestic logo threadwatch logo seochat tools logo