0

I hope you are going well.

I was wondering what could be the C++ equivalent without using sprintf of this statement?

sprintf(buff, "%-5s", list[idx]);

I am trying to follow a tutorial where the goal is to develop a menu with the Ncurses library in C. But I want to do it in C++.

This is the code given in the tutorial:

#include <ncurses.h>
 
int main() {
     
    WINDOW *w;
    char list[5][7] = { "One", "Two", "Three", "Four", "Five" };
    char item[7];
    int ch, i = 0, width = 7;
 
    initscr(); // initialize Ncurses
    w = newwin( 10, 12, 1, 1 ); // create a new window
    box( w, 0, 0 ); // sets default borders for the window
     
// now print all the menu items and highlight the first one
    for( i=0; i<5; i++ ) {
        if( i == 0 ) 
            wattron( w, A_STANDOUT ); // highlights the first item.
        else
            wattroff( w, A_STANDOUT );
        sprintf(item, "%-7s",  list[i]);
        mvwprintw( w, i+1, 2, "%s", item );
    }
 
    wrefresh( w ); // update the terminal screen
 
    i = 0;
    noecho(); // disable echoing of characters on the screen
    keypad( w, TRUE ); // enable keyboard input for the window.
    curs_set( 0 ); // hide the default screen cursor.
     
       // get the input
    while(( ch = wgetch(w)) != 'q'){ 
         
                // right pad with spaces to make the items appear with even width.
            sprintf(item, "%-7s",  list[i]); 
            mvwprintw( w, i+1, 2, "%s", item ); 
              // use a variable to increment or decrement the value based on the input.
            switch( ch ) {
                case KEY_UP:
                            i--;
                            i = ( i<0 ) ? 4 : i;
                            break;
                case KEY_DOWN:
                            i++;
                            i = ( i>4 ) ? 0 : i;
                            break;
            }
            // now highlight the next item in the list.
            wattron( w, A_STANDOUT );
             
            sprintf(item, "%-7s",  list[i]);
            mvwprintw( w, i+1, 2, "%s", item);
            wattroff( w, A_STANDOUT );
    }
 
    delwin( w );
    endwin();
}
L_J
  • 2,351
  • 10
  • 23
  • 28
  • What have you tried? What input gives different results between the original and your try? Show both results, the one you are getting and the one you should be getting (that `sprintf` gives) – Ben Voigt Apr 05 '21 at 16:47
  • `std::stringstream`? – Antonio Apr 05 '21 at 16:50
  • 2
    C++ is quite different from C: not only in the syntax but also in the way you should use it. It seems to me that you're trying to solve your problem in a C-like way on C++ (i. e. write C++ code similar to how you'd write it in C), while there is a real C++ solution, and we can't help you with that because you provide very little info about what you're doing ([the XY problem](https://en.wikipedia.org/wiki/XY_problem)?). Tell us what you're actually doing! Give us a piece of code! – Kolay.Ne Apr 05 '21 at 16:50
  • You can use "std::ostringstream". Once check this https://stackoverflow.com/questions/4983092/c-equivalent-of-sprintf I think this what you want to ask. – Abhishek Pratap Singh Apr 05 '21 at 17:04
  • 1
    `sprintf` is a legal `C++` function. You use `C` strings, so, why do you ask for equivalent? I mean, if the code were different... – user14063792468 Apr 05 '21 at 17:36

1 Answers1

5

%-5s says to print a string in a field of at least five characters and to print it left-justified, meaning that, if the string is shorter than the field, it is printed on the left and the right side is padded with spaces to fill the field.

In C++ I/O streams, you can do this with:

    std::cout.width(5);               // Set field width to five.
    std::cout.setf(std::ios::left);   // Request left-justification.
    std::cout << list[idx];           // Insert the string.

You can also do it with I/O manipulators by including <iomanip> and using:

std::cout << std::left << std::setw(5) << list[idx];
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312