Menu type system flow-control

This is a discussion on Menu type system flow-control within the C Programming forums, part of the General Programming Boards category; Hello: I am building a console application that entails a menu. The user may select a number on the menu ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Question Menu type system flow-control

    Hello:
    I am building a console application that entails a menu.
    The user may select a number on the menu and a certain action will be performed. I am using a switch statement to loop through the menu options; however; if I want the program to return to the main menu everytime it finishes the task the user selects; how do I do that?
    Do I use goto? I thought that was a bad programming habit to use it?

    Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The best way to do this is to use a loop of some sort. For example:
    Code:
    int main() {
        int n;
    
        do {
            printf("Type 1 for this, 2 for that, -1 to quit: ");
            scanf("%d", &n);
            switch(n) {
                /* ... */
            }
        } while(n != -1);
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    I see. So I assume the return 0; means it will start the loop all over again?
    I also have another question related to this program...
    This is an assignment for a course I am taking and the restrictions are that I have to make each task in the menu a function of its own. There are tasks such as "print all multiples of a number" or "change the value of a variable". However; I am not supposed to use any global variables. Does this mean I have to use pointers? And for example; if there was a function to print the first 10 multiples of an integer; should I make the function calculate all the multiples at once and store them in an array? Or should I make the function to simply give the next multiple? Which way would be more efficient?

    Thanks!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by drag0n69 View Post
    I see. So I assume the return 0; means it will start the loop all over again?
    Not exactly. The return 0 exits from main() and ends the program. The loop goes from "do" to "while". Perhaps you should read this tutorial. http://www.cprogramming.com/tutorial/c/lesson3.html

    A do-while or while loop, in essence, continues executing its contents over and over again while some condition is true. For example:
    Code:
    int x;
    printf("Enter a number, but please, not zero: ");
    scanf("%d", &x);
    while(x == 0) {
        printf("I said, \"not zero\"! Try again: ");
        scanf("%d", &x);
    }
    I also have another question related to this program...
    This is an assignment for a course I am taking and the restrictions are that I have to make each task in the menu a function of its own. There are tasks such as "print all multiples of a number" or "change the value of a variable". However; I am not supposed to use any global variables. Does this mean I have to use pointers?
    No; not necessarily. Have you learned about functions? They have a way to pass information in, via parameters, and a way to pass information out, via return values. The only trouble is, you can only pass out one return value. So if you need to return more than one variable, you probably need to use pointers. (You can use other hacks, like returning a structure, but never mind that.) Another reason to use pointers is that passing information in and out means making possibly unnecessary copies of that information. If the variables and types involved are quite large, it can be more efficient to pass pointers.

    (Remember the "return 0" in main? That was main passing information to whatever executed your program -- probably Windows -- telling it that your program executed successfully. Returning 1 usually indicates that your program encountered an error.)

    But until you get to structures -- there's no need to use pointers. Passing simple variables "by value" in and returning them back again is quite efficient.

    So -- you need to read up on parameters, return values, and functions in general. Your textbook undoubtably has some information on this topic, and there are many tutorials of this sort on the web. Here's one of them. http://www.cprogramming.com/tutorial/c/lesson4.html

    And for example; if there was a function to print the first 10 multiples of an integer; should I make the function calculate all the multiples at once and store them in an array? Or should I make the function to simply give the next multiple? Which way would be more efficient?

    Thanks!
    There's no need to use an array if you can just print the information after you've discovered it. It's easier that way.
    Last edited by dwks; 02-23-2008 at 04:09 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Thanks for the long explanation! I really appreciate your help.
    I have a few questions concerning what you said about calling functions; taking the function return variable and recalling the function again with that variable.
    Do you mean like for example:
    I want to find the multiples of 3 between 3 and 12.
    I make a function to multiply each parameter number by 3 and return a variable and then use that output number to get the next multiple and etc... through a loop in the main of course...
    Sorry if what I am saying is confusing... lol

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Sure, something like that would work. e.g.
    Code:
    int timesthree(int x) {
        return x * 3;
    }
    
    x = 3;
    while(x <= 12) {
        printf("&#37;i\n", x);
        x = timesthree(x);
    }
    However, I think you are perhaps over-complicating things. I quote:
    There are tasks such as "print all multiples of a number" or "change the value of a variable".
    If all you want to do is print all multiples of a number, you could use a function like
    Code:
    void print_multiples(int number, int min, int max);
    and do everything in there. (Set x to min, increment by number until x is max, print the number each time.) Changing the value of a variable between different functions isn't necessary in this case. (Passing variables is -- unless you want the function to always print multiples of 3 between 3 and 12 -- but return values isn't really.)

    But of course, when you get around to implementing the "change the value of a variable", you probably will be using return values and stuff.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  2. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

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