arrays - C++ output duplicating lines when there is a '\n' -
i'm having trouble member function of class. basically, it's meant translate words different language while maintaining same punctuation , spaces. linetotranslate input argument array of words, spaces , punctuation. each word within has taken out of line individually , translated using dict.translate() function. working properly.
however, trouble when there multiple new lines, previous line of words output. whitespaces not looked after. when there more 1 space in sentence, 1 space output. idea's on might going wrong? appreciated.
updated code entered errors fixed. issue i'm having spaces aren't added needed between words. there 2 blank spaces in row, 1 blank space being entered there 1 blank space, none being entered , words areoutputlikethis.
int len = strlen(linetotranslate); string strcomplete = ""; const char *cs; (int x = 0; x < len; x++) { if (!isspace(linetotranslate[x])) { char temp[max_word_len]; int j = 0; while(linetotranslate[x] != ' ' && linetotranslate[x] != '\t' && linetotranslate[x] != '\n') { temp[j] = linetotranslate[x]; x++; j++; } temp[j] = '\0'; char returned[max_word_len]; if(temp[0] != '\0') { dict.translate(returned, temp); strcomplete = strcomplete + returned; } } else { strcomplete = strcomplete + linetotranslate[x]; x++; } } cs = strcomplete.c_str(); strcpy(translatedline, cs);
when for loop iterating spaces or punctuation dnt qualify enter while loop still executing strcomplete = strcomplete + returned; appending \0 in middle without reason, have output string -- <space>\0<space>\0
please see default values in array
so solution put strcomplete = strcomplete + returned; inside if. array uninitialized when not enter if(temp[0] != '\0') should not appending returned.
next....the 2 lines below should out of for loop, since want final result of strcomplete copied translatedline instead of each iteration.
cs = strcomplete.c_str(); strcpy(translatedline, cs);
Comments
Post a Comment