Results 1 to 4 of 4

Thread: pig letin generator readout problems

  1. #1
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy pig letin generator readout problems

    I writing a pig latin generator. Some times it prints out the english phrase fine, other times it doesn't
    For instance "i want to have fun prints out
    iay antway tnoway avehay eunfay
    The buffer doesn't empty out after each word.
    Help !

    Here is my code:
    Qt Code:
    1. #include <iostream>
    2. #include <cstring>
    3. #include <cstdlib>
    4.  
    5. using namespace std;
    6.  
    7. int len=0,i;
    8. char postfix[3]="ay";
    9. char tmp[2]="\0";
    10. char word[10]="\0";
    11.  
    12. void printLatinWord(char *token)
    13. {
    14. len=strlen(token);
    15. //get first char
    16. strncpy(tmp,token,1);
    17. ///copy backwards to get string
    18. strrev(token);
    19. strncpy(word,token,len-1);
    20. // reverse it back no we have the string
    21. strrev(word);
    22. //add first and postfix;
    23. cout<<word<tmp<<postfix<" ";
    24.  
    25.  
    26. }
    27.  
    28.  
    29. int main()
    30. {
    31.  
    32. char *token;
    33. char sentance[50];
    34. cout<<"Enter in English sentance to be translated->";
    35. cin.getline(sentance,50);
    36. token=strtok(sentance," ");
    37. while (token != NULL )
    38. {
    39. printLatinWord(token);
    40. token=strtok(NULL, " ");
    41. }
    42.  
    43. system("Pause");
    44. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Petr_Kropotkin; 7th March 2010 at 17:51.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: pig letin generator readout problems

    strncpy will not include the null terminator if the length specified in the third argument is less than the string given in the second argument. Therefore you words are not null terminated, and will get whatever garbage is there previously. You either need to initialise the array your copying to, or change your algorithm.

  3. #3
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: pig letin generator readout problems

    I thought i did initialise the arrays.
    I thought it was initiaised to
    char postfix[3]="ay";
    char tmp[2]="\0";
    char word[10]='\0'

    if word[10] is not initiaiised how about this strcpy(word, '0');

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: pig letin generator readout problems

    Consider this:

    Qt Code:
    1. wordA[8] = 'ABCDEFGH';
    2. wordB[4] = 'WXYZ';
    3. strncpy (wordA, wordB, 4);
    To copy to clipboard, switch view to plain text mode 

    wordA now contains: 'WXYZEFGH';
    if you want wordA to contain 'WXYZ' you need to do wordA[4] = 0; or strncpy (wordA, wordB, 5);

Similar Threads

  1. EAN13 Barcode class & test generator
    By SteveH in forum Qt-based Software
    Replies: 2
    Last Post: 27th December 2013, 08:11
  2. JulyButton Class Generator
    By IGHOR in forum Qt Programming
    Replies: 7
    Last Post: 8th October 2008, 13:05
  3. Win32 UML generator from C++ .h files?
    By hickscorp in forum General Discussion
    Replies: 1
    Last Post: 13th May 2007, 18:05
  4. Random No Generator in C/C++
    By ankurjain in forum General Programming
    Replies: 1
    Last Post: 6th July 2006, 11:33
  5. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.