Results 1 to 2 of 2

Thread: Magic 8ball in c++

  1. #1
    Join Date
    Apr 2006

    Magic 8ball in c++

    I got bored and just wanted to make a magic 8ball program in c++. It simple yet fun, but I noticed a problem.
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    	string question;
    	string yn;
    	int srand(0);
    	string again;
    	char* answer[] =
    	 "As I See It, Yes\n",
    	 "Ask Again Later\n",
    	 "Better Not Tell You Now\n",
    	 "I don't Want to Tell You\n",
    	 "Concentrate And Ask Again\n",
    	 "Don't Count On It\n",
    	 "It Is Certain\n",
    	 "It is Decidedly So\n",
    	 "Most Likely\n",
    	 "My Reply Is No\n",
    	 "My Sources Say No\n",
    	 "Outlook Good\n",
    	 "Outlook Not So Good\n",
    	 "Reply Hazy, Try Again\n",
    	 "Signs Point To Yes\n",
    	 "Very Doubtful\n",
    	 "Without A Doubt\n",
    	 "Yes Definately\n",
    	 "You May Rely On It\n"
    //	for(int i=0;i<20;i++)
    //	{cout << answer[i];}
     do {
      cout << "Ask Your Question: ";
      getline(cin, question);
      cout << "Your Question was:\n" << question <<endl;
      cout << "Are You Ready For My Answer?(y/n): ";
      cin >> yn;
      if (yn=="y")
    	cout << answer[rand()%20]<< endl;
      cout << "Ask Again?(y/n): ";
      cin >> again;
    return 0;
    When I save and run this in Visual Studio, everything works fine and it randomly picks an answer. However when I compile and run this on my Slackware partition, it picks the same answer everytime.
    Also when it executes through and asks if it should be run again, when I type in 'y' it skips the question input and goes to the "Are you ready to see my answer" part. I think it has something to do with getline()....I am not in any hurry to figure this out, more just to satisfy my curiosity, so any help would be appreciated.

  2. #2
    Senior Member
    Join Date
    Jul 2001
    Hi Phalse,

    In some implementations of Rand you need to seed it with a dynamic number to change the value. I would use a time function (something like the number of seconds since 1970). Its been awhile since I've used rand but that is away to ensure it is alsways different.

    If you spend more on coffee than on IT security, you will be hacked. What\'s more, you deserve to be hacked.
    -- former White House cybersecurity adviser Richard Clarke

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts