C ++显示有组织的单词列表

时间:2015-05-23 18:35:13

标签: c++ visual-studio-2010

我正在使用C ++制作一个20个问题的游戏,并且除了displayWords函数外,一切都正常。我目前的代码一直在破碎。任何解释将不胜感激!谢谢!

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;
    }
}

我希望以有组织的方式显示50个单词的列表。

3 个答案:

答案 0 :(得分:1)

例如,as:

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

考虑到数组大小计算的问题:请参阅此链接How do I find the length of an array?

答案 1 :(得分:0)

如果我理解正确,您希望列表显示为5列。最简单的方法是,使用嵌套的public static final String DATABASE_NAME = "your_data.db"; public database(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); String path=context.getDatabasePath(database.DATABASE_NAME).getAbsolutePath(); } 循环以及std::setw的正确格式(必须for):

#include <iomanip>

您的实际循环不正确,因为它会导致重复。

答案 2 :(得分:0)

也许我没有正确解释您的问题,但如果您只想打印50个字,那么您可以使用类似下面的代码。不确定嵌套for循环迭代y的原因是什么。

修改

&#13;
&#13;
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]<< ", "; 
  }
}
&#13;
&#13;
&#13;

还有一些关于代码如何破坏的信息,比如抛出的任何错误或调试到目前为止都会引起问题?