0

I know a poor way of doing it:

QTextStream cin(stdin);
QString choice = cin.readLine();
if (choice == "yes" || choice == "y") {...

I'd like though to do it where when it shows up in cmdline:

Accept quest? (y/n) 
y

Where:

  1. y is displayed by default
  2. If you press n, it will not go "yn", but just change the character to "n"
  3. If you press any letter besides y or n, they won't register.

  4. And maybe as a bonus, if you press y, Yes! is displayed, and if you press n, No. is displayed.

Thanks!

Anon
  • 2,267
  • 3
  • 34
  • 51
  • 1
    I don't know if Qt does support this, aren't you looking for something like curses library? – Jack Nov 27 '15 at 01:56
  • I'm not familiar with curses. For the benefit of me and the general audience; Is the curses library specifically curtailed for something like this? Is it installed on ubuntu systems by default? – Anon Nov 27 '15 at 04:16

1 Answers1

1
QTextStream cin(stdin);
QTextStream cout(stdout);
while (true) {
    cout << "Accept quest? (y/n)" << endl;
    QString choice = cin.readLine();
    if (choice == "" || choice == "y") {
        cout << "Yes.\n";
        break;
    }
    else if (choice == "n") {
        cout << "No.\n";
        break;
    } else {
        cout << "Please make a valid choice. ('y' or 'n', or just hit enter." << endl;
    }
} 
  • Mmmmm a nice start.... there is no default choice though. If you can achieve number 2, 3 and 4, then i'll accept this as the answer. – Anon Nov 26 '15 at 22:58
  • 2) "If you press n, it will not go "yn", but just change the character to "n". - If you enter 'n', n is accepted and not appended to the last answer. 3) "If you press any letter besides y or n, they won't register. - Anything but y, n or the enter key registers. Everything else sends you back to the prompt. 4) "And maybe as a bonus, if you press y, Yes! is displayed, and if you press n, No. is displayed." - "Yes" is displayed for both yes and an empty send. "No" is displayed for 'n'. – Jay Bennett Nov 27 '15 at 01:38
  • Hey I guess I was not clear; ty 2) http://i.imgur.com/CiXB7tx.png It says yynn. I don't want that to be possible. I want it so if you press y, "Yes" is displayed, and waiting for you to press enter. 3) I mean registers as in the characters you type such as "r" won't show up in the command line. 4) If you press y, I want this to show up: "http://i.imgur.com/Cft5VAf.png". – Anon Nov 27 '15 at 03:49
  • 1
    This would require to setup the terminal (not possible with all terms) to ansi-mode plus read input on every character (not just on enter). See http://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal and http://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed – Sebastian Lange Nov 27 '15 at 11:02
  • Well then its probably not possible with Qt, unless perhaps – Anon Nov 28 '15 at 21:31