C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old April 26th, 2003, 11:58 PM
EDIT EDIT is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 4 EDIT User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 1rintf("How are you\n");break;
case 2rintf("How are you\n");break;
case 3rintf("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;
}
defaultrintf("\nInvaild entry, please enter again.\n");
main();break;
}
}

Reply With Quote
  #2  
Old April 27th, 2003, 12:24 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
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.

Reply With Quote
  #3  
Old April 27th, 2003, 02:44 AM
EDIT EDIT is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 4 EDIT User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old April 27th, 2003, 10:27 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
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()

Reply With Quote
  #5  
Old April 28th, 2003, 01:09 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Infinite loop.


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway