Like Tree1Likes
  • 1 Post By stdq

My first c program - HELP!!

This is a discussion on My first c program - HELP!! within the C Programming forums, part of the General Programming Boards category; HELP! This is my first c program so i apologize if the technique is poor. My basic problem is that ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Post My first c program - HELP!!

    HELP! This is my first c program so i apologize if the technique is poor. My basic problem is that when the user inputs an invalid integer for the menu which is then processed by the function menuProcessor, the program goes into an infinite loop of printing the menu to the screen and clearing the screen, never again asking for a new menuItem input. Why is this happening?! I simply want menuProcessor to call menu again until a valid menuItem input is entered. Thanks!

    Code:
    #include <stdio.h>
    
    void menu();
    void menuProcessor(int);
    
    int main () {
        int numbers[20] = {33, 77, 99, 2, 4, 1, 7, 12, 65, 3, 78, 5, 8, 10, 28, 27, 67};
        menu();
        //getch();
        return 0;
    }
    
    void menu () {
        int menuItem;
        system("cls");
        printf("Please select from the following options:\n");
        printf("1. Print the content of the array\n");
        printf("2. Compute the average of the array\n");
        printf("3. Compute the sum of the array\n");
        printf("4. Print the content of the array in reverse order\n");
        printf("5. Add a value to each element of the array\n");
        printf("6. Exit\n");
        scanf("%d", &menuItem);
        menuProcessor(menuItem);
        return;
    }
    
    void menuProcessor (int menuItem) {
        if ((menuItem < 1) || (menuItem > 6)) menu();
        return;
    }

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    114
    Hi, I tried your program in my computer and it seems that when the program enters into an infinite loop, it doesn't keep on printing the menu over and over again. You could use, for example, a switch case with cases from 1 to 5 to call the function menuProcessor(), and a default to display a message like "Invalid option." The option 6 can be handled by putting the whole switch case inside a while loop with the condition

    Code:
    while ( menuItem != 6 )
    so that when the user inputs 6, the while loop and the menu are exited.
    shruthi likes this.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,572
    I re-wrote you code logic using a do/while loop and changed the two function prototypes to what is normally done. You way was doing Indirect recursion which is when function "A" calls function "B" with function "B" calling function "A". Indirect recursion is not really a good thing for a beginner to do. Some people consider it a bad thing for experts to do.

    Note: If this violates the HW rules; I do NOT care.
    I got little support on my CB rules post(Linux users getting dumber). So, it looks to me that following the forum rules does not matter on this site.

    Tim S.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int menu();
    void menuProcessor();
    
    int main () {
        int numbers[20] = {33, 77, 99, 2, 4, 1, 7, 12, 65, 3, 78, 5, 8, 10, 28, 27, 67};
        menuProcessor ();
        //getch();
        return 0;
    }
    
    int menu () {
        int menuItem;
        // system("cls");
        printf("Please select from the following options:\n");
        printf("1. Print the content of the array\n");
        printf("2. Compute the average of the array\n");
        printf("3. Compute the sum of the array\n");
        printf("4. Print the content of the array in reverse order\n");
        printf("5. Add a value to each element of the array\n");
        printf("6. Exit\n");
        scanf("%d", &menuItem);
        return menuItem;
    }
    
    void menuProcessor () {
        int menuItem = 0;
        do {
            menuItem = menu();
            printf("menuItem: %d\n", menuItem);
        } while ((menuItem < 1) || (menuItem > 6));
        return;
    }
    Last edited by stahta01; 01-28-2012 at 01:05 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Quote Originally Posted by stahta01 View Post
    I re-wrote you code logic using a do/while loop and changed the two function prototypes to what is normally done. You way was doing Indirect recursion which is when function "A" calls function "B" with function "B" calling function "A". Indirect recursion is not really a good thing for a beginner to do. Some people consider it a bad thing for experts to do.

    Note: If this violates the HW rules; I do NOT care.
    I got little support on my CB rules post(Linux users getting dumber). So, it looks to me that following the forum rules does not matter on this site.

    Tim S.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int menu();
    void menuProcessor();
    
    int main () {
        int numbers[20] = {33, 77, 99, 2, 4, 1, 7, 12, 65, 3, 78, 5, 8, 10, 28, 27, 67};
        menuProcessor ();
        //getch();
        return 0;
    }
    
    int menu () {
        int menuItem;
        // system("cls");
        printf("Please select from the following options:\n");
        printf("1. Print the content of the array\n");
        printf("2. Compute the average of the array\n");
        printf("3. Compute the sum of the array\n");
        printf("4. Print the content of the array in reverse order\n");
        printf("5. Add a value to each element of the array\n");
        printf("6. Exit\n");
        scanf("%d", &menuItem);
        return menuItem;
    }
    
    void menuProcessor () {
        int menuItem = 0;
        do {
            menuItem = menu();
            printf("menuItem: %d\n", menuItem);
        } while ((menuItem < 1) || (menuItem > 6));
        return;
    }
    when i compile and run this code and intentionally give a bad menuItem, it seems to go into an infinite printing loop and never prompts the user for a new menuItem? basically the same problem as what i was doing before i think.. is there a problem with my compiler?

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    114
    I tried to come up with another version too:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int menu(void);
    void menuProcessor(void);
    
    int main(void)
    {
        /* Initializes option to a value other than 6 to enter the loop: */
        int option = 1;
    
        while ( option != 6 )
        {
            option = menu();
            switch ( option )
            {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    menuProcessor();
                    break;
    
                /* Do nothing else before trying (and not succeeding) to
                enter the while loop: */
                case 6:
                    break;
    
                default:
                    printf("Invalid option.\n\n");
            }
        }
    
        return 0;
    }
    
    
    int menu(void)
    {
        int menuItem;
    
        printf("Please select from the following options:\n");
        printf("1. Print the content of the array\n");
        printf("2. Compute the average of the array\n");
        printf("3. Compute the sum of the array\n");
        printf("4. Print the content of the array in reverse order\n");
        printf("5. Add a value to each element of the array\n");
        printf("6. Exit\n");
        scanf("%d", &menuItem);
    
        return menuItem;
    }
    
    
    void menuProcessor(void)
    {
        printf("Menu processed.\n\n");
    }
    It works if the wrong option entered is an integer, but if it receives another character or a floating-point number, it loops infinitely - maybe you would need exception handling to deal with these kinds of input, although I'm not sure about that.
    Last edited by stdq; 01-28-2012 at 03:16 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    163
    Try putting this line right after the scanf line.

    Code:
    while( !feof(stdin) && getchar()!='\n' );

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Quote Originally Posted by A34Chris View Post
    Try putting this line right after the scanf line.

    Code:
    while( !feof(stdin) && getchar()!='\n' );
    that did the trick!! thanks! just so i can understand what's goin on there, what does that line do exactly? what is feof(stdin) and getchar? thanks!

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    163
    Quote Originally Posted by dblack87 View Post
    that did the trick!! thanks! just so i can understand what's goin on there, what does that line do exactly? what is feof(stdin) and getchar? thanks!
    In a nutshell it clears out the keyboard buffer. Scanf seems to clutter up the keyboard buffer for some reason sometimes. This line has saved me when trying to put getchar()'s here and there did not help.

    FEOF I think is EndOfFile something or other and GETCHAR() gets a keyboard character input from stdin(keyboard).

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by A34Chris View Post
    Try putting this line right after the scanf line.

    Code:
    while( !feof(stdin) && getchar()!='\n' );
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    163
    Well at least we figured out what the problem was

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-03-2010, 12:45 PM
  2. Replies: 1
    Last Post: 03-03-2009, 03:47 PM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 12:11 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21