Jump to content

Not Giving Up But Need Help


Archerzz

Recommended Posts

I am trying to make a type of Fortune Teller program. But I dont know how to get the program to randomly choose one of the fortunes from the string.

 

#include <iostream>
#include <string>
using namespace std;

int main()
{ 
   int number;
   string fortunes[4] = ("Only Death Awaits you", "Your 1337ness is a inspiration", "On the binary finger shirt everyone is giving you 00000100", "Watch out for that car");
   
   cout << "Press any button to reveal your fortune....(wooo mystical)\n\n";
   system ("pause");
   
   cout << "\n";
   cout << "Your Fortune is.....\n\t";
   
   number = rand()%4;
   
   cout << fortunes[number];
   
   return 0;
   
}

Share this post


Link to post
Share on other sites

Umm...

 

string[4] should be string[3].

 

Then put the string variables into the appropriate array spaces, starting at 0.

 

Btw, try rand() % 4 +1;.

514515[/snapback]

  • string should be [4] like he has, not [3]. [4] still means make 4 spaces but from 0 - 3, not 1 - 4.
  • You seem to atleast half understand, obviously, as you've yourself said starting at 0. :)
  • rand() % 4 + 1 again would result in 1 - 4, not appropriate for addressing an the array for the reason stated in point #1.

:) Not trying to be mean so don't feel bad there. I myself (yes, ME) have made similar mistakes to those before! It's just the whole learn from experience thing :)

 

KB is spot on with having to seed rand() before use. Using srand(time(NULL)); is a simple way to do such a thing.. and you'll want to do that before you call rand()

Share this post


Link to post
Share on other sites

when I compiled it, it ran fine without using srand(time(NULL)); (and what does typing that actully accomplish).

514696[/snapback]

 

It makes your random numbers actually random.

 

If you just use a plain old random number with no time seed, you'll always get the same series. I have not tested this in windows C++, but I have in VB6. In VB, if you just use a regular random number call, the program will generate the same exact "random numbers" in the same exact order every time. That's not exactly random :)

 

When your program generates a "random" number, it actually uses a seed. You can "seed" your random numbers manually. It's pretty tricky to understand, but the concept is to "seed" your random number generation with the clock time (like time of day), because that way you'll get truely varying numbers. The only way you'd get the same random numbers twice would be if you ran the program at the EXACT same time of day. Does that make sense?

Share this post


Link to post
Share on other sites

now by exact time you mean down to the second or minute because if I run this 5 times a minute I would like varying responses.

514715[/snapback]

 

No, it's MUCH more precise than that. I don't know how specific exactly, and it may even vary between OS's, but it's safe to say that it's precise enough that you wouldn't have to worry about getting the same numbers.

Share this post


Link to post
Share on other sites

Ok I did that, but my program still prints out the 4th phrase in the string

 

Here is what I have.

#include <iostream>
#include <string>
using namespace std;

int main()
{ 
   srand(time(NULL));
   string fortunes[4] = ("Only Death Awaits you", "Your 1337ness is a inspiration", "On the binary finger shirt everyone is giving you 00000100", "Watch out for that car");
   
   cout << "Press any button to reveal your fortune....(wooo mystical)\n\n";
   system ("pause");
   
   cout << "\n";
   cout << "Your Fortune is.....\n\t";
   
   int number = (rand() % 4);
   
   cout << fortunes[number];
   
   return 0;
   
}

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...