2010-07-02 3 views
0

나는 암소 변수를 만드는 함수를 만들었고, 나중에 나는 그들을 도살 할 다른 함수를 만들었습니다. 두 함수는 클래스의 메서드입니다. 나는 암소 만들기 함수를 사용할 때 작동하며 암소의 가치를 얻습니다. 그러나 정육점 기능에 액세스 할 때 소의 가치는 0입니다.int 함수가이 함수 내에서 작동하지 않는 이유는 무엇입니까?

두 가지 변수가 있습니다. 함수가 void 인 경우 함수가 지역 변수를 가질 수 있다고 생각하지 않았습니다.

어떻게 해결할 수 있습니까?

#include <string> 
using namespace std; 

class Cheif 
{ 
    int points; 
    string name; 

protected: 
    int NoodleS; 
    int RosemaryS; 
    int ParcleyS; 
    int PepperS; 
    int Field; 
    int Water; 

public: 
    int star; 
    int foodPrep; 
    int ingrPrep;  
    int endOfPrep; 
    int money; 
    int ingr1; 
    int ingr2; 
    int ingr3; 
    int Noodle; 
    int Rosemary; 
    int Parcley; 
    int Pepper; 
    int chickMeat; 
    int beef; 
    int chickens; 
    int cows; 

// constructor 
    Cheif() 
    { 
     money = 1000; 
     points = 0; 
     star = 10;  
     endOfPrep = 0; 
     ingr1 = 0; 
     ingr2 = 0; 
     ingr3 = 0; 
     Noodle = 0; 
     Rosemary = 0; 
     Parcley = 0; 
     Pepper= 0 ; 
     cows = 0; 
     chickens = 0; 
     beef = 0; 
     chickMeat = 0; 
    } 

// method 

////////////////////////////////////////////////// 
//            // 
//   ask user for their name   // 
//            // 
////////////////////////////////////////////////// 
    void FindName() 
    { 

     cout << "what is your name? " << endl;; 
     cin >> name; 
     cout << endl << " Welcome " << name 
     << " let us begin... " <<endl; 


    } 

////////////////////////////////////////////////// 
//      END      // 
////////////////////////////////////////////////// 

////////////////////////////////////////////////// 
//            // 
//   Buy animal live stock    // 
//            // 
////////////////////////////////////////////////// 
    void Buy_Cow() 
    { 
     if(money > 199) 
     { 
     money -= 200; 
     cows += 1; 
     cout <<" you now have " << cows << " cows" << endl; 
     } 
    } 

    void Buy_Chick() 
    { 
     if(money > 99) 
     { 
     money -= 200; 
     chickens += 1; 
     } 
    } 

////////////////////////////////////////////////// 
//      END      // 
////////////////////////////////////////////////// 


////////////////////////////////////////////////// 
//            // 
//   if user goes to open store   // 
//            // 
////////////////////////////////////////////////// 

