c++ - Modifying pointer value -
i having trouble work assigned schooling. told write program modifies char. can fine. cannot how work pointers. i've researched this, , found solutions. don't meet assignment's needs. told define function this:
int stripwhite(char *userinput)
so did. problem is, believe makes copy of value. , not modify actual value. here code picked out of program:
inside main, declare char, , use cin.getline collect input. call stripwhite. cout char. value never changes. assignment says stripwhite needs defined above. @ lose here. help?
char userinput[100]; int spaces = 0; cin.getline(userinput,99); spaces = stripwhite(userinput); cout << userinput;
your function takes character pointer. based on function name , usage, function presumably intended take character buffer input , remove spaces while returning number of spaces removed.
so basically, have iterate through buffer, , move down contents while skipping spaces. easiest way maintain 2 pointers , copy 1 other.
here's how i'd (warning, not tested). since it's assignment you'll want write own code, example clear misconceptions have.
int stripwhite(char* userinput){ auto inpos = userinput; auto outpos = userinput; int count = 0; while(*inpos){ if(*inpos != ' '){ *outpos = *inpos; ++outpos; } else {count++;} ++inpos; } *outpos = '\0'; return count; }
Comments
Post a Comment