0

I am making a 20 questions game in C++ and have everything working, except for the displayWords function. The code I currently have keeps breaking. Any explanation would be appreciated! Thank you!

void displayWords()
{
    int x = 0;
    string words[50] = {"LCHS","Shark","Pencil","Pizza","New York","Fish","Car","Ice Cream","Los Angeles","Bird","Basketball","Fried Chicken",
        "Dog","Tiger","Penguin","Plane","Rock","Barbecue Sauce","Mustard","Ketchup","Hot sauce","Peppers","Salt","Tacos","Shrimp","Pickels",
        "Tomatos","Bannanas","Burger","Computer","Iphone","Motorcycle","Bicycle","Skateboard","Lightbulb","Golf Ball","Surfboard","Luggage",
        "Rollercoaster","Cat","Lion","Cockroach","Grasshopper","Beach","Theme Park","Swimming Pool","Bowling Ally","Movie Theater","Golf Course","Shopping Mall"};

    cout << "The following list of words are what the computer is capable of guessing" << endl;
    cout << endl;

    while(x < 50)
    {
        for (int y = 0; y <= 5; y++)
        {
            cout << words[x] << ", ";

            if(x<50)
            x++;
        }
        cout << endl;
    }
}

I would like it to display the list of 50 words in an organized fashion.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • 1
    first, organize your question first! second, what error are you getting? third, why are you using that for in the while? fourth, what do you mean by "organized fashion"? – Sinapse May 23 '15 at 18:39
  • You print 6 strings per line. After 8 full lines, you only have two words left. You print those, end up with `x==50`, then proceed to access `words[50]` - an index out of bounds - a few more times. – Igor Tandetnik May 23 '15 at 18:41
  • 1
    Define _"organized fashion"_ more clearly please! – πάντα ῥεῖ May 23 '15 at 18:42
  • By organized fashion, I mean in a list or table format that will be easy for the user to understand. I am new to coding, and only basing this program off of examples in my book, which is why I am running into this problem. – Anthony Tugman May 23 '15 at 18:46
  • you need to track the array size so you won't give an `index out of range` error. Either control it manually (with a size variable) or use a container like std::vector which will track the size of the array automatically – Sinapse May 23 '15 at 19:03

3 Answers3

1

By example, as:

for( int x = 0; x<sizeof(words)/sizeof(*words); x++ ) {
        if( x%5==0 ) cout << endl; else cout << ", ";
        cout << words[x];
}

take into account the problematic of the array's size calculation: see this link How do I find the length of an array?

Community
  • 1
  • 1
pasaba por aqui
  • 3,446
  • 16
  • 40
0

If I understand correctly, you want your list displayed as 5 columns. Simplest way, use a nested for loop and proper formatting with std::setw (must #include <iomanip>):

for(size_t i = 0; i < 10; ++i)
{
    for(size_t j = 0; j < 5; ++j)
    {
        std::cout << std::setw(20) << std::left << words[i * 5 + j];
    }
    std::cout << std::endl;
}

Your actual loop is incorrect, as it will lead to repetitions.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
0

Maybe I'm not interpreting your question correctly but if you want to just print out the 50 words then you can use something like the code below. Not sure of the reason that the nested for loop iterating y was there.

Edit

void displayWords()
{
    int x;
    string words[50] = {"LCHS","Shark","Pencil","Pizza","New York","Fish","Car","Ice Cream","Los Angeles","Bird","Basketball","Fried Chicken",
        "Dog","Tiger","Penguin","Plane","Rock","Barbecue Sauce","Mustard","Ketchup","Hot sauce","Peppers","Salt","Tacos","Shrimp","Pickels",
        "Tomatos","Bannanas","Burger","Computer","Iphone","Motorcycle","Bicycle","Skateboard","Lightbulb","Golf Ball","Surfboard","Luggage",
        "Rollercoaster","Cat","Lion","Cockroach","Grasshopper","Beach","Theme Park","Swimming Pool","Bowling Ally","Movie Theater","Golf Course","Shopping Mall"};

    cout << "The following list of words are what the computer is capable of guessing" << endl;
    cout << endl;

    for(x = 0; x < words.size();x++)
  {
   cout << words[x]<< ", "; 
  }
}

Also some information on how the code is breaking, like are any errors being thrown or has debugging caused issues so far?