How do I make the message center and flash on the screen in C++?

Asked

Viewed 3,158 times

2

How to make this message flash on screen, stay centered, and how to change the message color?

    #include <iostream>
    #include <cstdlib>
    #include <windows.h>

    using namespace std;
    string mensagem;
    int main(int argc, char *argv[]) {


    cout << "Digite um palavara: " ;
    cin >> mensagem;

        cout << "***************************" "\n";
        cout << "***************************" "\n";
        cout << "***                     ***" "\n";
        cout << "***                     ***" "\n";
        cout<< "***""\t"<<mensagem<<"\t***""\n";
        cout << "***                     ***" "\n";
        cout << "***                     ***" "\n";
        cout << "***************************" "\n";
        cout << "***************************" "\n";


    system("PAUSE");
    return EXIT_SUCCESS;
}
  • If by any chance you’re referring to the attribute BLINK console text, this is legacy code feature that made use of lib conio.h of the former Borland, now Embarcadero: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devwin32/textattr_xml.html. This functionality comes from formerly widespread VGA modes.

1 answer

2


To center is easy. I’m seeing how to blink, which I’ve seen is not so simple:

#include <iostream>
#include <string>
using namespace std;

string Center(string str) {
   int espacos = (int)((21 - str.length())/2);
   return string(espacos, ' ') + str + string(espacos + (str.length() > espacos * 2 ? 1 : 0), ' ');
}

int main() {
    string mensagem;
    cout << "Digite um palavara: ";
    cin >> mensagem;

    cout << "***************************" "\n";
    cout << "***************************" "\n";
    cout << "***                     ***" "\n";
    cout << "***                     ***" "\n";
    cout<< "***" << Center(mensagem) << "***""\n";
    cout << "***                     ***" "\n";
    cout << "***                     ***" "\n";
    cout << "***************************" "\n";
    cout << "***************************" "\n";

    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference. Color doesn’t work on it.

From what I understand to blink you have to make one loop changing the color for a timer but there must be a better way.

  • 1

    That question has led me to past lives. The only context in which flashing on the console is trivial was at the time I learned C and C++ in Turbo C++ in Windows 95 when there was still real MS-DOS mode. http://en.wikipedia.org/wiki/VGA-compatible_text_mode

  • 1

    @pepper_chico is, at this time it was easier. Actually I think there should be a console mode on Windows easier to do this. And probably even the old way still works. But you’re right that having something blink like that doesn’t make much sense. Good times, everything was easier, now we look for problems to apply solutions that created :)

  • An interesting answer would be an example application in the context of VGA text mode, I think worth a search :)

  • There are schools that are still in this environment throughout Brazil, I do not doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.