The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Infinite loop.
Discuss Infinite loop. in the C Programming forum on Dev Shed. 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:
|
|
|

April 26th, 2003, 11:58 PM
|
|
Junior Member
|
|
Join Date: Apr 2003
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Infinite loop.
hello
I had a program and it look okay, but the switch had cause an infinite loop when I key in a letter instead of any number. Number is okay. I cannot figure it out can anyone free to help me out. Here is part of my program with some change to reduce to the problem area.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int option;
printf("\tHow are you,please enter your choice\n");
printf("\tEnter 1, 2, 3, 4\n");
printf("\n\tEnter choice==> ");
scanf("%d", &option);
switch(option)
{
char response;
case 1  rintf("How are you\n");break;
case 2  rintf("How are you\n");break;
case 3  rintf("How are you\n");break;
case 4: printf("\n\n\t\t Are you sure you want to Quit? Y / N ");
response=getche();
if(response=='y' || response== 'Y')
{
printf("\n\n\t\t\t Have a nice day!\n");
exit(0);break;
}
else if(response=='n' || response=='N')
{
main();break;
}
else
{
printf("\n\n\t\t\t\t ERROR");
main();break;
}
default  rintf("\nInvaild entry, please enter again.\n");
main();break;
}
}
|

April 27th, 2003, 12:24 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
That's because you're recursively calling main() within itself. Specifically, your problem is for the default case.
Code:
default: printf("\nInvaild entry, please enter again.\n");
main();break;
Change it to:
Code:
default: printf("\nInvaild entry, please enter again.\n");
break;
BTW you're also recursively calling main() within the case 4 block. Go ahead and remove that call as well.
On a totally unrelated note, it is better to declare main() as int main(void), rather than void main(). Also, you might want to put your code in a while or for loop, like this:
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
int option;
int done = 0;
while (!done) {
printf("\tHow are you,please enter your choice\n");
printf("\tEnter 1, 2, 3, 4\n");
printf("\n\tEnter choice==> ");
scanf("%d", &option);
switch(option)
{
char response;
case 1: printf("How are you\n"); done = 1; break;
case 2: printf("How are you\n"); done = 1; break;
case 3: printf("How are you\n"); done = 1; break;
case 4: printf("\n\n\t\t Are you sure you want to Quit? Y / N ");
response=getche();
if(response=='y' || response== 'Y')
{
printf("\n\n\t\t\t Have a nice day!\n");
exit(0);
}
else if(response=='n' || response=='N')
{
break;
}
else
{
printf("\n\n\t\t\t\t ERROR");
break;
}
default: printf("\nInvaild entry, please enter again.\n");
break;
}
}
return 0;
}
Last edited by Scorpions4ever : April 27th, 2003 at 01:45 AM.
|

April 27th, 2003, 02:44 AM
|
|
Junior Member
|
|
Join Date: Apr 2003
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Hello Scorpions4ever,
Thank you for your reply. I had try your alternate choice but it give the same problem as before and for the removable of the main(); I still have the same problem. My problem is that when I enter a letter but not a number, the infinite loop occur. It should prompt an error message "Invaild entry, please enter again." Please advice me. Thank again.
|

April 27th, 2003, 10:27 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
yes, i remember having these problems when i was in CS a couple years ago. input validation seems to be very tricky. try someting like this maybe:
cin >> num;
while(!isdigit(num))
{
cin.flush() or is it cin.clear() ?? not sure...
cout << " bad input, enter again";
cin >> num;
}
if u find that loop running over the cin, try stickin in a cin.ignore()
|

April 28th, 2003, 01:09 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
My mistake. The reason you're looping is because the invalid characters are left behind in the input buffer. Since you loop again to read a number, the scanf function encounters the invalid characters again and leaves them unread and so on. You have to put some code to empty the input buffer, if invalid characters are entered. Luckily, you can do it by adding one extra line in the code  . In the default section, add a call to getchar() as follows:
Code:
default: printf("\nInvaild entry, please enter again.\n");
getchar();
break;
Hope this helps 
|
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
|
|
|
|
|