Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Wait for user input but print the next line

Tags:

c++

So I am making a GUI with ascci, I would like to wait for user input but print the last line of ascci border. As of now it will wait for user input and then print the last ascci border line. Is there anyway to fix this?

Example of what I want:

Login Screen

======================================================

        Welcome to Bank Beta 0.1
        ------------------------

        (1)Login
        (2)Create Account


        USER INPUT HERE

======================================================

Example of what I am getting:

======================================================

        Welcome to Bank Beta 0.1
        ------------------------

        (1)Login
        (2)Create Account


        USER INPUT HERE

Here's my code:

void login ()
{
    cout << "======================================================" << endl
        << "\t\tWelcome to Bank Beta 0.1" << endl
        << "\t\t------------------------" << endl << endl
        << "\t\t (1)Login" << endl
        << "\t\t (2)Create Account" << endl << endl;
}
int main()
{ 
    int loginChoice;
    login();
    cin >> loginChoice;
    cout << "======================================================" << endl;
    _getch();
}
like image 763
Max Mcgregor Avatar asked Aug 25 '15 07:08

Max Mcgregor


1 Answers

Since you are working on a console based GUI, i suggest using some sort of cursor movement functionality. This will save you a lot of time in the alignment of "objects" too.

Here is the code you will need to move the cursor for both win and unix

#ifdef _WIN32

    #include <windows.h>

    void gotoxy(int x, int y)
    {
        COORD p = { x, y };
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
    }

    #else

    #include <unistd.h>
    #include <term.h>

    void gotoxy(int x, int y)
    {
        int err;
        if (!cur_term)
        if (setupterm(NULL, STDOUT_FILENO, &err) == ERR)
            return;
        putp(tparm(tigetstr("cup"), y, x, 0, 0, 0, 0, 0, 0, 0));
    }

#endif 

You can remove either of them if you don't need platform independence, but having them both does no harm. now the interesting part:

void login ()
{
    cout << "======================================================" << "\n"
    << "\t\tWelcome to Bank Beta 0.1" << "\n"
    << "\t\t------------------------" << "\n\n"
    << "\t\t (1)Login" << "\n"
    << "\t\t (2)Create Account" << "\n\n";
    gotoxy(0, 7);
    cout << "======================================================" << "\n";
    gotoxy(0, 6);
    cout << "\t\t";
}
int main()
{ 
    int loginChoice;
    login();
    gotoxy(0,8);
    cin >> loginChoice;
    _getch();
}

The writing and reading are now independent of each other and you can also mess around with all the positions a lot more easyer.

like image 72
Alex Rusu Avatar answered Oct 15 '22 15:10

Alex Rusu