0

These are the two programs I have:

Program 1 -

#include<stdio.h>
#include<stdlib.h>
#include<ncurses.h>

int display_ui(char *[]);

int main(int argc, char **agrv){
    int n, Max_W, Max_P, Input_MaxP;
    int * Profits;
    int * Weights;
    char *options[2] = {"Custom Input", "Random Input"}; //Menu List sent to 
    int choice = display_ui(options);

Program 2 (UI) -

#include<ncurses.h>

int display_ui_(char * options[]){
    initscr();

    int X, Y;
    getmaxyx(stdscr, Y, X);

    WINDOW * menuwin = newwin(6, X-X/2, Y-Y/2, 5);
    box(menuwin, 0, 0);
    refresh();
    wrefresh(menuwin);
    keypad(menuwin, true);

    int choice;
    int highlights = 0;

    while(true){
        for(int i = 0; i < 2; ++i){
            if(i == highlights){
                wattron(menuwin, A_REVERSE);
            }
            mvwprintw(menuwin, i+1, 1, options[i]);
            wattroff(menuwin, A_REVERSE);
        }
        choice = wgetch(menuwin);

        switch (choice)
        {
        case KEY_UP:
            highlights--;
            if(highlights == -1){
                highlights = 0;
            }
            break;
        case KEY_DOWN:
            highlights++;
            if(highlights == 2){
                highlights = 1;
            }
            break;
        default:
            break;
        }
        if(choice == 10){
            endwin();
            return highlights;
        }
    }
}

I want the my UI to return the the option selected based on the cursor position (highlights) back to my main function which is in another file but my program just goes into an infinite loop after selecting an option (hitting enter).

make
gcc -o knapsack Final_Knapsack.c ui_curses.c -lcurses && ./knapsack

Sai Nishwanth
  • 146
  • 1
  • 9
  • Is this C or C++? – Quimby Nov 15 '22 at 08:33
  • 1
    Both programs are in C – Sai Nishwanth Nov 15 '22 at 08:43
  • A couple of things: First of all please don't tag unrelated and different languages, only the language you're actually programming in. Then you use the term "programs" quite wrongly, as you try to *return a value from a **function***. – Some programmer dude Nov 15 '22 at 08:47
  • As for your problem, this seems like a very good time to learn how to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code line by line (while monitoring variables and their values) to see what really happens. – Some programmer dude Nov 15 '22 at 08:49
  • @Someprogrammerdude than you, I shall keep that in mind. I shall try it out with a deubbger too, ty! – Sai Nishwanth Nov 15 '22 at 12:50
  • 1
    This answers your question: [getch and putchar not working without return](https://stackoverflow.com/questions/10256477/getch-and-putchar-not-working-without-return) – Thomas Dickey Nov 15 '22 at 20:51
  • @ThomasDickey Than you, that fixed it! – Sai Nishwanth Nov 16 '22 at 03:49

0 Answers0