c++ - Using cin, how can I accept a character or an integer as an input? -


i'm writing program accepts card rank input, coverts these ranks values represent. examples include a, 5, 10, k. i've been trying figure out ways this.

i thought accepting char, converting it, so...

char input = 0; std::cin >> input; if(input < 58 && input > 49) //accepting 2-9 { //convert integers } else if(input < 123 && input > 64) { //convert characters , check if they're valid. } 

and work...except 10 unfortunately. what's option works?

why not use code have, , have special case, in third if block, handle 10?

since there's no valid input besides 10 starts 1, should pretty straightforward:

char input = 0; std::cin >> input; if(input < 58 && input > 49) //accepting 2-9 { //convert integers } else if(input < 123 && input > 64) { //convert characters , check if they're valid. } else if(input == 49){ //accepts 1     std:cin >> input; //takes second character     if(input == 48){ //this 10         //do stuff 10     }     else{         //throw error, 1 followed 0 invalid input     }  } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -