簡體   English   中英

傳遞給特定函數時,數組內容會發生變化

[英]Array contents changing when passing it to a particular function

我正在向我的程序中的所有其他函數傳遞一個填充了一個函數的2d。 出於某種原因,當我在我的一個函數中輸出時,內容似乎發生了變化,雖然它在其它兩個函數中輸出正常。我相信我傳遞數組的方式和其他函數一樣,所以很令人費解內容正在改變我。 此函數與成功傳遞給數組的其他函數之間的唯一區別是,此函數是從函數內而不是從函數中調用的。 有什么關心可以提供一些可能發生的事情嗎?

標題:

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cstdlib>
#include <string>

using namespace std;

void extern readFile(ifstream&, int&, int&, int&, int&, int[][6]);
void extern userInput(int&, int&, int&, int&, int&, int&, char&, char&, int[][6]);
void extern findSeats(int&, int&, int&, int&, int&, int&, char&, char&, int[][6]);



#endif // HEADER_H_INCLUDED

主要:

#include "header.h"

int main()
{
    ifstream inFile;
    int FC_Row, FC_Col, EconRow, EconCol, ticketNum, rowNum;
    int airplane[100][6];
    char ticketType, seatType;

    cout << setw(48) << "Thank you for choosing Cheeta Airlines!" << '\n' << '\n' << endl;
    ifstream inData;

    inData.open("Airplane.txt");

    if (!inData)
    {
        cout << "Cannot open the input file."
             << endl;
            return 1;
    }

    readFile(inFile, FC_Row, FC_Col, EconRow, EconCol, airplane);
    userInput(FC_Row, FC_Col, EconRow, EconCol, ticketNum, rowNum, ticketType, seatType, airplane);
}

readFile :(數組已填充,然后在此函數中打印)

#include "header.h"

void readFile(ifstream& inFile, int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int[][6])
{
    int a, b;
    int airplane[100][6];

    inFile.open("Airplane.txt");

    inFile >> FC_Row >> FC_Col >> EconRow >> EconCol;

    for (a = 0; a < FC_Row; a++)
    for (b = 0; b < FC_Col; b++)
        inFile >> airplane[a][b] ;


    for (a = 0; a < EconRow; a++)
    for (b = 0; b < EconCol; b++)
        inFile >> airplane[a + FC_Row][b] ;


    cout << setw(11)<< "A" << setw(6) << "B"
    << setw(6) << "C" << setw(6) << "D"
    << setw(6) << "E" << setw(6) << "F" << endl;
    cout << " " << endl;

    cout << setw(30) << "First Class: $2,000" << endl;
    cout << '\n';
    for (a = 0; a < FC_Row; a++)
    {
        cout << "Row " << setw(2) << a + 1 << ":";
        for (b = 0; b < FC_Col; b++)
        cout << setw(5) << airplane[a][b] << " ";

        cout << endl;
    }

    cout << '\n';
    cout << setw(30) << "Economy Class: $750" << endl;
    cout << '\n';
    for (a = FC_Row; a < (EconRow + FC_Row); a++)
    {
        cout <<"Row " << setw(2)<< a + 1 << ":";
        for (b = 0; b < EconCol; b++)
        cout << setw(5) << airplane[a][b] << " ";

        cout << endl;
    }


}

userInput :(數組被傳遞給這個函數,因為在這個函數中調用的函數使用了數組。數組在這個函數中打印的內容與填充它的函數相同,readFile)

#include "header.h"

