Thread: Menu function

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    52

    Menu function

    This is supposed to emulate the "Menu" function on the TI-8* calculators (in TIBASIC). It compiles fine, but I can't quite figure out what's wrong with it. It's using ncurses, btw.....
    I can't see what's wrong with it, so any help would be greatly appreciated.

    Sorry it's not commented.

    Code:
    #include <ncurses.h>
    #include <stdarg.h>
    
    int wannabemenu(char* title, char* opt, ...) {
      clear();
      va_list argslist;
      int xpos, ypos;
      char* currentarg;
      int ch;
      int choices = 1;
      int currchoice = 1;
      move(0,0);
      printw("%s\n", title);
      
      va_start(argslist, opt);
      for (currentarg = va_arg(argslist, char*); currentarg != NULL; choices ++) {
        printw("%d. %s\n", choices, currentarg);
      }
      va_end(argslist);
      mvchgat(currchoice, 0, 2, A_BLINK, 0, NULL);
      while ((ch = getch()) != KEY_ENTER) {
        switch (ch) {
          case KEY_UP:
            mvchgat(currchoice, 0, 2, A_BLINK, 0, NULL);
            currchoice += 1;
            break;
          case KEY_DOWN:
            mvchgat(currchoice, 0, 2, A_BLINK, 0, NULL);
            currchoice -= 1;
            break;
          default:
            break;
        }
        if (currchoice > choices) {
          currchoice -= 1;
        }
        if (currchoice < 1) {
          currchoice = 1;
        }
        refresh();
      }
      return currchoice;
    }
    
    int main() {
      int chosen;
      
      initscr();
      raw();
      keypad(stdscr, TRUE);
      noecho();
      chosen = wannabemenu("PICK ONE!", "ASDL:KFJS:LDKJF", "MOOCOW", "ZOOKS?");
      printw("You chose #%d.", chosen);
      refresh();
      getch();
      
    }

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    What is it doing differently than it should? How do you know there's a problem?

    You say it compiles fine, but you can't figure out what's wrong. You've got to give more info if we're to have any chance of helping you.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    ok.

    it displays a blank ncurses screen, and doesn't respond to any input.
    it's SUPPOSED to display a arrow-key navigable text menu, and when you hit enter it prints something.

    and i compiled with gcc -lncurses -o nmenu nmenu.c

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Okie dokes...

    At first glance, it looks pretty solid, but here's some things I noticed which may not solve your problem, but either way, they're things I noticed.

    One thing I noticed is that you're not calling...
    Code:
    endwin();
    ...at the end to close "NCurses mode". Probably best to toss that in once you're done with NCurses.

    Also main()'s defined to return an int, so you should make sure to throw in a "return 0;" at the end of your function, to show everything happened smoothly.

    For the use of va_args(), MSDN states you must include "stdarg.h" AND "stdio.h"

    Ah, I think I found it...you're stuck in an infinite loop:
    Code:
      for (currentarg = va_arg(argslist, char*); currentarg != NULL; choices ++) {
        printw("%d. %s\n", choices, currentarg);
      }
    Your for loop initializes currentarg with the first element, if it's not NULL, it will keep running the for loop, as long as currentarg != NULL. But you never do another call to va_arg(). You just increment choices, call printw(), with no refresh(), so nothing's displayed. And you're stuck in an infinite loop, because currentarg isn't changing.

    Instead, try:
    Code:
    for (currentarg = va_arg(argslist, char*); currentarg != NULL; choices++)
    {
    
    printw("%d. %s\n", choices, currentarg);
    
    currentarg = va_arg(argslist, char*);
    
    }
    However, va_arg() uses the size of your type (char*) to increment to the next item, so I'm not sure if your output will be as expected with variable length strings.

    Other than that, it looks pretty solid...
    Last edited by Epo; 01-07-2006 at 07:22 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    Thanks.
    forgot endwin and return, i'll put those in.....

    stdio.h is included with ncurses.h.

    UPDATE: i recompiled and got a segmentation fault. uhoh.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    i can't figure out why it's getting a segmentation fault, anybody want to help? everything else above still applies.

    the seg fault appears as soon as it's run.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > chosen = wannabemenu("PICK ONE!", "ASDL:KFJS:LDKJF", "MOOCOW", "ZOOKS?");
    How does your code tell the end of the list?
    By looking for a NULL pointer.

    So maybe
    chosen = wannabemenu("PICK ONE!", "ASDL:KFJS:LDKJF", "MOOCOW", "ZOOKS?", (char*)NULL );

    Oh, and most of your code is in GNU/C99 style rather than the more portable C89 style

    > and i compiled with gcc -lncurses -o nmenu nmenu.c
    Add -W -Wall -ansi
    to the flags as well.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    salem, what's the difference between C99 / C89? I thought C99 was better, and ive always written like this because that's how i learned it, i didn't realize there were other ways to write it.... also, what does -W do (it's the same as wall, right? which does.... what?). and i'm guessing ansi just compiles it according to the ANSI standards.

    ANOTHER UPDATE:

    recompiled, new source is below.

    it almost works -- the second parameter to the menu function isn't displayed ("A:LKJF:SLKDJF:LK" or something like that in my test)........ and the arrow keys don't quite work. sometimes they move it the wrong direction, and sometimes they don't do anything. also, enter doesn't end it.

    Code:
    #include <ncurses.h>
    #include <stdarg.h>
    
    int wannabemenu(char* title, char* opt, ...) {
      clear();
      va_list argslist;
      char* currentarg;
      int ch;
      int choices = 1;
      int currchoice = 1;
      move(0,0);
      printw("%s\n", title);
      
      va_start(argslist, opt);
      for (currentarg = va_arg(argslist, char*); currentarg != NULL; choices ++) {
        printw("%d. %s\n", choices, currentarg);
        currentarg = va_arg(argslist, char*);
      }
      va_end(argslist);
      refresh();
      mvchgat(currchoice, 0, 2, A_BLINK, 0, NULL);
      while ((ch = getch()) != KEY_ENTER) {
        switch (ch) {
          case KEY_UP:
            mvchgat(currchoice, 0, 2, A_BLINK, 0, NULL);
            currchoice += 1;
            break;
          case KEY_DOWN:
            mvchgat(currchoice, 0, 2, A_BLINK, 0, NULL);
            currchoice -= 1;
            break;
          default:
            break;
        }
        if (currchoice > choices) {
          currchoice -= 1;
        }
        if (currchoice < 1) {
          currchoice = 1;
        }
        refresh();
      }
      return currchoice;
    }
    
    int main() {
      int chosen;
      
      initscr();
      raw();
      keypad(stdscr, TRUE);
      noecho();
      chosen = wannabemenu("PICK ONE!", "ASDL:KFJS:LDKJF", "MOOCOW", "ZOOKS?", (char*)NULL);
      printw("You chose #%d.", chosen);
      refresh();
      getch();
      endwin();
      return 0;  
    }
    Last edited by w00tw00tkab00t; 01-08-2006 at 09:03 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > what's the difference between C99 / C89? I thought C99 was better
    Well nearly everyone can get hold of a C89 compiler for their machine, but fully functional C99 compilers are still pretty rare.
    Of course, the choice is yours as to which you go for, I'm just pointing out a few things in the hope people won't come back with "why doesn't this work with my foo compiler".

    > also, what does -W do
    It reads the manual page for you.

    > the second parameter to the menu function isn't displayed
    How about adding a
    printw("%s\n", opt);

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    umm... where can i learn more about the differences between the two?

    and I finally got it to work. i'm making a header file to emulate the most useful functions on the TI-8X calculators, so i threw it in as timenu and mvtimenu. w00t time to go make text based adventures!

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    http://www.ucalgary.ca/~bgwong/n869.pdf
    This is the last public draft of the C99 standard, which is free.
    You can get the actual standard for some $$$ at http://webstore.ansi.org

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    sweet thanks.
    what about the C89 standard?

    and what's the book "The ANSI C Programming Language: Second Edition" -- C89 or C99? (you know, the kernigan + ritchie book)

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    K&R is C89

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    ah, thanks.

    nothing left to say on this thread, i got everything to work :-D
    took me a while though....

    and NOW i have another thread, for my next horribly lame program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM