The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C program- using switch causing infinite loop
Discuss C program- using switch causing infinite loop in the C Programming forum on Dev Shed. C program- using switch causing infinite loop C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 29th, 2012, 06:03 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 27 m 32 sec
Reputation 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");
}
|

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
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 71
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
|
|
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.
|

October 29th, 2012, 07:43 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 27 m 32 sec
Reputation Power: 0
|
|
|
How do I incorporate this into the program? Have it before the switch loop?
|

October 29th, 2012, 09:34 PM
|
 |
Contributing User
|
|
|
|
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!
|

November 1st, 2012, 04:12 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 6
Time spent in forums: 2 h 59 m 21 sec
Reputation Power: 0
|
|
|
web development company
I am Just start learn C program this post all programming language like very much.
|

November 1st, 2012, 06:33 AM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 61
  
Time spent in forums: 1 Day 42 m 57 sec
Reputation Power: 2
|
|
|
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.
|

November 3rd, 2012, 06:48 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 27 m 32 sec
Reputation Power: 0
|
|
Quote: | 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.
|

November 4th, 2012, 09:07 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 61
  
Time spent in forums: 1 Day 42 m 57 sec
Reputation Power: 2
|
|
Quote: | 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 09:55 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|