why trying input 2 strings using cin.get() fails? can read first string input fails second string , subsequent operations.. see code:
#include <iostream> #include <stdlib.h> int main(){ long int n,k; char a[11],b[11]; cin.get(a,11); n = atoi(a); cin.get(b,11); cout<<b; k = atoi(b); cout << "\ncin.rdstate(): " << cin.rdstate() << "\n cin.eof(): " << cin.eof() << "\n cin.fail(): " << cin.fail() << "\n cin.bad(): " << cin.bad() << "\n cin.good(): " << cin.good() << endl << endl; }
i trying input 2 strings , store them long int variables shown in program, cin.get(b,11)
fails , stack overflow occurs k= atoi(b)
.also, may observe nothing output cout<<b
.. and, @ last cin.fail()
set 1 , means doing kind of logical error.. please me in rectifying this! please suggest method fast , meant c++ .. (if feel question bad please mention in comments before down voting this, struggling @ 21 rep!)
\n remain in buffer after first cin. can solve problem adding empty cin.get()
cin.get(a,11); n = atoi(a); cin.get(); cin.get(b,11); cout<<b; k = atoi(b);
Comments
Post a Comment