jeudi 4 août 2016

Why doesn't rand() work in this code?

I can build the code perfectly fine, but when I run it returns

A wild grunt has attacked you! And 1

I tried to give NameRand a random value, and if it turned out to be 1, I'd give Name the string ogre. So why does NameRand only have a value of 1 but Name has the string grunt?

Code:

#include <stdlib.h>
#include <iostream>
#include <time.h>

using namespace std;

int main(){



string Name;

int NameRand;
srand ( time(NULL) );

NameRand = rand() % 3 + 1;

        if(NameRand = 1)
                Name = "ogre";

        else if(NameRand = 2)
                Name = "direwolf";

        else;
                Name = "grunt";



cout <<  "A wild " << Name << " has attacked you! And " << NameRand;


return 0;
}




Aucun commentaire:

Enregistrer un commentaire