Thread: Menu driven program & filling an array - assistance needed

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    10

    Menu driven program & filling an array - assistance needed

    So I'm a bit stuck trying to figure out how to fill an array with a set of random number but not to print it out until the user requests to.

    I've figured out how to create the random numbers and print them out.

    And I'm using switch cases for my menu but not quite sure how to keep from the program ending and to allow still for the user to input until they decide to exit.

    Code:
    // This program will write a menu-driven program that allows the user to:// fill an array, query the array, and print the array.
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 50
    
    
    int main(){
        int range;
        int i;
        char choice;
        int array[SIZE];
    // Array elements will range from 1-999
        srand (time(NULL));
        range = rand() % (999 - 1) +1;
        
        
        printf("******MENU******\n");
        printf("F. Fill the array with a random number series.\n");
        printf("P. Print the array.\n");
        printf("Q. Query the array.\n");
        printf("Z. Terminate the program.\n");
        printf("\n");
        printf("Select one of the following options:\n");
        
        scanf("%c", &choice);
        
        switch(choice){
        
            case 'F' : case 'f':
                printf("Please wait while the array is being filled...\n");
                printf("Process is complete.\n");
                break;
            
            case 'P' : case 'p':
                for(i = 0; i < SIZE; i++){
                    printf("%i ", rand() %(999 - 1) +1);}
            break;
    
            case 'Q' : case 'q':
                printf("Please enter element you would like to query:\n");
            
            case 'Z' : case 'z':
            printf("Goodbye.");
            return(0);
            break;
            
            default:
                printf("Please select from the menu\n");
            }
    
    
    }
    For example, you enter F and then closes out versus letting you then enter p.
    But mostly the issue is filling the array without printing until prompted.

    In case 'P', is where I was able to create the array.

    Suggestions would be awesome! I'm still pretty new to C though

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Whenever you want any code to repeat (e.g. a menu and its execution), it needs to be in a loop -> For, While and Do While Loops in C - Cprogramming.com

    For menus, a "do-while" loop is a good choice since it executes at least once before checking for the terminating condition.

    Your "case F" is not filling an array.

    Your "case P" is not printing an array - it is just printing out randomly generated numbers.

    I'd suggest developing each of these separately, one at a time.

    First, make your P option just print out the values of the array. That it is. You might want to initialize your array to all zeroes so it prints practical values (especially if the user starts by entering 'P' before 'F'). "int array[SIZE] = {0}" will initialize all elements to zero.

    When that works, start working on your F option. There is no printing of values in that case, but instead generating random numbers and assigning them to your array.

  3. #3
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Put the switch statement into label a
    EDIT: For novice users, goto loop will be easier.

    But if you intend to be professional programmer, avoid using goto loops, they form a "spaghetti" code.
    You will be doing while() loop instead.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    EDIT: For novice users, goto loop will be easier.
    What do you mean by easier? Easier to learn bad practices.

    But if you intend to be professional programmer, avoid using goto loops,
    Avoid using goto loops even if you don't intend to be a professional programmer. They are considered a very bad practice and should be avoided by all novice programmers and most professional programmers. In very very knowledgeable hands they do have there use in extremely rare cases.


    Jim

  5. #5
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Not really. They are nicely used if you want to loop large code. Putting that in a while loop for example, causes scope confusions and loop-body addlement as well as loop-condition addlement.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Al3
    Not really. They are nicely used if you want to loop large code.
    No, they are not. If you have a large loop, then you should break it down into functions that do one thing and do it well, not use a goto to loop. Perhaps you are thinking of using goto to break from a deeply nested loop, which could be okay, but that could be replaced by appropriate use of flags in loop conditions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Not really. They are nicely used if you want to loop large code.
    Bull. Use one of the more accepted loop constructs, such as while(), do/while(), or for(). It is too easy to step out of a current scope into a different scope when using goto.

    Putting that in a while loop for example, causes scope confusions and loop-body addlement as well as loop-condition addlement.
    The proper use of functions and indentation should eliminate, or greatly reduce scope confusions. IMO if you have more than three levels of indentation you have too many and you need to see about reducing the complexity of that function.

    Jim

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    10
    @Matticus-

    Thats the part im confused about. In my original code, those "random" numbers you say are being printed out are what I want to be my array that case 'F' fills with.
    So to explain, what prints out from 'P' I want to be my array.


    I've added the do/while loop, but its not correct. If I press: f, p or q it gives me the menu still regardless.

    Code:
    // This program will write a menu-driven program that allows the user to:
    // fill an array, query the array, and print the array.
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 50
    
    
    int main(){
        int range;
        int i;
        char choice;
        char Z, z;
        int array[SIZE];
    // Array elements will range from 1-999
        srand (time(NULL));
        range = rand() % (999 - 1) +1;
        
        do{
        printf("******MENU******\n");
        printf("F. Fill the array with a random number series.\n");
        printf("P. Print the array.\n");
        printf("Q. Query the array.\n");
        printf("Z. Terminate the program.\n");
        printf("\n");
        printf("Select one of the following options:\n");
        
        scanf("%c", &choice);
        
        switch(choice){
        
            case 'F' : case 'f':
                printf("Please wait while the array is being filled...\n");
                printf("Process is complete.\n");
                break;
            
            case 'P' : case 'p':
                for(i = 0; i < SIZE; i++){
                    printf("%i ", rand() %(999 - 1) +1);}
            break;
            
            case 'Q' : case 'q':
                printf("Please enter element you would like to query:\n");
            
            case 'Z' : case 'z':
                printf("Goodbye\n");
                return(0);
            default:
                printf("Please select from the menu\n");}
        }
        while(choice != Z || z);
        return (0);
    
    
    }

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you want to fill the array in 'F' you need a loop, very similar to what you now have in 'P'. The difference is that you will store the random numbers being generated to an element of the array.

    In 'P' you need to print each element of your array, not generate random numbers. Do you know how to access individual members of your array?

    Jim

  10. #10
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Seriously guys, Do I have to bold the text FOR NOVICE USERS. Of course.. breaking it up to functions, structs and more elements will be better. But it wasn't about readability or professionality (if thats even a word) It was about working code on an easy way. Programmer's ego though.. He is free to decide which loop to use. Thats his own choice, not ours. I am not going to argue about the use of goto loop. If you took C or C++ course for that matter you would already know. But again.. this is off-topic.
    Last edited by Al3; 11-17-2014 at 01:45 PM.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Seriously guys, Do I have to bold the text FOR NOVICE USERS.
    Why should you do that, novice users should never Never NEVER use goto, period. They need to learn the proper way to program not the easy way.

    Jim

  12. #12
    Registered User
    Join Date
    Oct 2014
    Posts
    10
    Quote Originally Posted by jimblumberg View Post
    If you want to fill the array in 'F' you need a loop, very similar to what you now have in 'P'. The difference is that you will store the random numbers being generated to an element of the array.

    In 'P' you need to print each element of your array, not generate random numbers. Do you know how to access individual members of your array?

    Jim
    No I dont know how to access elements of an array. Let alone assign random numbers to each element within the array.

  13. #13
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by jimblumberg View Post
    Why should you do that, novice users should never Never NEVER use goto, period. They need to learn the proper way to program not the easy way.

    Jim
    Well then, thankfully this is not about learning him the most common, known and good way. As it is clear, that way has already been spoken. I was one, to bring another idea for him to chooce from. An idea that has its benefits. Thanks.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    No I dont know how to access elements of an array. Let alone assign random numbers to each element within the array.
    Then you may find this link useful.


    I was one, to bring another idea for him to chooce from.
    A very bad idea at that. Pushing the idea of using goto is like throwing someone into a well to get some water instead of teaching them to use the bucket, or pump.

    Jim

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Chris Panton
    No I dont know how to access elements of an array.
    What material are you using to learn C? It should be teaching you about this at some point, so you should read up on it. It will probably tell you something along the lines of using array[0] to access the first element of the array named array.

    Quote Originally Posted by Al3
    Seriously guys, Do I have to bold the text FOR NOVICE USERS.
    No, if you really had to mention goto here, then you should have bolded the text ONLY FOR EXPERTS WHO KNOW WHAT THEY ARE DOING. Novices should not be taught the use of goto until they understand and are routinely able to use more specific control structures.

    Quote Originally Posted by Al3
    Well then, thankfully this is not about learning him the most common, known and good way. As it is clear, that way has already been spoken. I was one, to bring another idea for him to chooce from. An idea that has its benefits. Thanks.
    Sigh. No, please do not teach novices bad practice. If you provide an option and label it "FOR NOVICE USERS", then you're suggesting that it be used for novice users, but this is precisely what they should not use. No, thanks.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu Driven Array of Function Pointers Calculator, Help Needed
    By SarahDaniella in forum C Programming
    Replies: 4
    Last Post: 05-13-2013, 09:18 PM
  2. Menu driven program
    By kenadams in forum C Programming
    Replies: 9
    Last Post: 10-27-2011, 01:38 AM
  3. Need assistance with Menu Driven Programming Assessment!
    By nicbono in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2011, 10:48 PM
  4. menu driven program help
    By vallamrao in forum C Programming
    Replies: 2
    Last Post: 09-10-2010, 04:56 AM