Anthony Tugman
Anthony Tugman

Reputation: 1

C++ Displaying An Organized List of Words

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.

Upvotes: 0

Views: 91

Answers (3)

pasaba por aqui
pasaba por aqui

Reputation: 3529

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?

Upvotes: 1

christopherhernandez
christopherhernandez

Reputation: 67

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?

Upvotes: 0

vsoftco
vsoftco

Reputation: 56567

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.

Upvotes: 0

Related Questions