Results 1 to 2 of 2

Thread: C++

  1. #1
    Join Date
    Nov 2006
    Posts
    249

    C++

    Didn't really know where the best place to ask this was but thought someone here might be able to help me.

    I am trying to write a c++ program for a class that works like a magic 8-ball. We haven’t really learned very much and what I need to know is how to display a random string from a list of predefined strings. So when a person asks a question it will randomly pick from yes, no, etc...

    Thanks

  2. #2
    Join Date
    Nov 2006
    Posts
    249
    I got the random part workign fine but now my problem is if the user inputs more than 1 word it will answer multiple times. like if the question is 5 words long it will answer 5 times. does anyone know what i am doing wrong?

    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <time.h>
    #include <ctime>
    using namespace std;


    int main()
    {

    string words[5] = {"Yes", "No", "Maybe", "Try again later", "Go away"};

    string quest;

    int pos;

    while (quest != "0")
    {

    cout << "0 to Exit. Ask a yes or no question: ";
    cin >> quest;

    pos = rand() % 5;

    cout << words[pos] << endl; // Randomly prints word


    }

    }

Posting Permissions

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