How to rand produce a c output.

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
using namespace std;




char randn(char letters) //random definition
{
    return rand()%letters;
}

int main()
{
    cout << "Welcome I am the magic 8 ball, because Adam writes basic programs."
    << endl;

    cout << "Enter your question and I will answer it to the best of my poor abilities (Yes or no questions only)"
    << endl;
    
    
    string question;
    cin >>  question;

    
    string PossibleAnswers = "no", "yes", "maybe";
    
    cout << rand(PossibleAnswers);
    

    return 0;
}


(Note: I know I shouldn't be using namespace std as it is bad practice but only
I am using it on my test progs for shorter syntax)

Hey guys Im brand new to c++ been learning for about 3 weeks now. What I am trying to do is build a magic 8 ball that quips back random predictions of the future if you ask it a questions like "Will I win my march madness bracket?"
Anyway, I am trying to create a random out put to terminal. Not sure if I should be using cout or printf etc.
I know my code is off, probably should be using an array in some way. Help would be greatly appreciated!
Not sure if I should be using cout or printf
std::cout for C++, printf() for C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>     // std::cout
#include <algorithm>    // std::shuffle
#include <random>       // std::default_random_engine
#include <chrono>       // std::chrono::system_clock
#include <string>
#include <vector>

int main () {

    std::vector<std::string> answers {"yes", "no", "maybe"};

    std::cout << "Enter question(Yes / no / maybe answers only)" << "\n";

    std::string question{};
    getline(std::cin, question);//use getline() for string, cin trips on whitespace

    auto seed = std::chrono::system_clock::now().time_since_epoch().count();

    std::shuffle (answers.begin(), answers.end(), std::default_random_engine(seed));
    //http://www.cplusplus.com/reference/algorithm/shuffle/

    std::cout << answers[0];

}
Awesome thank you so much gunnner!
Topic archived. No new replies allowed.