PDA

View Full Version : C++



Magiccupcake
07 May 2007, 04:13 PM
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

Magiccupcake
09 May 2007, 12:06 AM
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


}

}