windows - How to add char directly after (cin) in c++ -
i trying add percent sign directly after users input (so user doesn't have type percent symbol). when try this, either goes next line or doesn't work @ all.
what want: _%
// blank user's input.
sorry if messy, i'm not sure how add c++ here.
here things have attempted:
// used percent variable: const char percent = '%'; cout << "enter tax rate: " << percent; // here percent symbol goes before number. double taxrate = 0.0; cin >> taxrate >> percent; // here tried adding cin after cin. cin >> taxrate >> '%'; // here tried adding char itself, yet failed attempt...
so, possible wanting?
it possible, iostream
not provide proper interface perform it. typically achieving greater control on console io requires use of platform-specific functions. on windows vs done _getch
this:
#include <iostream> #include <string> #include <conio.h> #include <iso646.h> int main() { ::std::string accum{}; bool loop{true}; { char const c{static_cast<char>(::_getch())}; switch(c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { // todo limit accumullated chars count... accum.push_back(c); ::std::cout << c << "%" "\b" << ::std::flush; break; } case 'q': { loop = false; accum.clear(); break; } case '\r': // enter pressed { // todo convert accumullated chars number... ::std::cout << "\r" "number set " << accum << "%" "\r" "\n" << ::std::flush; accum.clear(); break; } default: // else pressed. { loop = false; accum.clear(); ::std::cout << "\r" "oops!! " "\r" << ::std::flush; break; } } } while(loop); ::std::cout << "done" << ::std::endl; return(0); }
Comments
Post a Comment