c++ - comparing characters causes a bus error -
i'm lost trying figure out runtime error. have struct datatype, , implementation of in array items[]
. in struct, there char name
defined. i'm reading user input using cin char
datatype.
i=0; { printf("%c\n", items[i].name); printf("%c\n", itemname); //if ( items[i].name == itemname ) //found=true; i++; } while (i<numofitems || found);
if uncomment if statement, printf("%c\n", itemname);
seems run thousands of times followed bus error: 10
. note numofitems current 5.
output way written:
a c b c c c d c e c
any idea why can't compare if 1 char equivalent another?
your condition should i<numofitems && !found
. right now, if either true, keeps looping, , found
becomes true on third iteration.
i'd rewrite this:
for (int = 0; <numitems; ++i) std::cout << items[i].name << '\n'; std::cout << itemname << '\n'; if ( items[i].name == itemname ) break; }
Comments
Post a Comment