2014-11-20 2 views
0

RPG 통계 코드에 약간의 문제가 있습니다. 나는 사람들이 최소한의 가치가 10 인 6 가지 기본적인 통계 (힘, 손재주, 헌법, 지혜, 지혜, 카리스마)를 사용하기를 원한다. 캐릭터를 만들 때 15 점을 사용하고 싶다. 코드를 사용하면 마지막 통계에 도달하기 전에 점수가 부족한 경우를 제외하고는 모두 잘 작동합니다. 15 점 모두를 강점으로 둡니다. 디스플레이에는 25 개의 힘, 32478493의 손재주, -42734627 헌법, 1 개의 지능 등이 있습니다 (정확한 숫자는 아니며 모양이 예일뿐입니다). 코드는 다음과 같습니다.RPG 통계 문제

CharCreate.h

#ifndef CharCreate_H 
#define CharCreate_H 
#include<fstream> 
#include<string> 

using namespace std; 

int charcreate(){ 

     fstream file; 

     char gender, choice; 
     string name, dummy; 
     int points; 
     int str, dex, con, intel, wis, cha; 
     float level; 
     double experience; 


     level = 1; 
     experience = 0; 

     ofstream myFile; 
     myFile.open ("T:\\character.txt"); 

     system("color 2"); 
     cout << "Welcome to the character creator." << endl; 
     genderchoice:cout << "First, are you male or female? (M/F)" << endl; 
     cin >> gender; 
     system("cls"); 

     if (gender == 'M' || gender == 'm'){ 
       cout << "You're male? (Y/N)" << endl; 
       cin >> choice; 
       system("cls"); 
          if (choice == 'Y' || choice == 'y'){ 
            cout << "Great!" << endl; 
            goto name; 
          } else if (choice == 'N' || choice == 'n'){ 
            goto genderchoice; 
          } 
     } else if (gender == 'F' || gender == 'f'){ 
       cout << "You're female? (Y/N)" << endl; 
       cin >> choice; 
       system("cls"); 
         if (choice == 'Y' || choice == 'y'){ 
            cout << "Great!" << endl; 
            goto name; 
          } else if (choice == 'N' || choice == 'n'){ 
            goto genderchoice; 
          } 
//------------------------------------------------------------------------------       
name:system("cls"); 
     system("color 3"); 
     cout << "What is your name, traveler?" <<endl; 
     getline(cin,dummy); 
     getline(cin, name); 
     cout << "" << endl; 
     cout << "Your name is " << name << "? (Y/N)" << endl; 
     cin >> choice; 

      if (choice == 'Y' || choice == 'y'){ 
        system("cls"); 
        cout << "Greetings, " << name << "!" << endl; 
      } else if (choice == 'N' || choice == 'n'){ 
        system("cls"); 
        cout << "You must provide your name, stranger." << endl; 
        goto name; 
      } 
//------------------------------------------------------------------------------ 
stats:system("cls"); 
     system("color 4"); 
     cout << "You have 6 stats to deal with in this game, and 15 points" << endl; 
     cout << "to allocate between them all." << endl; 
     cout << "These are: Strength (STR), Dexterity (DEX), Constitution (CON)," << endl; 
     cout << "Intelligence (INT), Wisdom (WIS), and Charisma (CHA)." << endl; 
     cout << "Continue: C" << endl; 
     cout << "Help: H" << endl; 
     cin >> choice; 

     if (choice == 'C' || choice == 'c'){ 

      attrib:points = 15; 

      str:cout << "You have 10 Strength. How many more points do you wish to add?" << endl; 
       cin >> str; 
       points = points - str; 
         if (str > points > 15){ 
           cout << "Not enough points!" << endl; 
           str = str - points; 
           goto str; 
         } else if (str == points){ 
           cout << "Are you sure you want to put all of your points here?" << endl; 
           cin >> choice; 
            if (choice == 'Y' || choice == 'y') { 
               goto fin; 
            } else if (choice == 'N' || choice == 'n'){ 
               goto str; 
         } else if (str < points){ 
           goto dex; 
         } 
       } 
       cout << "Remaining points: " << points; 
       cout << "" << endl; 
       str = str + 10; 
       cout << "You have " << str << " Strength" << endl; 
       system("pause"); 
       system("cls"); 

      dex:cout << "You have 10 Dexterity. How many more points do you wish to add?" << endl; 
       cin >> dex; 
       points = points - dex; 
         if (dex > points > 15){ 
           cout << "Not enough points!" << endl; 
           dex = dex - points; 
           goto dex; 
         } else if (dex == points){ 
           cout << "Are you sure you want to put all of your points here?" << endl; 
           cin >> choice; 
            if (choice == 'Y' || choice == 'y') { 
               goto fin; 
            } else if (choice == 'N' || choice == 'n'){ 
               goto dex; 
         } else if (dex < points){ 
           goto con; 
         } 
       } 
       cout << "Remaining points: " << points; 
       cout << "" << endl; 
       dex = dex + 10; 
       cout << "You have " << dex << " Dexterity" << endl; 
       system("pause"); 
       system("cls"); 

      con:cout << "You have 10 Constitution. How many more points do you wish to add?" << endl; 
       cin >> con; 
       points = points - con; 
         if (con > points > 15){ 
           cout << "Not enough points!" << endl; 
           con = con - points; 
           goto con; 
         } else if (con == points){ 
           cout << "Are you sure you want to put all of your points here?" << endl; 
           cin >> choice; 
            if (choice == 'Y' || choice == 'y') { 
               goto fin; 
            } else if (choice == 'N' || choice == 'n'){ 
               goto con; 
         } else if (con < points){ 
           goto intel; 
         } 
       } 
       cout << "Remaining points: " << points; 
       cout << "" << endl; 
       con = con + 10; 
       cout << "You have " << con << " Constitution" << endl; 
       system("pause"); 
       system("cls"); 

      intel:cout << "You have 10 Intelligence. How many more points do you wish to add?" << endl; 
       cin >> intel; 
       points = points - intel; 
        if (intel > points > 15){ 
           cout << "Not enough points!" << endl; 
           intel = intel - points; 
           goto intel; 
         } else if (intel == points){ 
           cout << "Are you sure you want to put all of your points here?" << endl; 
           cin >> choice; 
            if (choice == 'Y' || choice == 'y') { 
               goto fin; 
            } else if (choice == 'N' || choice == 'n'){ 
               goto intel; 
         } else if (intel < points){ 
           goto wis; 
         } 
        } 
       cout << "Remaining points: " << points; 
       cout << "" << endl; 
       intel = intel + 10; 
       cout << "You have " << intel << " Intelligence" << endl; 
       system("pause"); 
       system("cls"); 

      wis:cout << "You have 10 Wisdom. How many more points do you wish to add?" << endl; 
       cin >> wis; 
       points = points - wis; 
        if (wis > points > 15){ 
           cout << "Not enough points!" << endl; 
           wis = wis - points; 
           goto wis; 
         } else if (wis == points){ 
           cout << "Are you sure you want to put all of your points here?" << endl; 
           cin >> choice; 
            if (choice == 'Y' || choice == 'y') { 
               goto fin; 
            } else if (choice == 'N' || choice == 'n'){ 
               goto wis; 
         } else if (con < points){ 
           goto cha; 
         } 
        } 
       cout << "Remaining points: " << points; 
       cout << "" << endl; 
       wis = wis + 10; 
       cout << "You have " << wis << " Wisdom" << endl; 
       system("pause"); 
       system("cls"); 

      cha:cout << "You have 10 Charisma. How many more points do you wish to add?" << endl; 
       cin >> cha; 
       points = points - cha; 
        if (cha > points == 15){ 
           cout << "Not enough points!" << endl; 
           cha = cha - points; 
           goto cha; 
         } else if (cha == points){ 
           cout << "Are you sure you want to put all of your points here?" << endl; 
           cin >> choice; 
            if (choice == 'Y' || choice == 'y') { 
               goto fin; 
            } else if (choice == 'N' || choice == 'n'){ 
               goto cha; 
         } else if (con < points){ 
           goto fin; 
         } 
        } 
       cout << "Remaining points: " << points; 
       cout << "" << endl; 
       cha = cha + 10; 
       cout << "You have " << cha << " Charisma." << endl; 
       system("pause"); 
       system("cls"); 

      fin:cout << "Your stats are:" << endl; 
       cout << "Strength: " << str << endl; 
       cout << "Dexterity: " << dex << endl; 
       cout << "Constitution: " << con << endl; 
       cout << "Intelligence: " << intel << endl; 
       cout << "Wisdom: " << wis << endl; 
       cout << "Charisma: " << cha << endl; 
       cout << "Are these correct? (Y/N)" << endl; 
       cin >> choice; 
       cout << "" << endl; 

        if (choice == 'Y' || choice == 'y'){ 
        cout << "Congratulations, you have successfully finished your character." << endl; 
        } else if (choice == 'N' || choice == 'n') 
        goto attrib; 
        } 
     } else if (choice == 'H' || choice == 'h'){ 
      cout << "Strength is how easily you can crush a tomato." << endl; 
      cout << "Dexterity is how easily you can handle a tomato with your hands." << endl; 
      cout << "Constitution is how easily you can eat a bad tomato." << endl; 
      cout << "Intelligence is knowing that tomato is a fruit." << endl; 
      cout << "Wisdom is not putting tomato in a fruit salad." << endl; 
      cout << "Charisma is selling a tomato-based fruit salad." << endl; 
      system("pause"); 
      goto stats; 
     } 

      myFile << "Name: " << name << "\n"; 
      myFile << "Gender: " << gender << "\n"; 
      myFile << "\n"; 
      myFile << "Level: " << level << "\n"; 
      myFile << "Experience: " << experience << "\n"; 
      myFile << "\n"; 
      myFile << "Strength: " << str << "\n"; 
      myFile << "Dexterity: " << dex << "\n"; 
      myFile << "Constitution: " << con << "\n"; 
      myFile << "Intelligence: " << intel << "\n"; 
      myFile << "Wisdom: " << wis << "\n"; 
      myFile << "Charisma: " << cha << "\n"; 
    myFile.close();  
}    
#endif 

#include <cstdlib> 
#include <iostream> 
#include "CharCreate.h" 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
    charcreate(); 
    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

가 어떻게 포인트에서 실행 한 후 엉망이가는 숫자의 문제를 해결 할 MAIN.CPP? 도움이된다면 저는 Bloodshed Dev C++를 컴파일러로 사용하고 있습니다. 왜냐하면 그것이 대학에서 사용해야하기 때문입니다.

+2

변수를 10으로 초기화하려고 시도하십시오. – nurettin

답변

1

C++에서 값을 변수로 설정하지 않으면 그 당시의 임의의 데이터가 포함됩니다. 시작시 각 통계를 10 (또는 정상적인 기본값)으로 초기화하려고합니다.

+0

임의 값은 스택의 변수에서 찾을 수 있으며 해당 변수는 기본 유형 (생성자 없음) 일 가능성이 큽니다. 그렇지 않으면 정적 및 전역 기본 변수는 기본적으로 모두 0이며 생성자가있는 변수는 일반적으로 컨텍스트에 관계없이 기본적으로 초기화됩니다. – AlexanderVX