typechecking - to check type of input in c++ -


## check type of data entered in cpp ##

int main() {     int num;     stack<int> numberstack;     while(1)     {         cin>>num;         if(isdigit(num))             numberstack.push(num);         else             break;     } return(0); } 

if declare variable interger, , input alphabet, 'b', instead of number, can check behavior of user? code above exits when first number entered , not wait more inputs.

first of all, std::isdigit function checks if character digit.

secondly, using input operator >> make sure input number, or state flag set in std::cin object. therefore e.g.

while (std::cin >> num)     numberstack.push(num); 

the loop end if there's error, end of file, or input not valid int.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -