Question

I've only recently gotten into C++ and I plan on making games mostly. Naturally I decided to play around and see what I could do.

#include <iostream>
#include <ctime>
#include <cstdlib> 
using namespace std;

int main ()
{ 
    int HIT, DMG, HRT, ATK, DEF, EVS, EVS1, CRT, CRT1, CRT2, CRTMUL, CRTMUL1, BK, BKDMG;

    srand( time(0));

    HIT=rand()%100+1;

    cout<<"Please enter thier Evasion: ";
    cin >> EVS;

    EVS1 = 100 - EVS;

    if ( HIT < EVS1 ) {
        cout<<"Hit!";
        cout << '\n';
    }

    else if ( HIT > EVS1 ) {
        cout<<"Miss!";
        return 0;
    }

    cout<<"Please enter your Damage: ";
    cin >> DMG;

    cout<<"Please enter your Attack: ";
    cin >> ATK;

    cout<<"Please enter thier Defence: ";
    cin >> DEF;

    cout<<"Please enter your Crit Chance: ";
    cin >> CRT;

    cout<<"Please enter your Crit Multiplier: ";
    cin >> CRTMUL1;


    CRT1=rand()%100+1;

    CRT2 = 100 - CRT;

    if ( CRT1 < CRT2 ) {
        cout<<"You didnt crit.";
        cout << '\n';
    CRTMUL = 1;
    }

    else if ( CRT1 > CRT2 ) {
        cout<<"Crit!";
        cout << '\n';
        CRTMUL = CRTMUL1;
    }   


    // no matter what you input here,...
    cout<<"From where did you hit them? ";
    cout << '\n';
    cout<<"(1 for from the back, 2 for from the side, 3 for from the front).";
    cout << '\n';
    cin >> BK;
    // ...this area...
    if ( BK = 1 ) {
        BKDMG = 1.6;
    }

    else if ( BK = 2 ) {
        BKDMG = 1.3;
    }

    else if ( BK = 3 ) {
        BKDMG = 1;
    }
    // ... to this area wont work, in the equation below BKDMG is allways 1
    HRT =  ((((((ATK/5)/100)+1)*(DMG))-(((((ATK/5)/100)+1)*(DMG))/100)*(DEF/5))*BKDMG)*CRTMUL;

    cout<<"You hit for ";
    cout<<HRT;
    cout<<" damage!";

    return 0;
}

As you can see within the code, no matter what you input for BK it seems that BKDMG will all ways result in 1. I believe this is because of rounding? If not let me know.

If so how do I fix this problem? I think the answer is somewhere on here but I don't know exactly what to search as I don't know exactly what the problem is. From what I can understand float could help me? I don't understand what a float is as this is the first thing I have coded.

Était-ce utile?

La solution

You've defined BKDMG to be of type int meaning that it can only hold integers. So, yes, if you assign it a real-number value it will round it down to the next integer value. Using double or float would probably suffice for this example.

Your conditions are also wrong:

if ( BK = 1 ) {
    BKDMG = 1.6;
}

Because 'BK = 1' is an assignment, it always sets the value of BK to 1. The code snippet above should be:

if ( BK == 1 ) {
    BKDMG = 1.6;
}

Autres conseils

if ( BK = 1 ) 

Is a classic (beginners) mistake. You are not comparing, you are assigning.

It should be

if ( BK == 1 ) 

Or even better,

if ( 1 == BK) 

If you put the constant first, it is impossible to make the error.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top