samedi 5 décembre 2015

srand(time(NULL)) c++ isnt working properly [duplicate]

This question already has an answer here:

So I'm working on a program in which you input how many words there should be(variable a is how many words at least there should be and b is how many words most there can be) and output is sentence. Sentence must satisfy this criteria: 1.Every word must have at least 1 letter and at most 15 letters. 2.Sentence must have at least b / 2 words. So this is my code:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <time.h>

using namespace std;

int a, b, duljina, n;
char slovo;
string recenica, rijec;

int main() {
    srand(time(NULL));
    cin >>a >>b;

   if(b / 2 < a) duljina = a;
   else duljina = b / 2;

    for(int i = 0; i < duljina; i++) {
        srand(time(NULL));
        for(int j = 0; j < 5; j++) {
            srand(time(NULL));
            n = rand() % 26 + 1;
            slovo = n + 'a';
            recenica.push_back(slovo);
        }
        recenica.push_back(' ');
    }

    cout <<recenica <<endl;

    return 0;
}

and this is the output when the input is as example 2 7:

wwwww wwwww wwwww wwwww

So my problem is srand(time(NULL)) doesn't work in these for loops. If any of you guys can help me I would be very thankful. P.S.SORRY4MYBADENGLISH

Domagoj




Aucun commentaire:

Enregistrer un commentaire