我正在开展一个学校项目,我需要创建一个幸运饼干计划。该程序的基础是它创建一个随机数,然后将其分配给将在屏幕上打印出来的数组中的“财富”。我需要帮助获取生成的随机数以打印出数组中预定义的字符串。
另外为了让它看起来很花哨,我想制作一个for循环,它会在每个财富周围创建一个边框,因为它们的长度不同,所以它看起来很有凝聚力,看起来比手动操作要好。
任何帮助都会有所帮助
这是代码:
fortunecookie.h
#ifndef FORTUNECOOKIE_H_INCLUDED
#define FORTUNECOOKIE_H_INCLUDED
#include <cstring>
#include <cstdlib>
using namespace std;
class Fortunecookie
{
private:
string fortune[10];
int rand_index;
public:
void openFortuneCookie();
void generateNewFortune();
FortuneCookie();
};
#endif // FORTUNECOOKIE_H_INCLUDED
fortunecookie.cpp
#include <iostream>
#include <ctime>
#include "fortunecookie.h"
using namespace std;
void Fortunecookie::generateNewFortune()
{
string fortune [10] = {" One that would have the fruit must climb the tree",
" A new opportunity awaits you at the fork of the road",
" The early bird gets the worm, but the second mouse gets the cheese. ",
" You are cleverly disguised as responsible adult.",
" The best things in life aren't things",
" Forget injuries; never forget kindnesses.",
" Borrow money from a pessimist. They don't expect it back",
" Your good enough, strong enough and gosh darnit' people like you",
" A feather in the hand is better than a bird in the air. ",
" In life, you must be the water"
};
for (int i=0; i<10; i++)
{
srand(time(0));
rand_index = rand() %10 +1;
}
}
Fortunecookie::FortuneCookie()
{
if (rand_index == 1)
{
fortune[1]=rand_index;
}
if (rand_index == 2)
{
fortune[2]=rand_index;
}
if (rand_index == 3)
{
fortune[3]=rand_index;
}
if (rand_index == 4)
{
fortune[4]=rand_index;
}
if (rand_index == 5)
{
fortune[5]=rand_index;
}
if (rand_index == 6)
{
fortune[6]=rand_index;
}
if (rand_index == 7)
{
fortune[7]=rand_index;
}
if (rand_index == 8)
{
fortune[8]=rand_index;
}
if (rand_index == 9)
{
fortune[9]=rand_index;
}
if (rand_index == 10)
{
fortune[10]=rand_index;
}
}
void Fortunecookie::openFortuneCookie()
{
Fortunecookie::generateNewFortune();
cout << " |====================================================================| \n";
cout << " |\t\t\t\t\t\t\t\t | \n";
cout << " |\t\t\t\t\t" <<rand_index<< "\t\t\t |\n";
cout << " |\t\t\t\t\t\t\t\t | \n";
cout << " |====================================================================| \n";
}
main.cpp中
#include <iostream>
#include "fortunecookie.h"
using namespace std;
int main ()
{
Fortunecookie yourfortune;
yourfortune.generateNewFortune();
yourfortune.FortuneCookie();
yourfortune.openFortuneCookie();
return 0;
}
以下是项目详情:
在本实验中,您将创建一个Fortune Cookie类。上传包含该程序的三个文件的压缩文件。
- fortunecookie.h
- fortunecookie.cpp
- fortuneDriver.cpp
每个幸运饼干都有10个字符串,可以容纳不同的财富。你可以弥补10大财富。其中一个命运将是活跃的。将通过为阵列中的索引生成随机数来选择活跃的命运。
幸运饼干将有以下方法:
void generateNewFortune();简介:此函数创建一个代表不同财富的新随机索引。命运存储在名为fortunes的字符串数组中,大小为10.前提条件:已经初始化了10个命运的数组,因此没有空字符串。后置条件:已生成0到9之间的新索引并将其分配给rand_index。
void openFortuneCookie();简介:此函数在数组//字符串中显示rand_index的命运。示例输出如下所示:
| ========================= | |你会得到好消息! | ========================= |
前提条件:已经初始化了带有命运的字符串数组。已为rand_index指定了0到9之间的值。后置条件:以上面显示的格式显示随机命运。
幸运饼干();简介:默认构造函数为fortunes数组中的每个索引分配一笔财富。它还将rand_index初始化为0到9之间的随机数。前提条件:FortuneCookie在变量中有垃圾值。后置条件:已经初始化了FortuneCookie对象,因此rand_index的值为0 - 9,并且每个索引的财富数组都有一笔财富。
此代码中有几个错误:
Fortunecookie
或FortuneCookie
。main()
中,您不应该显式调用默认构造函数。如果您定义了对象,则在语句中调用它。string fortune [10] = {...};
中编写的财富。 Unfortunatley这是一个本地财富数组的声明,它隐藏了该类的财富数组,并且您希望将其初始化调整后的main()
:
int main ()
{
srand(time(0)); // only once, at the beginning of the programme
Fortunecookie yourfortune; // this calls the default constructor
//yourfortune.generateNewFortune();// not needed, you call it in openFortune()
//yourfortune.Fortunecookie(); // OUCH !!
yourfortune.openFortuneCookie();
return 0;
}
然后构造函数应该接管字符串初始化。这里有一个固定字符串数组作为初始化程序的示例。但您也可以单独分配每个数组元素:
Fortunecookie::Fortunecookie()
: fortune{" One that would have the fruit must climb the tree",
" A new opportunity awaits you at the fork of the road",
" The early bird gets the worm, but the second mouse gets the cheese. ",
" You are cleverly disguised as responsible adult.",
" The best things in life aren't things",
" Forget injuries; never forget kindnesses.",
" Borrow money from a pessimist. They don't expect it back",
" Your good enough, strong enough and gosh darnit' people like you",
" A feather in the hand is better than a bird in the air. ",
" In life, you must be the water"
}
{
rand_index = rand() %10; // number between 0 and 9
}
然后简化了新cookie的生成(尽管你可以完全摆脱这个函数,因为索引已经在cookie创建中用随机数初始化):
void Fortunecookie::generateNewFortune()
{
rand_index = rand() %10;
}
您还可以通过添加字符串来改善财富的打印:
void Fortunecookie::openFortuneCookie()
{
generateNewFortune();
cout << " |====================================================================| \n";
cout << " | | \n";
cout << " | " <<setw(2)<<rand_index+1<<"-"<<setw(63)<<fortune[rand_index]<< " |\n";
cout << " | | \n";
cout << " |====================================================================| \n";
}
在这里,这里是一个online demo