Hello,

Yes, I am a new C++ student and yes I have tried looking for this topic already. I'm very interested to know if there is a way to create a text array that would produce random output akin to a magic 8 ball?

Here's what I want to do...

I would to create a program that combines user input with stored words or sentence fragments to produce a randomly generated excuse.

However, I'm not sure where to start with this.
How can I take the examples I have in parenthesis and make them random output? What do I need to do to accomplish this?

Yes you could use rand in time.h to create randomness and say if it is a range from a certain number produced by rand it would output one sentence then another if it equaled something else.

Can you explain in more more detail? Where can I find examples of this used in the manner you are describing?

I'm not really an expert with time.h it would probably be a good idea to read up on it. But the basics is that the function "rand" makes things random which is what a magic eight ball does theoretically then if you got a certain number you would make it cout your statement.

Thanks! I had a breakthrough a few hours ago and will try to update the latest version of my program sometime tomorrow. Now that I got the main points down. I may try to convert this into a text box type of format.

>you could use rand in time.h
rand is declared in stdlib.h, or for C++, cstdlib.

>Where can I find examples of this used in the manner you are describing?

#include <cstdlib>
#include <ctime>
#include <iostream>

// Trick for getting the number of
// items in an array of T using sizeof
template <typename T, int N>
char (&array(T(&)[N]))[N];

int main()
{
  const char *messages[] = {
    "I'm a little teapot",
    "This is a message",
    "If you can read this, you're too close",
    "Han shot first",
    "What are you looking at?"
  };

  // Seed rand so that each run of the program is different
  std::srand ( (unsigned)std::time ( 0 ) );

  for ( int i = 0; i < 10; i++ ) {
    // Get a random number from 0 to the number
    // of items in the list of messages
    int r = std::rand() % sizeof array ( messages );

    std::cout<< messages[r] <<'\n';
  }
}

Also, make sure if you do not want the same out put everytime you use the srand() function. rand in itself is really useful, but it produces the same "random" numbers everytime unless you use srand() to begin random. If you do use srand, you will have to use time.h. It uses the clock on the computer to dictate the random number.

>If you do use srand, you will have to use time.h.
You don't have to use time to seed rand, it's just the most common solution. It's not even a very good solution, but it's easy, so everyone uses it.

As you can see, I have gone in a different direction from my earlier post...

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define MAX_RANGE 10
using namespace std;
//Random Excuse Generator Version 2.0.1

int main()
{
    int value;
  
    srand ( time (NULL) );         // Initialize random number generator
    value = rand()%MAX_RANGE+1;

	      if (value == 1)
{
 cout << "\nMy program has been subpoenaed as evidence.  That's all I can say.\n\n\n\n";
 
}

if (value == 2)
{
 cout <<"\nThe crew from that Extreme Home Makeover show tore down my house by mistake.\n\n\n\n";
 
}

if (value == 3)
{
 cout <<"\nMy kid fell asleep next to wet cement in our backyard.\n His foot fell in and we can't get it out.\n\n\n\n";
 
}

if (value == 4)
{
 cout<<"\nMy dog re-wrote my program and now he won't tell me where he buried it in the back yard!\n\n\n\n";
 
}

if (value == 5)
{
 cout<<"\nWas that TODAY?  I didn't think you were serious about it NOT being optional.\n\n\n\n";
 
}

if (value == 6)
{
 cout<<"\nThe team is in a slump right now and they really need me.  \nNevermind that I'm not actually on the team per se.\n\n\n\n";
 
}

if (value == 7)
{
 cout<<"\nIt works on MY machine?\n\n\n\n";

}

if (value == 8)
{
 cout<<"\nThere is something funky in your data.\n\n\n\n";
 
}

if (value == 9)
{
 cout << "\nIt must be a hardware problem.\n\n\n\n";
 
}

if (value == 10)
{
 cout << "\nI made the error in judgement of asking Ben Affleck if he could get me Matt Damon's autograph,\nthen the next thing I remember is Ben trying to stab me with a swordfish.\n\n\n\n";
 
}


		
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.