    void GOTO_Store() 
    { 
     // while food is not prepared yet 
     while(endOfPrep == 0){ 
     PREPAREMEAL: 
     // show menu 
     cout << "<1> Make Food \n" 
      << "<2> Collect Ingridents \n"   
      << "<3> Butcher Animal \n" 
      << "<4> Go Back... \n" << endl; 


     // create variable to hold choice 
     string OS_Choice; 

     cin >> OS_Choice; 

     /////////// if user decides to "make food" ///////////// 
     if (OS_Choice == "1") 
     { 
      if(foodPrep == 1) 
      { 
      cout << "you've already prepared the meal..." << endl;   
      }else{   
      if(ingr1 <= 0 ||ingr2 <= 0 || ingr3 <= 0) 
      { 
       goto PREPAREMEAL; 
      }else{   
       cout << "your using" << ingr1 << " " << ingr2<< " " << ingr3 << endl; 
      } // end of ingredient check 

      cout << " and how shall this mean be prepared? " << endl; 

      int prepMethod; 

      cout << "<1> Baked \n " 
       << "<2> boiled \n " 
       << "<3> Fried \n " 
       << "<4> mixed \n "; 

      cin >> prepMethod; 

      cout << " And what kind of sides would you like to add? " << endl; 

      int sideChoice; 

      cout << "<1> bread Roll \n " 
       << "<2> Rice \n " 
       << "<3> Beans \n" << endl; 

      cin >> sideChoice; 

      foodPrep = 1; 
      }// end of food choice 

      //begin food compare. 
      /////////// if user decides to get ingrediants ///////////// 
     }else if(OS_Choice == "2"){ 
      if (ingrPrep == 1){ 
      cout << " you have already collected the ingredients " << endl; 

      }else{  
      cout << "what 3 ingridents will you use? " << endl; 
      cin >> ingr1; 
      cin >> ingr2; 
      cin >> ingr3; 
      }// end of ingrident prep 
      /////////// if user decides to get ingrediants ///////////// 
     }else if(OS_Choice == "3") 
     { 
      cout << " you have " << cows << " cows \n " 
      << " you have " << chickens << " Chickens \n "; 

      cout << "what would you like to butcher? " << endl; 
      cout << "<1> Cows : " << cows << endl; 
      cout << "<2> Chicken : " << chickens << endl; 
      int B_Choice; 

      cin >> B_Choice; 

      if(B_Choice == 1){ 
      if(cows == 0){ 
       cout << " sorry you dont have any cows " << endl; 
      }else{ 
       cows = cows - 1; 
       beef = beef + 5; 
       cout << " you now have " << beef << "peices of cow meat" << endl; 
       ingrPrep = 1; 
      }//end of cow check 
      }else if(B_Choice == 2){ 
      if(chickens == 0){ 
       cout << " sorry you dont have any chickens " << endl; 
      }else{ 
       chickens = chickens - 1; 
       chickMeat = chickMeat + 2; 
       cout << " you now have " << chickMeat << "peices of Chicken meat" << endl; 
       ingrPrep = 1; 
      } // end chicken Check 
      }else { 
      cout << "invalid Choice" << endl; 
      }// end of b choice 
     }else if(OS_Choice == "4") { 
      endOfPrep = 1;  
      foodPrep = 0; 
      ingrPrep = 0; 
      ingr1 = 0; 
      ingr2 = 0; 
      ingr3 = 0; 
     }// end of ingr prep. 
     }//end of while loop 
    } 
////////////////////////////////////////////////// 
//      END      // 
////////////////////////////////////////////////// 
}; 
+4

포스트 실제, 컴파일 가능한가는 , 코드. –

+1

일관되게 들여 쓰기가되면 해치지 않을 것입니다. –

+0

예, 클래스의 실제 코드를 지정하면 도움이됩니다. –

답변

1

설명하는 문제의 원인이되는 클래스 구성에있어 명백한 문제는 없습니다. (unwind point는 보통 클래스 선언 내에서 메소드 구현을 직접 구현하는 것이 좋지 않지만 문제의 원인은 아닙니다.)

실제로 간단한 기본 (예 :) 당신이 게시 코드의 끝 :

int main() { 

    Cheif c; 

    c.Buy_Cow(); 
    c.GOTO_Store(); 
} 

컴파일 & 실행이, 그것은 (소 값을 예상 결과를 얻을 수 = 1).

그래서이 클래스는 문제가 아니라는 것을 알려줍니다. 프로그램의 나머지 부분에서 문제를 호출하는 방법과 관련이 있습니다.

추적하려면 일반적인 디버깅 기술이 적용됩니다. 예를 들어 :

  • 시도는
  • 알아낼 디버거 및/또는 인쇄 문을 사용하여 문제를 일으키는 이벤트의 특정 순서를 식별하는 경우 일이 잘못
1

반환 유형이 void인지 여부는 중요하지 않습니다. 클래스가 동일한 클래스의 일부이면 클래스의 인스턴스 변수를 사용합니다.

그러나 메소드는 메소드처럼 보이지 않습니다. 이름 앞에 클래스 이름이 없습니다. 이것들은 수업의 선언에 정의되어 있습니까?

+0

그는 자신의 클래스에서 메서드를 구현했을지도 모릅니다. 그러나 그렇게하는 것은 매우 조직화되지 않았을 것이며, 또한 그는 그것들을 기능으로 언급했습니다. –

+0

@thyrgle 그들은 내 수업의 메소드입니다. 그 코드는 현재 무섭게 조직화되어 있지 않습니다. 나는 보통 내 코드에서 체크 포인트에 충돌 한 후 정리한다. – TimothyTech

+0

네, 클래스가 크다. 내 프로그램 사용자의 전체 코드를 게시 할 때마다 나는 언제나 때리지 않는다. 저를위한이 공개 토론에 많은 승리가 없다. – TimothyTech

관련 문제