Results 1 to 4 of 4

Thread: string handling problems

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

    Unhappy string handling problems

    I can;t read my array of strings.
    I am creating a random sentance generator program.
    Here is the code I have so far:
    Qt Code:
    1. int main()
    2. {
    3. char *totalSentances[200];
    4. const char *verb[]={"drove","jumped","ran","walked","skipped","fucked","smoked"};
    5. const char *noun[]={"boy","girl","dog","town","car","ganja","pussy"};
    6. const char *prep[]={"to","from","over","under","before","into","by"};
    7. const char *article[]={"the","a","one","some","any"};
    8. char temp[10],sentance[100]={NULL};
    9.  
    10.  
    11. int word_gen,i=0,article_gen,sentance_gen,j=0;
    12. srand(time(0));
    13.  
    14. for(i=1;i<=200;++i)
    15. {
    16. word_gen=0+rand()%6;
    17. article_gen=0+rand()%4;
    18.  
    19. strcpy(temp,article[article_gen]);
    20. strcat(sentance,temp);
    21. strcat(sentance," ");
    22.  
    23. strcpy(temp,noun[word_gen]);
    24. strcat(sentance,temp);
    25. strcat(sentance, " ");
    26.  
    27. strcpy(temp,verb[word_gen]);
    28. strcat(sentance,temp);
    29. strcat(sentance, " ");
    30. strcpy(temp,prep[word_gen]);
    31. strcat(sentance,temp);
    32. strcat(sentance, " ");
    33. strcpy(temp,article[article_gen]);
    34. strcat(sentance, " ");
    35. strcpy(temp,noun[word_gen]);
    36. strcat(sentance,temp);
    37. totalSentances[i]=sentance;
    38. cout<<sentance<<endl;
    39. *sentance=NULL;
    40. }
    To copy to clipboard, switch view to plain text mode 

    this prints out fine. I get garbage of nothing when i do this:
    Qt Code:
    1. fOR( j=1;j<100;++j)
    2. cout<<totalSentances[j]<<endl
    To copy to clipboard, switch view to plain text mode 

    or
    Qt Code:
    1. fOR( j=1;j<100;++j)
    2. cout<<*totalSentances[j]<<endl
    To copy to clipboard, switch view to plain text mode 

    How can I print out post load ? HELP!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: string handling problems

    It would be much easier for you if you used std::string instead of char *. Your code is currently pretty much unreadable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2010
    Posts
    73
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 8 Times in 8 Posts

    Default Re: string handling problems

    The code is very wrong and leads me to believe that you do not yet understand pointers and C++. Here are a few comments on your code and the behavior:

    You do not need temp, you can use the other directly.
    Qt Code:
    1. strcpy(temp,article[article_gen]);
    2. strcat(sentance,temp);
    3. strcat(sentance," ");
    To copy to clipboard, switch view to plain text mode 

    This will accomplish the same thing

    Qt Code:
    1. strcat(sentance, article[article_gen]);
    2. strcat(sentance," ");
    To copy to clipboard, switch view to plain text mode 

    That said, you should simply use QString and be done with it. This avoids all those pointers and messy memory copies that ultimately lead to the failure. Consider lines 37-39:

    Qt Code:
    1. totalSentances[i]=sentance;
    2. cout<<sentance<<endl;
    3. *sentance=NULL;
    To copy to clipboard, switch view to plain text mode 

    The variable sentance is a an array of type char. In C, this also means that it is a char*. Does the value of sentance ever change? The answer is no, THE VALUE OF SENTANCE NEVER CHANGES. Remember, sentance points to an area of memory with space for 100 characters. The characters stored in those locations change, but sentance always points to the same block of memory.

    If you now look at every entry in totalSentances, every entry points at the same memory location. When you print the value, the value is correct, because you just set it. The next thing that you do is set the first entry to NULL, which means that you effectively set this to an empty string.

    In case you missed it, this means that every entry in totalSentances will, at the end of the loop point to the same memory location that contains an empty string.

    If you had an array of type QString, however, you would assign that. I might go so far as to recommend a QStringList, and then you can replace line 37 with

    Qt Code:
    1. totalSentances.append(sentance);
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. totalSentances << sentance;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2010
    Posts
    73
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 8 Times in 8 Posts

    Default Re: string handling problems

    Can't help myself.... look at this simple example:
    Qt Code:
    1. void randStringTest()
    2. {
    3. QStringList sentences;
    4. QStringList verbs; verbs << "drove" << "jumped" << "ran" << "walked" << "skipped" << "froliced" << "smoked";
    5. QStringList nouns; nouns << "boy" << "girl" << "dog" << "town" << "car" << "ganja" << "cat";
    6. QStringList preps; preps << "to" << "from" << "over" << "under" << "before" << "into" << "by";
    7. QStringList articles; articles << "the" << "a" << "one" << "some" << "any";
    8. // In c++0X, you can do this
    9. // QStringList articles({"the","a","one","some","any"});
    10.  
    11. srand(time(0));
    12.  
    13. for (int i=0; i<20; ++i)
    14. {
    15. int word_gen = 0+rand()%6;
    16. int article_gen=0+rand()%4;
    17. sentences << articles[article_gen] + " " + nouns[word_gen] + " " + verbs[word_gen] + " " + preps[word_gen] + " " + articles[article_gen] + " " + nouns[word_gen];
    18. qDebug(qPrintable(sentences.back()));
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    Result...
    the dog ran over the dog
    one boy drove to one boy
    a car skipped before a car
    a ganja froliced into a ganja
    the boy drove to the boy
    some town walked under some town
    the car skipped before the car

Similar Threads

  1. Handling QMouseEvents
    By Lodorot in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2009, 15:21
  2. std:string how to change into system:string?
    By yunpeng880 in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 09:51
  3. Bug in MDI handling
    By daren in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2009, 15:13
  4. Int to String - manipulating string
    By mickey in forum General Programming
    Replies: 6
    Last Post: 5th November 2007, 21:11
  5. string handling
    By vijay anandh in forum Qt Programming
    Replies: 4
    Last Post: 5th May 2006, 10:15

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.