Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print out all the strings within an array within an ncurses window?

I’m trying to display an array of multiple random generated strings within a ncurses window. At first, I thought this was going to be simple and I can print the array as I would with any other array: putting it through a loop and displaying each string that it comes across within the array. When I tried this, all the words were printing, just that they were coming out of the borders of the ncurses window. Then I tried to figure out a way I can set the location of the cursor for each word, but I can’t figure out how I would do that. Does anybody know a solution I can keep the strings within the borders of the window? Any help would be appreciated.

initscr();

string wordList[19] = {"icecream", "computer", "dictionary", "algorithm", "the", "be", "to", "of", "and", "a", "your", "you", "year", "would", "work", "with", "will", "who", "which",};
string word[19];

// parameters for window
int height, width, start_y, start_x;    
height = 9;
width = 75;
start_y = 10;
start_x = 10;

//creating the window
WINDOW *win = newwin(height, width, start_y, start_x);
refresh();
box(win, 0, 0);
wrefresh(win);


// displaying random strings from the wordList array.
srand(time(0));
for(int i=0; i<19 ; i++){
    word[i] = wordList[rand() % 19];
    printf("%s", word[i].c_str());
    printf(" ");
}
wrefresh(win);
getch();
endwin();

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Don’t use printf, use ncurses’ functions like waddstr() instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading