2014-11-09 3 views
0

읽기 입력 변수를 계산 시트의 범위로 가져 오는 방법을 알 수 없습니다. 변수를 다시 정의하는 것이 부족하다고 선언하는 방법에도 불구하고; 행, 열 및 배열 seattingChart는 calculateSeats 함수로 찾을 수 없습니다.다른 함수의 범위에서 매개 변수 값 얻기

* 추가 ​​된 calculateSeats (seatingChart [rows] [columns]); *

int main() { 

    int seatsSold = 0, oneSeat = 0, seatsTogether = 0, threeSeats = 0, fourSeats = 0, fiveSeats = 0, noSeats = 0, totalSold = 0, 
    rows, columns, pecentageSold, i; 
    const int SIZE = 50; 
    char seatingChart[SIZE][SIZE]; 

    readInput(rows, columns, seatingChart[rows][columns]); // Call read input. 

    // Values of read input are then passed to calculate values. 
    calculateSeats(seatsSold, oneSeat, seatsTogether, threeSeats, fourSeats, fiveSeats, noSeats, i); 

    // Calculated valus are then declared as calculated values and passed to write output. 

    /* int calculatedValues = calculateSeats(seatsSold, oneSeat = 0, seatsTogether = 0, threeSeats = 0, 
              fourSeats = 0, fiveSeats = 0, noSeats = 0); */ 

    // Function is called to write the output. 
    writeOutput(seatsSold, oneSeat, seatsTogether, threeSeats, fourSeats, fiveSeats, noSeats, pecentageSold, totalSold); 

    return 0; // Program ends. 
} 

// Function is needed for assigning values to the char array. Values are then passed from read to calculate. 
int readInput(int & rows, int & columns, int i) { 

    const int SIZE = 50; 
    int seatingChart[SIZE][SIZE]; 
    ifstream inputFile; 
    inputFile.open("SeatingChart.txt"); 
    for (rows; rows < SIZE; rows++) { // Step through the valueless array and give the array values. 
     for (columns; columns < SIZE; columns++) 
      inputFile >> seatingChart[rows][columns]; // Assign the array values from the input file. 
    } 
    inputFile.close(); 

    calculateSeats(seatingChart[rows][columns]); 

    return 0; 
} 

// Function is needed just for calculations. Values are then passed from calculate to write output. 
void calculateSeats(int & seatsSold, int & oneSeat, int & seatsTogether, int & threeSeats, 
        int & fourSeats, int & fiveSeats, int & noSeats, int & sixSeats) { 

    for (int count = 0; count < rows; count++) { // Step back through the array with loaded values. 
     for (int num = 0; num < columns; num++) { 

      // If equal to A and count is equal to count++. Then consecutive read chars is true. 
      if (seatingChart[count][num] == 'A' && seatingChart[count][num] == seatingChart[count][num]++) { 
       seatsTogether++; 

        if (seatsTogether > 1) { 
         threeSeats++; 

        if (seatsTogether > 2) { 
         fourSeats++; 
        } 

        if (seatsTogether > 3) { 
         fiveSeats++; 
        } 

        if (seatsTogether > 4) { 
         sixSeats++; 
        } 
       } 
      } 

      else { 
       seatsSold++; 
       cout << "Total seats sold: " << seatsSold << endl; 
       if (seatsSold == 6) { 
        cout << "Rows with no seats available: " << "row" << seatingChart[count] << endl; 
       } 
      } 
     } 
    } 
} 
+0

C++ 컨테이너 클래스 (예 :'std :: vector')를 사용하면 많은 두통을 피할 수 있습니다. 따라서 나는 그것들을 사용하는 방법을 살펴 보길 강력히 권합니다. 또한 좌석과 관련된 모든 변수가 연결되어있는 것처럼 보입니다.이 경우 'struct'를 사용하여 논리적으로 변수를 코드에 그룹화하는 것이 좋습니다. 또한 참조로 전달되는 변수를 변경하지 않으면 아마도 const 참조 나 값으로 전달해야합니다. – shuttle87

+0

왜 벡터가 배열보다 효율적입니까? –

+1

프로그래머의 효율성이 떨어지고, 벡터를 전달하는 것이 매우 쉽습니다. 현재 프로그램을 컴파일하는 데 문제가 있습니다. 원시 배열 대'std :: vector'의 효율성에 대해 걱정하기 전에 프로그램을 수정하십시오. 이 C++ FAQ 항목은이 문제에 대한 내 생각을 요약 한 것입니다. http://www.parashift.com/c++-faq/arrays-are-evil.html 배열 버전을 만들려면 여기에서 작업해야합니다. readInput에서 배열을 반환 한 다음 배열을 calculateSeats 함수에 대한 포인터로 전달해야합니다 (또는 나중에 최적으로 전역 변수로 만들 수 있음) – shuttle87

답변

0

행, 열 및 좌석표는 main의 범위에서 선언합니다. 이 변수는 스택에 놓입니다. readInput에 표시되도록하려면 전역 변수로 선언해야합니다. 그래서 이렇게 보일 것입니다.

int row, column; 
char seatingChart[SIZE][SIZE]; 

int main() 
{ 
//your code 
} 

세계는 나쁜 스타일로 간주됩니다. http://en.wikipedia.org/wiki/Global_variable

+0

나는 전역 변수에 대해서는 언급하지 않았지만 고맙습니다. 나는 아직 변수를 전달하는 것을 완전히 이해하지 못하고 있다고 생각한다. 내 문제에 대한 간단한 해결책이 없습니까? –

+0

전역 변수를 사용할 수없는 경우 함수에 전달해야합니다. calculateSeats에는 이미 좋은 스타일이 아닌 8 개의 매개 변수가 있습니다. 두 개의 구조체를 선언하는 것이 좋습니다. 하나는 시트 매개 변수를 중앙에 배치하고 다른 하나는 행, column 및 ans seatingChart에 대해 선언하는 것입니다. –

+0

어떻게 그럴 수 있습니까? –