2013-10-24 2 views
-2
#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"; 
} 

이것은 내 프로그램의 전체 코드이며 "런타임 검사 오류 # 3 - 변수 'mdmg'가 초기화되지 않고 사용되었습니다. " 나는 어디로 잘못 갔는가? 나는 그것을 발견 할 수 없다. 3 시간 안에 끝내야 해. :(내 프로그램에서 오류를 찾을 수 없습니다. (C++)

+4

당신이 mdmg 대신에 dmg를 입력 한 당신의 선택 == 3 분기에서 오타처럼 보입니다. 또한 동일한 변수를 검사하는 if 문의 순서에 대한 대안으로 switch 문을 조사하십시오. – godel9

+0

디버거에 익숙해지기. –

+0

오류 메시지가 명확합니다. if-else 문 앞에 'mgmg'를 초기화하십시오. – user2784234

답변

4

나는이 문제를 추측하고있어이 여기에 있습니다 :

if(choice == 3) 
{ 
dmg = ((0.06 * def)/(1 + 0.06 * def)) * 100; 
    rhp = rhp - mdmg; 
} 

당신이 어떤 다른 선택하기 전에, mdmg가 RHP에서 차감되기 전에 초기화되지 않습니다 호출하는 경우

편집 :.

두 번째 문을 꺼내는 것도 좋은 생각이지만 여전히 if(choice ==3) 블록에서 mdmg가 초기화되지 않은 것과 동일한 문제가 있습니다.

if(choice == 3) 
{ 
    mdmg = ((0.06 * def)/(1 + 0.06 * def)) * 100; 
} 

(데미지가 mdmg로 변경)

+0

지금 변경했습니다. 그러나 여전히 오류가 있습니다. if (choice == 3) \t { \t dmg = ((0.06 * def)/(1 + 0.06 * def)) * 100; \t} \t 경우 (선택 == 4) { \t \t mdmg = ((0.10 * AGI)/(1 + 0.20 * AGI)) * 100; \t} \t rhp = rhp-mdmg; –

+0

주변의 코드를 보면'dmg'을'mdmg'로 대체해야합니다. – Dialecticus

+0

문제는 여기에 있습니다 :'cout << "당신은"<< mdmg << "좀비에게 피해를 입혔습니다! \ n"; "if (또는 switch 문) 밖. mdmg 값은 분기 중 하나에서 초기화되지 않은 경우 초기화되지 않은 상태로 유지됩니다. – user2784234

0

문제는 오류가 알려줍니다로 초기화하지 않고 mdmg를 사용하려고합니다.

cout << "You did " << mdmg << "damage to the zombie!\n";

당신은 당신의 다양한 if 제표에 mdmg에 값을 할당하지만, mdmg에 대한 기본 값이 아니므로 (당신이 mdmg에 값을 할당하는 if 블록 중 하나를 입력 할 수있는 기회가있다), 인쇄를 시도 할 때 오류가 발생합니다.

선언 할 때 (int mdmg = 0;) 초기화하거나 if 문 중 하나에없는 다른 수단을 사용하여 기본값을 할당하십시오.

관련 문제