Thread: puting an element from many areas inside one big array?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    puting an element from many areas inside one big array?

    I made this random sentance generating program it generates 20 sentences and it's is currently working like it's supposed to. I need to change comething though. Instead of printing out the array elements individually, I am supposed to concatenate them into one array named that looks like this
    Code:
    char sentence[80]
    I don't know how to go about doing that though. I need to concatenante all of the elements in one array with spaces in between the words, a capital letter at the begining and a period at the end. I tried playing around with strcat, but didn't have much luck.

    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
    
    
    srand(time(NULL));
    
    char *article[]= {"the", "a", "one", "some", "any"};
    char *noun[] = {"boy", "girl", "dog", "town", "car"};
    char *verb[] = {"drove", "jumped", "ran", "walked", "skipped"};
    char *preposition[]= {"to", "from", "over", "under", "on"};
    
    for(int i = 1; i <21; i++)
    {
    int articleRand = rand() % 5 ;
    int nounRand= rand() % 5 ;
    int verbRand = rand() % 5 ;
    int prepositionRand= rand() % 5 ;
    int noun2 = rand() % 5;
    int article2 = rand() % 5;
    
    cout << article[articleRand] << " " << noun[nounRand] << " " 
    << verb[verbRand] << " " << preposition[prepositionRand]
    << " " << article[article2] << " " << noun[noun2] << "." << endl;
    }
    
    system("pause");
    
    return 0;
    }

    Any help would be greately appreciated!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Since you're using C style strings, strcat or strncat is probably the way to go. What did you try?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why ARE you using char arrays for strings in a C++ program?

    Code:
    ostringstream sentence;
    
    sentence << article[articleRand];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I guess I am using them because that's what my professor and book are asking me to do. The code I used for strcat I believe was something like this

    Code:
    strcat(sentence, *article[articleRand])
    I am not exactly sure if that's what it was, but something close, I'm not next to my book or other compuer right now so I can't say for sure. Is that the best method for this? If so, my syntax is obviously wrong.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's not exactly right, but it's close (you don't need the star there). When you get to a computer try to solve it with strcat and then if it doesn't work post that code and the problem you're having. It might just be a simple thing.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I got it to work when it's not in a loop. When it's in the loop, it acts pretty crazy then crashes. I tried moving some declarations around but that didn't seem to do much.

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    
    
    char sentence[80];
    srand(time(NULL));
    
    char *article[]= {"the", "a", "one", "some", "any"};
    char *noun[] = {"boy", "girl", "dog", "town", "car"};
    char *verb[] = {"drove", "jumped", "ran", "walked", "skipped"};
    char *preposition[]= {"to", "from", "over", "under", "on"};
    
    
    
    for(int i = 1; i <21; i++)
    {
    
    int articleRand = rand() % 5 ;
    int nounRand= rand() % 5 ;
    int verbRand = rand() % 5 ;
    int prepositionRand= rand() % 5 ;
    int noun2 = rand() % 5;
    int article2 = rand() % 5;
    
    strcat(sentence, article[articleRand]);
    strcat(sentence, " ");
    strcat(sentence, noun[nounRand]);
    strcat(sentence, " ");
    strcat(sentence, verb[verbRand]);
    strcat(sentence, " ");
    strcat(sentence, preposition[prepositionRand]);
    strcat(sentence, " ");
    strcat(sentence, article[article2]);
    strcat(sentence, " ");
    strcat(sentence, noun[noun2]);
    strcat(sentence, ".");
    
     
    
    
    
    cout << sentence << endl;;
    
    }
    
    system("pause");
    
    return 0;
    }

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I figured out that the first 3 characters in sentence must be undefined or something because it just shows these random characters there (not letters or anything, I don't really know what they are). So maybe that's why it crashes and acts funny when I put it in a loop?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I believe strcat looks for the first null character in the array to find where the current string ends. (That's how C-style strings indicate the end of the string.)

    If you don't set the first character of sentence to null, then it just has garbage in there and strcat will just look until it accidentally finds a null character. Then it will append the first string after that. That's why the first few characters were garbage, because strcat thought they were part of the sentence and added the first word after them.

    Every time you want to "erase" sentence, you should set the first character to '\0'.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if there was ever a perfect example of how NOT to use a char array, then this is it!

    Explain again WHY you're playing with crummy char arrays in a C++ program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by Salem View Post
    Well if there was ever a perfect example of how NOT to use a char array, then this is it!

    Explain again WHY you're playing with crummy char arrays in a C++ program.
    I am playing with char arrays because my professor assigned this program to us and I want to get a good grade in the class. Since I'm using this char array so bad do you have any suggestions for me?

  11. #11
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    I don't see what makes this such a good demonstration of how not to use char*'s (I've certainly seen much worse code, and not just from beginners).

    A couple of things:
    You need to include <cstdlib> to use rand().
    Your char*'s should be const char*'s.
    Do what daved told you to and you should be fine.

    I think it's fine here in this program, but in the future you should know that the line:
    char sentence[80];
    is very bad. This exact line of code is one of the number one causes of serious security flaws.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It would seem your prof's approach to teaching you to run begins with breaking your legs.

    Suggestions
    - make sure you initialise everything
    Your first mistake was strcat() on an uninitialised char array

    - make sure you count everything
    Don't blindly assume that strcat can append without overrunning the array.
    There are strncpy() and strncat() functions, but these also need care in their use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Okay, I know what you guys are saying about setting the first character to null. I did that, and now my program works exactly like it's supposed to. Thank you guys for your help regarding this. Here is my code

    Code:
    #include <iostream>
    #include<ctime>
    
    
    using namespace std;
    
    int main()
    {
    
    
    char sentence[80];
    
    srand(time(NULL));
    
    char *article[]= {"the", "a", "one", "some", "any"};
    char *noun[] = {"boy", "girl", "dog", "town", "car"};
    char *verb[] = {"drove", "jumped", "ran", "walked", "skipped"};
    char *preposition[]= {"to", "from", "over", "under", "on"};
    
    
    
    for(int i = 1; i <21; i++)
    {
    
    int articleRand = rand() % 5 ;
    int nounRand= rand() % 5 ;
    int verbRand = rand() % 5 ;
    int prepositionRand= rand() % 5 ;
    int noun2 = rand() % 5;
    int article2 = rand() % 5;
    sentence[0] = '\0';
    
    strcat(sentence, article[articleRand]);
    strcat(sentence, " ");
    strcat(sentence, noun[nounRand]);
    strcat(sentence, " ");
    strcat(sentence, verb[verbRand]);
    strcat(sentence, " ");
    strcat(sentence, preposition[prepositionRand]);
    strcat(sentence, " ");
    strcat(sentence, article[article2]);
    strcat(sentence, " ");
    strcat(sentence, noun[noun2]);
    strcat(sentence, ".");
    
     sentence[0] = toupper(sentence[0]);
    
    
    
    cout << sentence << endl;;
    
    }
    
    system("pause");
    
    return 0;
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by nick753 View Post
    I am playing with char arrays because my professor assigned this program to us and I want to get a good grade in the class. Since I'm using this char array so bad do you have any suggestions for me?
    Have you actually asked if you are allowed to use std::string or not?
    Sometimes you may be given bad code, but you are allowed to "fix" it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Absolute value of each element in a given array
    By DriftinSW20 in forum C Programming
    Replies: 9
    Last Post: 11-15-2007, 04:08 PM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM