Random Phrase Generator Issues

I'm having trouble understanding whats going wrong here. Instead of outputting one random phrase, like it seems it should, my program outputs all four of them.


#include "stdafx.h"
#include "iostream"
#include "cstdlib"
#include "ctime"

using namespace std;
int ending;
int eattack;
int efinal;

int main()
{
srand(static_cast<unsigned int>(time(0)));

eattack =rand();

efinal = (eattack % 2) + 1;

if (efinal=1)
{
cout <<"Enemy uses fire blast! You feel your skin crackle and burn!"<<endl;
}

if (efinal=2)
{
cout <<"Enemy uses spinning kick! Your chest erupts with pain as your sternum shatters!"<<endl;
}

if (efinal=3)
{
cout <<"Enemy uses whirlwind punch! The world dims as your brain begins to swell!"<<endl;
}

if (efinal=4)
{
cout <<"Enemy uses sandstorm! Your insides burn as your lungs drown in sand!"<<endl;
}

cin>>ending;
}
if (efinal=1)
ahould be
if (efinal==1)

A single = is the assignment operator. You need == which is the test for equality.
Consider using a look up table instead of multiple if statements.

1
2
3
4
5
6
7
8
9
10
11
12
std::srand( std::time(0) ) ;

const char* const messages[] =
{
    "Enemy uses fire blast! You feel your skin crackle and burn!",
    "Enemy uses spinning kick! Your chest erupts with pain as your sternum shatters!",
    "Enemy uses whirlwind punch! The world dims as your brain begins to swell!",
    "Enemy uses sandstorm! Your insides burn as your lungs drown in sand!"
};
enum { NMESSAGES = sizeof(messages) / sizeof(messages[0]) } ;

std::cout << messages[ std::rand() % NMESSAGES ] << '\n' ;
Topic archived. No new replies allowed.