visual studio 2010 - c++显示有组织的单词列表
visual studio 2010 - C++ Displaying An Organized List of Words
我正在用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]<< ", ";
}
}
还有一些关于代码如何被破坏的信息,比如是否抛出了任何错误,或者到目前为止是否有调试引起的问题?
相关文章:
- 我有一个字符串变量单词= "APA"
- 有什么方法可以在 c++ 中组织"cycling"(循环)数字?
- 当字符串中只有一个"GOOD"字符串可用时,从没有空格的字符串中提取第一个单词的最佳和有效方法?
- 打印所有可能有 4 个字母的单词的时间太多
- 如何组织与两个对象有联系的变量
- 有没有办法在C 中扫描TXT文件中的单词或名称
- 在文件中遇到指定单词后,是否有读取数据的方法
- 是否有任何窗口函数可以使用域名获取所有OU(组织单位)名称
- 我需要一些帮助以有组织的方式格式化文件
- 是否有手动组织COM封送处理的方法
- 确定字符串中有多少单词被格式化为MMDDYY形式的日期,并对其进行计数.(C++)
- 有组织的点云的计算正常问题
- 有没有一个网站可以生成所有可能的显示单词的方式
- 使用 str.find() 查看单词中是否有特定字母
- 我编写了一个C++程序来打印单词字母的排列.我想知道是否有其他更简单的方法可以做到这一点
- 如何将特定数量的字符转换为大写或小写,并计算单词有多少个大写和小写字母
- c ++ 如何提取单词之间的空格(如果有)
- 字符串流中有一个单词后的奇怪行为
- visual studio 2010 - c++显示有组织的单词列表
- c++ -更有组织和高效的函数原型