2

I'm having some trouble with this member function of a class. Basically, it's meant to translate words to a different language while maintaining the same punctuation and spaces. lineToTranslate is an input argument which is an array of words, spaces and punctuation. Each word within this has to be taken out of the line individually and translated using the dict.translate() function. This is working properly.

However, the trouble is that when there is are multiple new lines, the previous line of words are output. Also whitespaces are not fully looked after. When there is more than one space in a sentence, only one space is output. Any idea's on where I might be going wrong? Any help would really be appreciated.

Updated code entered with most errors fixed. The only issue I'm having now is that spaces aren't added as needed between words. Where there are 2 blank spaces in a row, one blank space is being entered but where there is one blank space, none is being entered and the words areoutputlikethis.

int len = strlen(lineToTranslate);
string strComplete = "";
const char *cs;

for (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);
Michael
  • 199
  • 1
  • 9
  • I think you need a little more loop control than you first thought. I believe the idea you're trying to flesh out is to copy *all* data from the original string to the target, but pick out each word and substitute in the dictionary translation. You're apparently intending on using whitespace or punctuation as your word-delimiters, and said-whitespace/punctuation must be *kept*, but the words they separate replaced. Is that an accurate assessment of your intention with this code? – WhozCraig May 10 '13 at 16:05
  • is `ispunct` you code, if yes, can we see its implementation? I am guessing the behavior you are observing may be coming from its implementation. – Bill May 10 '13 at 16:08
  • @Bill I would hazard to guess `ispunct()` is likely the implementation [from the standard library](http://en.cppreference.com/w/cpp/string/byte/ispunct), but it would indeed be nice to know for sure. – WhozCraig May 10 '13 at 16:10
  • Thanks for your help WhozCraig. Yes, so the array that is passed as an input argument is divided into words based on whitespaces for a start. So this part is working fine and the words are getting translated as expected. However these spaces are not staying in the same as the original array has passed in. e.g. only one space is being output when there should be two. Also lines are being repeated when there is are two newline characters instead of there being a newline – Michael May 10 '13 at 16:11
  • one more possible error i see -- when your `for` loop is iterating for `spaces` or `punctuation` which dnt qualify to enter `while` loop you are still executing `strComplete = strComplete + returned;` which is appending `\0` in the middle without any reason, so you have output string like -- `\0\0` ..makes sense? – Bill May 10 '13 at 16:12
  • Yes ispunct() is the implementation from the standard library. – Michael May 10 '13 at 16:16
  • Can you pls try putting `strComplete = strComplete + returned;` inside the `if` and see if that helps? Your array is `uninitialized` when it does not enter `if(temp[0] != '\0')` so you should not be appending `returned`...that is definitely an issue for sure. – Bill May 10 '13 at 16:19
  • also shouldn't these two lines be out of the `for` loop?? `cs = strComplete.c_str(); strcpy(translatedLine, cs);` – Bill May 10 '13 at 16:21
  • @Bill I think you got it there by putting the cs = strComplete.c_str(); strcpy(translatedLine, cs); out of the loop. Just a little more testing needed I think. That has stopped lines being duplicated where there are two newline anyway. Thanks a lot for your help. – Michael May 10 '13 at 16:29
  • ok, i posted an answer -- FYI ..if there are more issues, pls let me know. – Bill May 10 '13 at 16:35
  • Thanks @Bill. I'll let you know if there are any more problems in case anything needs to be updated. – Michael May 10 '13 at 16:42
  • Hi @Bill. I've updated the code with the recommended fixes and they work very well. The only issue I'm having now is that spaces aren't added as needed between words. Where there are 2 blank spaces in a row, one blank space is being entered but where there is one blank space, none is being entered and the words areoutputlikethis. – Michael May 10 '13 at 17:47
  • @Michael You do not need `x++;` in the `else` block, your `for` loop should take care of that :) – Bill May 10 '13 at 18:02
  • also, the way your logic works, if `lineToTranslate[x]` is a `space`, you dnt increment `x`, but `for` loop increments the value, so you miss the space...so what you can do is add `x--` after `temp[j] = '\0';` so that the next iteration takes the `space`...hope this helps...let me know if this does not fix this. – Bill May 10 '13 at 18:07
  • 1
    That's exactly it @Bill. Code is running 100% now. Thank so much for your help, would have taken me months to figure all that out! – Michael May 10 '13 at 18:31

2 Answers2

4

When your for loop is iterating for spaces or punctuation which dnt qualify to enter while loop you are still executing strComplete = strComplete + returned; which is appending \0 in the middle without any reason, so you have output string like -- <space>\0<space>\0

Please see Default values in array

So solution is to put strComplete = strComplete + returned; inside the if. Your array is uninitialized when it does not enter if(temp[0] != '\0') so you should not be appending returned.

Next....the two lines below should be out of the for loop, since you want the final result of strComplete to be copied to translatedLine instead of each iteration.

cs = strComplete.c_str(); 
strcpy(translatedLine, cs);
Community
  • 1
  • 1
Bill
  • 5,263
  • 6
  • 35
  • 50
0

For whitespace I can say that for the first whitespace, it will check the if condition, then the iteration will terminate and we go back to the for loop, x is incremented and now lineToTranslate[x]=' '. Correct? Okay so the while loop never runs. If condition if (temp[0]!='\0') is satisfied. So now what does return have in store? It isn't initialised. Ur still appending it. Maybe I am not much help but this is what I figured out. Try debugging.

Rabbiya Shahid
  • 422
  • 3
  • 14