C ++随机数,用于在数组中打印字符串

时间:2018-09-08 21:51:08

标签: c++ arrays string random

我正在做一个学校项目,我需要创建一个幸运饼干程序。该程序的基本原理是创建一个随机数,然后将其分配给将显示在屏幕上的数组中的“财富”。我需要获取生成的随机数以打印出数组中预定义字符串的帮助。

还要使它看起来花哨,我想制作一个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(); 摘要:此函数创建一个新的随机索引,该索引 代表不同的命运。命运被储存 在大小为10的称为财富的字符串数组中。 先决条件:十个财富的数组 初始化,以便没有空字符串。 后置条件:已经建立了0-9之间的新索引 生成并分配给rand_index。

void openFortuneCookie(); 摘要:此函数在字符串数组//中的rand_index处显示财富。样品输出 如下所示:

| ======================== | |您将获得好消息!| | ======================= |

前提条件:带有财富的字符串数组已被初始化。 已为rand_index分配了0到9之间的值。 后置条件:随机命运以以下格式显示 如上所示。

FortuneCookie(); 摘要:默认构造函数为每一个分配财富 财富数组中的索引。它还会初始化rand_index 从0到9的随机数。 前提条件:FortuneCookie在 变量。 后置条件:FortuneCookie对象已初始化,因此 rand_index的取值范围为0 – 9,而运气数组的取值范围为 每个索引的财富。

1 个答案:

答案 0 :(得分:0)

此代码中有几个错误:

  • 第一个C ++区分大小写,因此必须一次选择FortunecookieFortuneCookie
  • 默认构造函数是构造新cookie时首先创建的东西。根据要求,您应该在那里初始化财富字符串。
  • main()中,您不应显式调用默认构造函数。在定义对象的语句中调用它。
  • 您尝试初始化在成员函数string fortune [10] = {...};中编写的财富。不幸的是,这是一个本地财富数组的声明,它隐藏了该类的财富数组,并且您希望对其进行初始化
  • 索引应介于0到9之间,否则您将超出范围。
  • 如果链条令人费解,则尝试在较长时间内设置财富的顺序。

调整后的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