0

I'm trying to create a simple login page in c++. Currently, in my program you have to enter the username before the password input becomes available. I want to be able to show both and have the option to input one before the other.

cout << "User:  "; cin >> enteruser;
cout << "Pass:  "; cin >> enterpass;

return 0;

I want it to be similar to a web login page, but in a c++ console application window.

  • It seems like you're looking for the kinds of console manipulation provided by the _ncurses_ library. More specific advice will depend on what operating system you are targeting. – paddy Nov 11 '19 at 02:23
  • Possible duplicate https://stackoverflow.com/questions/9709605/libraries-for-displaying-a-text-mode-menu – 4xy Nov 11 '19 at 02:27

2 Answers2

0

Here's a basic implementation without using any library and just using escape sequences defined at http://ascii-table.com/ansi-escape-sequences.php to set the cursor position.

#include <iostream>
#include <string>
#include <stdio.h>

void cursor_up(int lines)
{
    /* Enter escape sequence */
    printf("%c%c%dA", 0x1b, 0x5b, lines);
}

void cursor_forward(int cols)
{
    /* Enter escape sequence */
    printf("%c%c%dC", 0x1b, 0x5b, cols);
}

int main(int argc, char *argv[])
{
    std::string username;
    std::string passwd;

    std::string user_prompt = "Enter user: "; 
    std::string passwd_prompt = "Enter password: ";

    std::cout << user_prompt << std::endl;
    std::cout << passwd_prompt << std::endl;

    cursor_up(2);
    cursor_forward(user_prompt.length());
    std::cin >> username;

    cursor_forward(passwd_prompt.length());
    std::cin >> passwd;

    return 0;
}
Yasir Khan
  • 645
  • 4
  • 9
0

The ncurses way is something like:

#include <ncurses.h>

char userFieldText = "User: ";
char passFieldText = "Pass: ";
char inputUserName[100];
char inputPass[100];
int rowCount;
int columnCount;

initscr();
getmaxyx(stdscr,rowCount,columnCount);
mvprintw(1,1,"%s",userFieldText);
mvprintw(2,1,"%s",passFieldText);
getstr(inputUserName);          // Get input until enter
getstr(inputPass);          // Get input until enter
endwin();

Paraphrased from ncurses docs, see this link if you want further descriptions on their library functions. You get some convenience functions that help you traverse the console with something like ncurses. Not as creative as plain C++, but depends if you want more features out of a library like it down the road.

user176692
  • 780
  • 1
  • 6
  • 21