0

I need to get input from the user but if an invalid argument is passed in then the "Invalid" messages stack up.
I'd prefer if it just kept printing the invalid on the same line and then once a correct input is given it resets the line to original question.
I've seen the talk of using \r but it does not seem to work for me.

bool getInput(std::variant<std::string, float>* toOut, bool* restart, bool* qUsed){

    std::string userIn;
    std::cin >> userIn;

    if(userIn == SENTINAL){
        *restart = true; 
        return false;
    }

    try {

        *toOut = std::stof(userIn);

    } catch(std::invalid_argument) {

        if(userIn == TO_SOLVE && !*qUsed){
            *toOut = TO_SOLVE;
            *qUsed = true;
        } else {
            std::cout << "\rInvalid, please try again : ";
            getInput(toOut, restart, qUsed);
        }

    }

    return true;

}

I haven't put the resetting to original question yet.
When I run this the \r does nothing.
Why is this / what's the best way to "update" cmd text?

Derek C.
  • 890
  • 8
  • 22
  • 1
    Once the user typed their input and pressed , there is no way for the cursor to return to the previous line. So each "invalid ..." will be in its own line. – Eugene Oct 07 '19 at 03:51
  • 1
    I don't think this is possible with the standard library, look into the console manipulation functions of your platform or ncurses – Alan Birtles Oct 07 '19 at 03:59
  • @Eugene ah I see, it's simply because of it being cin rather than out. No way to modify behaviour of temporarily? – Derek C. Oct 07 '19 at 05:05
  • No way within the standard library. – Eugene Oct 07 '19 at 11:09
  • You might want to look at this question and answer. The base functionality comes from .Net. https://stackoverflow.com/q/48302647/447901 – lit Oct 07 '19 at 14:10

0 Answers0