-1

So I am writing a program, and need to use some formatting to pretty up the output. Right now, the way I write it is like this (as an example):

    cout << "|" << setfill('=') << setw(40) << "|" << endl;
    cout << "| What is your name?: ";
    cin >> userName;
    cout << setfill(' ') << setw(40 - 21 - userName.size()) << "|" << endl;
    cout << "|" << setfill('=') << setw(40) << "|" << endl;

What this should be outputting is this (the user's input is in all-caps):

|========================================|
| What is your name?: ALARIC             |
|========================================|

but what I get is this:

|========================================|
| What is your name?: ALARIC
             |
|========================================|

is there a way to solve this, and/or what am I doing wrong?

  • 3
    You're going to want a library like ncurses if you want to do things like this. – NathanOliver May 04 '22 at 20:56
  • 1
    ***is there a way to solve this*** Not really when the user has to input using the standard library and press enter – drescherjm May 04 '22 at 20:56
  • `cin << userName;` isn't going to work. You probably meant `cin >> userName;`. – Fred Larson May 04 '22 at 20:57
  • yes I did. Let me fix that. – Alaric Malikov May 04 '22 at 20:58
  • My advice is not to put the `|` on the lines with input. If you need to do that then you will have to use a library like the first comment or OS API / terminal functionality – drescherjm May 04 '22 at 20:58
  • When the user answers the prompt and inputs their name, they have to hit the [Enter] key. That gets echoed on the screen. Now what? – Fred Larson May 04 '22 at 21:04
  • is there a way I can prevent the program from echoing the \n when the user presses enter? – Alaric Malikov May 04 '22 at 21:08
  • Not in standard c++ alone. Your OS or terminal could allow you to do that. Related: [https://stackoverflow.com/questions/10058050/how-to-go-up-a-line-in-console-programs-c](https://stackoverflow.com/questions/10058050/how-to-go-up-a-line-in-console-programs-c) – drescherjm May 04 '22 at 21:41
  • You are probably going to need to use a different input stream than `cin`. You'll need to input the data, *without echo*, then covert to upper case and output. The standard input defaults to echoing the text that was entered. – Thomas Matthews May 04 '22 at 21:54

1 Answers1

1

This string "\033[F" moves the cursor up one line. What I did is move the cursor up one line after receiving the username and then reprinting the second line with the '|' symbol at the end.

This code only works if the username is not too long.

#include <iostream>

int main()
{
    std::string username;
    std::string second_line = "| What is your name?: ";

    std::cout << "|========================================|" << std::endl;
    std::cout << second_line;
    std::cin >> username;
    int line_size = second_line.size() + username.size();
    /* assumes line_size < 41 */
    std::cout << "\033[F" << second_line << username << std::string(41 - line_size, ' ') << "|" << std::endl;
    std::cout << "|========================================|" << std::endl;

    return 0;
}

Here are two examples of the output:

|========================================|
| What is your name?: username           |
|========================================|

|========================================|
| What is your name?: Alaric             |
|========================================|

I tried this on my Linux machine and with an online compiler. Both worked, but I haven't tried it on a Windows machine.

LuisUrzua
  • 123
  • 1
  • 3