c - passing a structure for comparison -
struct sign_in { char password[max_name_len+1];//the password each player char name[max_name_len+1];//name of people can sign in } //prototype int compare_names(char*, char*, struct sign_in*); int compare_names(char*pname,char*ppasscode,struct sign_in *var) { int icomparison = 1; int flag = 1; int icomparison2 = 1; int = 0; (i=0;i<6;i++) { printf("%s \t %s ", var[0].name,pname ); if(icomparison != 0) { icomparison = strcmp(pname,var[i].name); i++; } if(icomparison2 != 0) { icomparison2 = strcmp(ppasscode,var[i].password); i++; } printf("%d", icomparison); printf("%d", icomparison2); } }
i have updated code , attempted take account many of aspects guys have recommended , news runs now. bad news still attempts print random jargon don't understand, it's collection of symbols usually. structure function compares against has 6 members that's reason parameters on first loop.
the code you've presented cornucopia of sloppyness. when programming, that's not ok.
- you forgot closing curly braces
struct sign_in
definition ,compare_names()
function definition - you did not initialize
icomparisson
value.flag
initialized,icomparisson
not. also, it's misspelled! - don't use
printf()
user-input format string, there%
in there. @ leastprintf("%s", pname)
. , want\n
in there too. strcmp()
might return-1
meanpname
sorts beforevar[i].name
(and differs of course),while(icomparisson == 1)
not want- you need know length of
var
array , stop loop before run off end strcmp()
takes strings, pointers. when callstrcmp(*pname, ...)
you're dereferencing pname "pointer char" "char". it's getting first character pname string, , putting character value pointer-to-character value expected. not good. situationvar[i].name
bit more complicated because name array, rid of star, it's not needed either.- the second
while ()
loop loop forever ifipasscode
not match, wantif ()
- in problem description omit closing backtick after
*var[i].password
, closing double-quote after "invalid type argument of unaray", , mangled compiler error message well. makes harder understand wrote , went wrong. - the
ipasscode == var[i].password
looks fine. seems rather isn't code had problem with, due other ridiculous problems in sample ...
Comments
Post a Comment