Thread: Infinite loop.

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    4
    Rep 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:printf("How are you\n");break;
    case 2:printf("How are you\n");break;
    case 3:printf("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:printf("\nInvaild entry, please enter again.\n");
    main();break;
    }
    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    4
    Rep 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.
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    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()
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    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 :)

IMN logo majestic logo threadwatch logo seochat tools logo