Q:

I can't find the error on my program (C++)

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

void divider();

int main()
{
    int cstats, choice;
    int rhp, hp, atk, def, matk, mdef, dmg, mdmg, agi, magi;
    divider();
    cout << "Kill the Zombie! (Text-Based Game)\n";
    divider();
    cout << "Please choose your specialty: ";
    cout << "[1] Offense [2] Magic [3] Defense [4] Speed\n";
    do{cin >> cstats; }
    while(cstats > 4 || cstats < 1);
    {
    switch(cstats)
    {
    case 1:
        atk = 15;
        def = 8;
        agi = 6;
        matk = 5;
        mdef = 5;
        magi = 5;
        break;
    case 2:
        atk = 5;
        def = 5;
        agi = 5;
        matk = 15;
        mdef = 7;
        magi = 6;
        break;
    case 3:
        atk = 7;
        def = 15;
        agi = 5;
        matk = 1;
        mdef = 14;
        magi = 3;
        break;
    case 4:
        atk = 7;
        def = 4;
        agi = 15;
        matk = 3;
        mdef = 4;
        magi = 14;
        break;
    }
    if(cstats == 1)
    {
    cout << "You have chosen Offense\n";
    }
    if(cstats == 2)
    {
    cout << "You have chosen Magic\n";
    }
    if(cstats == 3)
    {
    cout << "You have chosen Defense\n";
    }
    if(cstats == 4)
    {
    cout << "You have chosen Speed\n";
    }
    }
    srand((unsigned)time(0));
    rhp = rand()%500 + 200;
    hp = rand()%500 + 200;
    while(hp > 0 || rhp > 0 )
    {
    cout << "What do you want to do? [1] Normal Attack [2] Magic Attack [3] Defend [4] Dodge\n";
    do{cin >> choice; }
    while(choice > 4 || choice < 1);
    {
    switch(choice)
    {
        case 1:
            atk = rand()%20+10;
            break;
        case 2:
            matk = rand()%20+10;
            break;
        case 3:
            atk = rand()%5+1;
            def = rand()%10+10;
            break;
        case 4:
            agi = rand()%10+10;
            break;
    }
    if(choice == 1)
    {
    mdmg = ((0.10 * def) / (1 + 0.10 * def)) * 100;
    }
    if(choice == 2)
    {
    mdmg = ((0.20 * mdef) / (1 + 0.20 * mdef)) * 100;
    }
    if(choice == 3)
    {
    dmg = ((0.06 * def) / (1 + 0.06 * def)) * 100;
    }
    if(choice == 4)
    {
    mdmg = ((0.10 * agi) / (1 + 0.20 * agi)) * 100;
    }
    rhp = rhp - mdmg;
    cout << "You did " << mdmg << "damage to the zombie!\n";
    cin.get();
    if(rhp <1)
    {
        cout << "You killed the Zombie! Congratulations, You won with " << hp << "hp left.\n";
        cin.get();
        system("pause>0");
        return 0;
    }
    cout << "The Zombie now has " << rhp << "hp left.\n";
    dmg = ((0.06 * def) / (1 + 0.06 * def)) * 100;
    dmg = ((0.10 * mdef) / (1 + 0.10 * mdef)) * 100;
    dmg = ((0.05 * agi) / (1 + 0.05 * agi)) * 100;
    if(dmg < 0)
    {
        dmg = 0;
    }
    hp = hp - dmg;
    cout << "The Zombie hit you for " << dmg << " damage.\n";
    if(hp < 1 )
    {
        cout << "You died. The Zombie still has " << rhp << "hp left.\n";
        cin.get();
        system("pause>0");
        return 0;
    }
    cout << "You now have " << hp << " hp left.\n";
}
}
}
void divider()
{
    cout << "*************************************\n";
}

This is the whole code of my program and I always get an error saying "Run-Time Check Failure #3 - The variable 'mdmg' is being used without being initialized." Where did I go wrong? I can't find it. I need to finish this in 3 hours. :(

A:

I'm guessing your issue is here:
if(choice == 3)
{
dmg = ((0.06 * def) / (1 + 0.06 * def)) * 100;
    rhp = rhp - mdmg;
}

If you call that before any other choices, mdmg won't be initialized before being subtracted from rhp.
EDIT:
Although taking the second statement out is a good idea, you still have the same issue that mdmg is not initialized in the if(choice ==3) block.  Did you mean to write 
if(choice == 3)
{
    mdmg = ((0.06 * def) / (1 + 0.06 * def)) * 100;
}

(dmg is changed to mdmg)

