C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 29th, 2012, 06:03 PM
RedSumac_ RedSumac_ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 RedSumac_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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");
}

Reply With Quote
  #2  
Old October 29th, 2012, 06:10 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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.

Reply With Quote
  #3  
Old October 29th, 2012, 06:42 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old October 29th, 2012, 07:43 PM
RedSumac_ RedSumac_ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 RedSumac_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old October 29th, 2012, 09:34 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,383 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 44 m 29 sec
Reputation Power: 383
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!

Reply With Quote
  #6  
Old November 1st, 2012, 04:12 AM
danieljacob344 danieljacob344 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 6 danieljacob344 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old November 1st, 2012, 06:33 AM
Shaun_B Shaun_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 61 Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #8  
Old November 3rd, 2012, 06:48 PM
RedSumac_ RedSumac_ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 RedSumac_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #9  
Old November 4th, 2012, 09:07 PM
Shaun_B Shaun_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 61 Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C program- using switch causing infinite loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap