如何将数字"Bind"到一串单词/短语中,以便我可以循环调用它?
How to "Bind" a number to a string of words/phrase so that I can call it up in a loop?
我正在做一个项目,需要让电脑打印12天的圣诞歌词。我想到了一个主意,我做一个FOR循环,让它重复12次。每当一元运算符"++"改变一天时,我的意思是:
int main()
{
string Print = first = 1; //Here I want first to become a number so that I can call it up in FOR loop.
cout << "On the first day of Christmas, nmy true love sent to menA partridge in a pear tree.n" << endl;
for(int loop = 0; loop <= 12; loop++)//This part is a simple for loop, it starts at 0 and goes to 12 until it stops.
{
cout << "On the " << (1,2,3,4,5,6,7,8,9...12) << " day of Christmas,nmy true love sent to men" << endl; HERE!!!!
这就是我的问题所在。我想把这些数字串起来,以表示当天。在x=1中,我会调用"First",然后我可以使用"x++"将数字向上移动,这将导致x=2,然后它会说"Second"。。一直到12岁。有人知道我该如何解决这个问题吗?}
这涉及到编程中一个简单但重要的部分,称为数组。我不想直接给你答案——你需要一直使用这些(或类似的结构),练习和理解它们非常重要。让我们使用打印"Hello World"的数组制作一个简单的程序:
#include <iostream>
#include <string>
int main() {
std::string words[2]; //make an array to hold our words
words[0] = "Hello"; //set the first word (at index 0)
words[1] = "World"; //set the second word (at index 1)
int numWords = 2; //make sure we know the number of words!
//print each word on a new line using a loop
for(int i = 0; i < numWords; ++i)
{
std::cout << words[i] << 'n';
}
return 0;
}
你应该能够想出如何使用类似的策略来获得你上面要求的功能。我在这里工作。
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 如何将数字"Bind"到一串单词/短语中,以便我可以循环调用它?