This question already has an answer here:
I am working on a school project where I need to create a fortune cookie program. The basics of the program is that it create a random number which is then assigned to a "fortune" in an array that will print out on the screen. I need help on getting the random number that is generated to print out the pre-defined string in an array.
Also to make it look fancy I'd like to make a for loop that will create a border around each fortune, since they are different lengths, so it looks cohesive and will look better than having to do it manually.
Any assistance will be helpful
Here's the code:
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;
}
Aucun commentaire:
Enregistrer un commentaire