The
s at the end of this line makes it hard to remove the text on the line using only standard control characters:
cout << "sorry thats not a valid number ;w;
";
Also, the system("pause");
is non-standard and will likely also result in a newline (I'm not sure about that).
What you could do is to skip the printing of
and to just sleep a little before continuing.
Example:
#include <chrono> // misc. clocks
#include <iostream>
#include <string>
#include <thread> // std::this_thread::sleep_for
// a function to print some text, sleep for awhile and then remove the text
void print_and_clear(const std::string& txt, std::chrono::nanoseconds sleep) {
std::cout << txt << std::flush; // print the text
std::this_thread::sleep_for(sleep); // sleep for awhile
// Remove the text by returning to the beginning of the line and
// print a number of space characters equal to the length of the
// text and then return to the beginning of the line again.
std::cout << '
' << std::string(txt.size(), ' ') << '
' << std::flush;
}
int main() {
print_and_clear("sorry thats not a valid number...", std::chrono::seconds(1));
std::cout << "Hello
";
}
The above will print your text, sleep for a second, then remove the text and continue. What's left on the screen after the program has executed is only Hello
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…