c++ - Automatic number issued by the system with array -
i wrote:
string a[100] = { "b", "f", "r", "p", "d", "\0" }; class bd{ //data member static int id; //....... }; void setid(int d){ id = d; (int = 0; < 5; i++){ id = + 1; user[id]; } cout << " id : " << id << endl; } int bd::id = 0;
in code, has array include name of user , each user different id number in run (output) 5 ! know it's logical error. when enter : b
id : 5
but want print :
if user enter name `b` id : 1
if user enter name f
id : 2
you rewriting id in loop
id = + 1;
thus last used in body of loop equal 4 id = 5
edit: if want find string in array of strings , return index + 1 function can like
int getid( const std::string &s ) { string a[100] = { "b", "f", "r", "p", "d", "\0" }; int id = 0; while ( a[id] != "\0" && a[id] != s ) ++id; return a[id] == "\0" ? 0 : id + 1; }
however seems not understand difference between objects of type std::string , character arrays , string literals. need different function showed.
Comments
Post a Comment