void userInput(int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int& ticketNum, int& rowNum, char& ticketType, char& seatType, int[][6])
{
    char reply;
    int airplane[100][6];

    cout << '\n' << "* The chart above is a seating diagram for your flight. 1 indicates that the " << '\n' << "  seat is taken, 0 indicates that the seat is free.";
    cout << " Refer to this chart to help   you make your class and seating selection." << endl;
    cout << '\n';

    cout << "- How many tickets will you be purchasing today?" << endl;
    cin >> ticketNum;

    while (!cin)
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "INVALID DATA!!!!!" << endl;
          cout << "Number of tickets must be a NUMBER." << endl;
          cout << "try again:" << endl;
          cin  >> ticketNum;
        }

         while (ticketNum > ((FC_Row * FC_Col) + (EconRow * EconCol)))
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "The number of tickets you need exceeds the amount of seats we have on that plane! please choose a different amount of tickets!"
          <<"[ or a different airline :( ]" << endl;
          cin >> ticketNum;
        }

         while (ticketNum < 1)
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "The number of tickets must be greater than 0." << endl;
          cout << "try again:" << endl;
          cin  >> ticketNum;
        }

    for (int j = 0; j < ticketNum; j++)
    {
    cout << '\n' << "Ticket # " << j +1 << " selection:" << endl;
    cout << "--------------------------------"<< endl;

    cout << "- Ticket type? First class or Economy class (enter F/E)" << endl;
    cin >> ticketType;
    ticketType = toupper(ticketType);

    while (ticketType != 'F' && ticketType != 'E')
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "INVALID DATA!!!!!" << endl;
          cout << "Please indicate your ticket type by entering either F (for First Class) " << '\n' << "or E (for Economy Class)" << endl;
          cout << "try again:" << endl;
          cin  >> ticketType;
        }

    cout << "- preferred seat type? Window, Aisle, or No preference (enter W/A/N)" << endl;
    cin >> seatType;
    seatType = toupper(seatType);

    while (seatType != 'W' && seatType != 'A' && seatType != 'N')
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "INVALID DATA!!!!!" << endl;
          cout << "Please indicate your preferred seat type by entering either W (for Window seat), " << '\n' << "A (for Aisle seat), or N (for No preference)" << endl;
          cout << "try again:" << endl;
          cin  >> seatType;
        }


    cout << "- row number? 1-" << (FC_Row) << " in First Class," << (FC_Row+1) << "-" << (FC_Row + EconRow) << " in Economy Class" << endl;
    cin >> rowNum;

    while (rowNum > (FC_Row + EconRow))
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "Your row number exceeds" << (FC_Row + EconRow) << endl;
          cout << "try again:" << endl;
          cin >> rowNum;
        }

    while (rowNum < 1)
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "The row number must be greater than 0." << endl;
          cout << "try again:" << endl;
          cin  >> rowNum;
        }

    if (ticketType == 'F')
        {
                while (rowNum > (FC_Row))
                    {
                    cout << "That row is not located in our first class section. Would you like to change your class so you can sit in that row (Y/N)?" << endl;
                    cin >> reply;
                    reply = toupper(reply);

                        while (reply != 'Y' && reply != 'N')
                            {
                            cin.clear();
                            while(cin.get()!='\n');
                            cout << "Please indicate your answer with Y (yes) or N (no)." << endl;
                            cout << "try again:" << endl;
                            cin >> reply;
                            reply = toupper(reply);
                            }
                         if (reply == 'Y')
                            {
                                ticketType = 'E';
                                break;
                            }
                        else
                            {
                                cout << "Then choose a row numbered 1-" << (FC_Row) << endl;
                                cin >> rowNum;
                            }
                    }
        }

        if (ticketType == 'E')
        {
            while (rowNum <= (FC_Row))
                    {
                    cout << "That row is not located in our economy class section. Would you like to change your class so you can sit in that row (Y/N)?" << endl;
                    cin >> reply;
                    reply = toupper(reply);

                        while (reply != 'Y' && reply != 'N')
                            {
                            cin.clear();
                            while(cin.get()!='\n');
                            cout << "Please indicate your answer with Y (yes) or N (no)." << endl;
                            cout << "try again:" << endl;
                            cin >> reply;
                            reply = toupper(reply);
                            }
                         if (reply == 'Y')
                            {
                                ticketType = 'F';
                                break;
                            }
                        else
                            {
                                cout << "Then choose a row numbered " << (FC_Row + 1) << "-" << (FC_Row + EconRow) << endl;
                                cin >> rowNum;
                            }
                    }
        }
        findSeats(FC_Row, FC_Col, EconRow, EconCol, ticketNum, rowNum, ticketType, seatType, airplane);
    }
}

findSeats :(這是數組似乎改變其內容的函數。我知道我正在正確打印數組,因為我正在使用完全相同的代碼來打印它,正如我在其他函數中正確打印時所使用的那樣。)

#include "header.h"

void findSeats(int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int& ticketNum, int& rowNum, char& ticketType, char& seatType, int[][6])
{
    int airplane[100][6], a, b;  
}

你在主要聲明飛機[] []。 然后你在readFile中重新聲明它,它實際上隱藏了你的第一個聲明。 數組在堆棧中聲明,當你的程序從readFile返回時,你剛剛填充的數組將被銷毀,你將得到包含giberish的原始數組。 這就是你打印不同東西的原因。 僅使用一個陣列。 不要在readFile函數中重新聲明它。

您實際上並沒有將數組傳遞給任何函數。 您的聲明“int [] [6]”聲明該函數采用具有六列的二維數組,但由於您沒有給它命名,因此不能在函數中使用它。 您聲明的飛機變量是每個函數中的本地完全獨立的變量。 嘗試寫你的功能

void findSeats(int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int& ticketNum, int& rowNum, char& ticketType, char& seatType, int airplane[][6])
{
  int a,b;
}

在每個功能中進行相同的更改。

你在談論你的airplane陣列嗎?

你沒有傳遞它,你在每個函數中聲明它。

要確保您的函數不會修改通過引用傳遞的輸入參數,請使用const

例如

const int& someArray[]

或者類似的應該禁止腳本執行此操作(如果不打算將其更改,則可能會拋出編譯時錯誤)。

問候,
丹尼斯·M

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM