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