I have been working on C++ by myself for a few weeks trying to learn it as best as I can, with a crappy little book I bought at Barns & Noble. I have learned a few things so I decided to make a program to show off what I have learned, a Magic 8 ball program. It would show an example of what I learned except one thing the book didn't teach me, Random functions or how to make one. I really could use some help with my 8 ball function or if anyone could please, make a random function/library for me that uses something like byonds. Rand(int a, int b);. I would be oh so gratefull if someone could help me with this. Alas, my book doesn't teach me anything about RAND_MAX and the formula I stole from some web site. Anyway also if you could fix my code that would be helpfull plus the rand function. It might also be helpfull to know that my rand line of code always returns 2 for some reason...so it always displays Answer[2]
#include <iostream> //Built in libs
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std; //So we don't have to type std::
int pause; //Declare pause as a variable
int main()
{
int j; //Declare a local variable
const int N=5; //Declare a constant variable
string disregardedQuestion; //Declares a variable you can type into as text
string answer[5]={ //Declares a string array
"Yes",
"No",
"Not a chance",
"Could be",
"Maybe"};
for(int j; j<25; j++){ // I stole this off the website, I would think it would have to be J<5, typo?
rand(); //Call rand if J is less than 25
}
cout << "Enter your question\n";
cin >> disregardedQuestion; //Asks for a value for Disregarded question
j = (int) N * rand() / (RAND_MAX + 1.0); // I can barely understand this, I stole this from a web site.
cout << answer[j]; //Display the output of J in the array, which should be 0-4 or 1-5 I hate the counts from 0 thing!
cout << "\n\n The program has ended, press any key to continue.\n";
cin >> pause;
return 0;
}
rand(1,100)//Use this to randomly pick a number between 1 and 100
There you go, if you need any more help just use the Resources to the left
<----