visual studio 2010 - c++显示有组织的单词列表

visual studio 2010 - C++ Displaying An Organized List of Words

本文关键字:有组织 单词 列表 显示 c++ studio 2010 visual      更新时间:2023-10-16

我正在用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个单词的列表。

例如:

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

考虑到数组的大小计算的问题:看看这个链接我如何找到一个数组的长度?

如果我理解正确,您希望您的列表显示为5列。最简单的方法是使用嵌套的for循环并使用std::setw(必须是#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;
}

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

也许我没有正确地解释你的问题,但是如果你只想打印出50个单词,那么你可以使用下面的代码。不确定为什么嵌套的for循环要迭代y。

编辑

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

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